free struct and malloc

C

C++ Shark

Hi,
I have a structure which contains another structure in the following
way:


struct vertex {
double x, y, z; }

struct cube {
struct vertex v;
}


struct tree {

struct tree *child1, *child2;
struct cube c
}

now, i start with allocating memory to a tre pointer in the following
way:


struct tree *Mtree;
*Mtree is grown by allocating memory to child1 and child2.

now, i want to free up the memory used by some nodes of *Mtree. I
tried doing the following:

free(Mtree->c.v.x) which didn't work. If i try free(&(Mtree->c.v.x))
the program compiles, but i am still able to access the data at
Mtree->c.v.x, and so i don't know what to do. can anyone give me some
pointers on this issue? thanking you,
Craig
 
R

Régis Troadec

C++ Shark said:
Hi,
Hi,

I have a structure which contains another structure in the following
way:


struct vertex {
double x, y, z; }

struct vertex
{
double x, y, z;
};
struct cube {
struct vertex v;
}

Mmmm...does your cube only represented thanks to one vertex?
It should be at least 8 vertices (as far I know in spatial geometry).

struct cube
{
struct vertex v;
};

is correct but it probably should be something like:
struct cube
{
struct vertex * v; /* pehaps better with an aliased type (typedef) */
/* or struct vertex v[8]; */
};
struct tree {

struct tree *child1, *child2;
struct cube c
}

struct tree
{
struct tree *child1, *child2;
struct cube c;
};
now, i start with allocating memory to a tre pointer in the following
way:


struct tree *Mtree;
*Mtree is grown by allocating memory to child1 and child2.

now, i want to free up the memory used by some nodes of *Mtree. I
tried doing the following:

free(Mtree->c.v.x) which didn't work. If i try free(&(Mtree->c.v.x))

Mtree is your pointer. Mtree->c.v.x stands for the x member of the v member
of the c member of a tree structure. It's not a pointer.
Simply free(Mtree); to desallocate your allocated Mtree.


Regis
 
R

Richard Tobin

C++ Shark said:
struct vertex {
double x, y, z; }

So a struct vertex contains 3 doubles.
struct cube {
struct vertex v;
}

And a struct cube contains a struct vertex.
struct tree {

struct tree *child1, *child2;
struct cube c
}

And a struct tree contains a struct cube.
free(Mtree->c.v.x) which didn't work.

You seem to be trying to free one of the doubles, but it's just part of
the struct tree you allocated. You can't free part of a structure. You
can only free things you allocate. If you're allocating struct trees,
the only things you can free are struct trees.

If you want to free things at smaller granularity, you'll have to
allocate them at smaller granularity, and put pointers to them in the
structs instead of making them part of the structs.

-- Richard
 
M

Mitchell

struct tree *Mtree;
*Mtree is grown by allocating memory to child1 and child2.

now, i want to free up the memory used by some nodes of *Mtree. I
tried doing the following:

free(Mtree->c.v.x) which didn't work. If i try free(&(Mtree->c.v.x))
the program compiles, but i am still able to access the data at
Mtree->c.v.x, and so i don't know what to do. can anyone give me some
pointers on this issue? thanking you,
Craig

In order words, free what you allocate. If you do

x = malloc(2000000000);

then free it through

free(x);

and not

free(x.a);
free(x->a);

cheers!
 
C

C++ Shark

Régis Troadec said:
struct vertex
{
double x, y, z;
};


Mmmm...does your cube only represented thanks to one vertex?
It should be at least 8 vertices (as far I know in spatial geometry).

yea because its sides are always parallel to the axes.
struct cube
{
struct vertex v;
};

is correct but it probably should be something like:
struct cube
{
struct vertex * v; /* pehaps better with an aliased type (typedef) */
/* or struct vertex v[8]; */
};
struct tree {

struct tree *child1, *child2;
struct cube c
}

struct tree
{
struct tree *child1, *child2;
struct cube c;
};
now, i start with allocating memory to a tre pointer in the following
way:


struct tree *Mtree;
*Mtree is grown by allocating memory to child1 and child2.

now, i want to free up the memory used by some nodes of *Mtree. I
tried doing the following:

free(Mtree->c.v.x) which didn't work. If i try free(&(Mtree->c.v.x))

Mtree is your pointer. Mtree->c.v.x stands for the x member of the v member
of the c member of a tree structure. It's not a pointer.
Simply free(Mtree); to desallocate your allocated Mtree.


Regis
 
O

Ori Bernstein

Mtree is your pointer. Mtree->c.v.x stands for the x member of the v member
of the c member of a tree structure. It's not a pointer.
Simply free(Mtree); to desallocate your allocated Mtree.
Sorry if this is a dumb question (C noob here), but would free(Mtree) free
all the pointers inside the struct?
 
B

Ben Pfaff

Ori Bernstein said:
Sorry if this is a dumb question (C noob here), but would free(Mtree) free
all the pointers inside the struct?

No. free() and malloc() should normally be paired: one free() for
each malloc().
 
D

Dan Pop

In said:
Sorry if this is a dumb question (C noob here), but would free(Mtree) free
all the pointers inside the struct?

Could you quote the text from your C book leading to this conclusion?

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top