Sign in to follow this  
TwinX

Pointing to structs?

Recommended Posts

I was wondering after revising my pointers whether you can point to a struct you create. It said in one tut that a structure was justa group of variables of mixed or equal type. so could you make a pointer a structure. And then dynamically assign memory within the dynamic struct? EDIT: this is in C++

Share this post


Link to post
Share on other sites
well of course!

struct test {

int test;
string word;
float wildNumber;
};

test *imAPtr = new test;
imAPtr->test = 12;
imAPtr->word = "See I'm a pointer of a struct.";
imAPtr->wildNumber = 45.383833;

Share this post


Link to post
Share on other sites

struct point {
int *x, *y;
};

point *p = new point();
point->x = new int();
point->y = new int();

...
delete p->x;
delete p->y;
delete p;

// or maybe you can make a destructor
point::~point() {
delete x;
delete y;
}
// so that deleting a point deletes it's stuff
// and a constructor
point::point() {
x = new (std::nothrow) int;
y = new (std::nothrow) int;

if (!x || !y)
throw OutOfMemory_Exception("WTF? Not enough memory to allocate an INT???");
}

// and then
point *p = new point;
delete p;


I'm not sure what you mean by dynamically allocate memory withing the stuct...

Share this post


Link to post
Share on other sites
Allocating each member is not necessary. Not only does it tax the memory allocation code (a common bottleneck), it's already taken into account with constructors, destructors and initialization lists.

Using new and delete a way to control the scope of your objects. There are downsides though, the most obvious one being forgetting to delete your objects. In small programs, you'll probably never notice, but in large programs, memory leaks can be very painful.

Share this post


Link to post
Share on other sites
I was guna have a structure with all the details of my file i want to import in. Then when it reaches a keyword in the file it will create a new struct and fill that with data is it possible to have a structure within a structure so itd be like structure.struct2.variable
and then create sub structures using new for example the planes in a cube
i have one structure called cube then substructures plane1,plane2,etc then if i want another cube in the scene i could have cube2 which could be *cube++ kind of thing.

PS. Agi thats what i wated but i dont think id acually have to use that now

PSPS. IS this code kinda right except for the file_op commands.


int line=0;

struct solid {
struct plane{
int side_start;
int id;
int x;
int y;
int z;
char material[30];
float rotation;
}
int id;
};


int main()
{
char str[30];
int ptr=0;
int ptrplane=0;
fstream file_op("test.vmf", ios::in);
while(!file_op.eof())
{
line++;
if (str == "solid")
{
solid *ptr = new solid;
ptr++;
*ptr.id = *ptr--.id+1;
}
if (str =="side")
{
*ptr.plane *ptrplane = new *ptr.plane;
ptrplane++;
*ptrplane.id = *ptrplane--.id +1;
*ptrplane.side_start = line;
}
if (line == *ptrplane.side_start+3)
{
file_op.getline (str,x);
strcpy(*ptrplane.x,str);
file_op.getline (str,y);
strcpy(*ptrplane.y,str);
file_op.getline (str,z);
strcpy(*ptrplane.z,str);
}
if (line == *ptrplane.side_start+4)
{
file_op.getline (str,material);
strcpy(*ptrplane.material,str);
}
if (line == *ptrplane.side_start+7)
{
file_op.getline (str,rotation);
strcpy(*ptrplane.rotation,str);
}
return 0;
}





i know tht the bit with side and solid is wrong because they havnt been read into str yet but apart from that plz tell me what to do I am a newbie dont flame me

[Edited by - TwinX on April 16, 2006 9:52:31 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this