LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-07-2008, 05:41 AM   #1
carlcs
LQ Newbie
 
Registered: Apr 2008
Posts: 14

Rep: Reputation: 0
how to free this structure


typedef struct{
int a;
char *b;
}A;

typedef struct{
int c;
char *d;
A *A1;
}B;

B *B1;

B1 = malloc(sizeof(B));
A1 = malloc(sizeof(A));

d = malloc(100);
b = malloc(10);

how to free , is the structure need a for loop to free?
 
Old 05-07-2008, 06:09 AM   #2
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 492

Rep: Reputation: 50
Quote:
B1 = malloc(sizeof(B));
you just get the place to keep two pointers, one on a character, and the other on a A struct. But you haven't reserved the room for the embedded A struct! So make something like "B1->A1->a = 10" IS AN ERROR, you have to
Code:
B1 = malloc(sizeof(B));
A* A1 = malloc (sizeof (A));
B1->A1 = A1;
or directly, if you prefer,
Code:
B1 = malloc(sizeof(B));
B1->A1 = malloc (sizeof (A));
Same comments for the other structure fields
 
Old 05-07-2008, 10:15 PM   #3
carlcs
LQ Newbie
 
Registered: Apr 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Thank you for pointing my error. Now I allocate like below
Quote:
B1 = malloc(sizeof(B));
B1->A1 = malloc (sizeof (A));
how do i free them if i allocate b and d
Quote:
d = malloc(100);
b = malloc(10);


Is the structure need a for loop to free?
 
Old 05-09-2008, 01:45 AM   #4
jf.argentino
Member
 
Registered: Apr 2008
Location: Toulon (France)
Distribution: FEDORA CORE
Posts: 492

Rep: Reputation: 50
Pointers are only addresses. The address they point on can be allocated or not. What you've to understand from this sentence is that each "malloc" must have its only one "free" on the same address, which is not necessary the same pointers.
With an example:
Code:
B1 = malloc(sizeof(B));
A* A1 = malloc (sizeof (A));
A1->b= malloc (100);
B1->A1 = A1; /* B1->A1 and A1 are 2 different pointers on the same address */
char* e = malloc (sizeof (1000));
B1->d = e;
You can un-allocate these with:
free (A1->b); /* or free (B1->A1->b), but not the two */
free (A1); /* or free (B1->A1, but not the two) */
You can't freeing A1 then A1->b since after free (A1), the pointer A1->b doesn't exist anymore, but the address it points on is always allocated, it's a memory leak! For B1, you can free (B1) then free (e), or free (B1->d) then free (B1), but not free (B1), then free (B1->d).
 
Old 05-10-2008, 11:12 AM   #5
carlcs
LQ Newbie
 
Registered: Apr 2008
Posts: 14

Original Poster
Rep: Reputation: 0
Thank you very much. I has been solved this problem.
 
  


Reply

Tags
free, structure


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Spread Open Media: Free Formats Benefit Free Software LXer Syndicated Linux News 0 09-14-2007 06:41 PM
LXer: Free Me: a DVD about free culture and free software LXer Syndicated Linux News 0 03-12-2007 12:01 PM
Wait Free Data Structure neo_119 Programming 0 11-27-2004 01:44 AM
Free, free,FREE=? no money, = Freedom? murshed Linux - Newbie 8 01-20-2003 07:01 AM


All times are GMT -5. The time now is 07:51 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration