Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-12-2006, 08:17 AM   #1 (permalink)
jimmiy
Registered User
 
Join Date: Feb 2006
Posts: 1
jimmiy is on a distinguished road
memory allocation

Hi all"

I have a quick question for you:

I have something like the following:
Code:
typedef struct recordinfo
{
   int ind;
   char *str;
} recordinfo1;
and somewhere in the code i do the following:
Code:
users   = (struct recordinfo *) malloc ( sizeof(struct recordinfo) * 2000 );
after running the one cycle in a loop, I would like to free the memory that I allocated.

I do the following:
Code:
free ((recordinfo *) users );
but that doesn't free everything because inside recordinfo another pointer (char *str).

do you know how to free all the memory allocated for this array?


Thanks

Jimmiy
jimmiy is offline   Reply With Quote
Old 02-12-2006, 08:28 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Quote:
free ((recordinfo *) users );
This is just freeing the users pointer allocated memory, you need to cyckle through your users in order to free all the memory occupied by it ie:
Code:
for (i=0; i < 2000; ++i)
    free  (users[i].str);
free(users);
Since the str would be allocated elsewhere, the users pointer allocation has no knowledge of what size is actualy stored in the allocated memory for the str member of the struct, thus it needs to explecitly be free'd.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
USB sticks / usb memory drives Valmont Lounge 7 11-02-2005 06:57 AM
Accessing a specific memory address madtown54 Standard C, C++ 1 03-16-2005 06:17 AM
Problem with Memory (EXC_BAD_ACCESS) m8j Standard C, C++ 1 11-18-2004 04:50 AM
dynamic allocation..urgent help needed!!! kashif Standard C, C++ 4 04-21-2003 09:50 AM


All times are GMT -8. The time now is 03:58 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting