Coder's Guild Mailing List

RE: Memory management in C...

Posted by rohan mitchell on 2001-04-19

As far as i kno, new and delete are c++ only.
apointer = malloc(sizeof(struct_name)) should work,
except it wont allocate the memory for the pointers in
the structure, so you have to malloc and free them
seperatly. You could always make it in a function, eg

struct mystruct *new_mystruct() {
	struct mystruct *mystruct;

	mystruct = malloc(sizeof(mystruct));
	mystruct->pointer1 = (char *) malloc(sizeof(char));
	// etc...

	return mystruct;
}

and a free_mystruct() as well.

Hope this helps.
Rohan

>From: Pradeep Bisht <x00bisht@xxxxx.xx.xxx>
>Subject: memory management in C.....
>
>hi everybodi...
>
> i 'm having a structure which contains sum chars
,ints ,doubles & pointers as
>elements...
>
>Date: Tue, 17 Apr 2001 06:39:28 -0500
>From: "James Steele" <sfox@xxxxxxxxxx.xxxxxxxxxxx.xxx>
>Subject: Re: memory management in C.....
>
>If you want my opinion, use 'new' and 'delete'.  To
me, it's simpler than messing around
>with the functions, as 'new' automatically calculates
how much to allocate on its own, and
>'delete' knows how much to deallocate on its own.  No
need to tell it.  Last I checked,
>new and delete should be supported by both C and C++
compilers.
>
>But shouldn't this:
>
>apointer = malloc(sizeof(your_classname));
>
>work?
>
>/********************/
>
>> hi everybodi...
>>
>>  i 'm having a structure which contains sum chars
,ints ,doubles & pointers as
>> elements...
>>
>>  now i want to allocate a big chunk of memory using
malloc so that the memory
>> allocated is just enough to hold all the members of
the structure including the
>> memory space pointed to by the member pointers ..how
this kin be done so that
>> memory wasted is minimum (since ,i think malloc
always take care of aligning a
>> double at a word boundary)...And also how kin i make
the member pointers to
>> point to sum parts of the memory from that allocated
big chunk onli...
>>
>>  thanks in advance...
>>
>> bi...bi...
>> with luv...
>> pradeep...



__________________________________________________________________
Get your free Australian email account at http://www.start.com.au