By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,804 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,804 IT Pros & Developers. It's quick & easy.

malloc() and free()

P: n/a
I need three buffers and am going to allocate memory for them, is it legal
to allocate and free the required memory with one call to malloc and free?

unsigned char *b1, *b2, *b3;

if( (b1 = (unsigned char *)malloc( 30000 )) != NULL )
{
b2 = b1 + 10000;
b3 = b2 + 10000;

/* use b1, b2 and b3 here */

free( b1 );
}

Thanks for any assistance.

----------------------------
Brian
Nov 13 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Pegboy" <pe****@neb.rr.com> writes:
I need three buffers and am going to allocate memory for them, is it legal
to allocate and free the required memory with one call to malloc and free?
Sure. Most of the time, calls to malloc() and free() should be
balanced--one malloc(), one free().
unsigned char *b1, *b2, *b3;

if( (b1 = (unsigned char *)malloc( 30000 )) != NULL )
I don't recommend casting the return value of malloc():

* The cast is not required in ANSI C.

* Casting its return value can mask a failure to #include
<stdlib.h>, which leads to undefined behavior.

* If you cast to the wrong type by accident, odd failures can
result.
{
b2 = b1 + 10000;
b3 = b2 + 10000;

/* use b1, b2 and b3 here */

free( b1 );
}


This code looks fine.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
Nov 13 '05 #2

P: n/a
Thanks for your reply.

-----------------------
Brian

"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@pfaff.stanford.edu...
"Pegboy" <pe****@neb.rr.com> writes:
I need three buffers and am going to allocate memory for them, is it legal to allocate and free the required memory with one call to malloc and
free?
Sure. Most of the time, calls to malloc() and free() should be
balanced--one malloc(), one free().
unsigned char *b1, *b2, *b3;

if( (b1 = (unsigned char *)malloc( 30000 )) != NULL )


I don't recommend casting the return value of malloc():

* The cast is not required in ANSI C.

* Casting its return value can mask a failure to #include
<stdlib.h>, which leads to undefined behavior.

* If you cast to the wrong type by accident, odd failures can
result.
{
b2 = b1 + 10000;
b3 = b2 + 10000;

/* use b1, b2 and b3 here */

free( b1 );
}


This code looks fine.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman

Nov 13 '05 #3

P: n/a
Mac
On Sat, 22 Nov 2003 14:22:35 +0000, Ben Pfaff wrote:
"Pegboy" <pe****@neb.rr.com> writes:
I need three buffers and am going to allocate memory for them, is it legal
to allocate and free the required memory with one call to malloc and free?


Sure. Most of the time, calls to malloc() and free() should be
balanced--one malloc(), one free().
unsigned char *b1, *b2, *b3;

if( (b1 = (unsigned char *)malloc( 30000 )) != NULL )


I don't recommend casting the return value of malloc():

* The cast is not required in ANSI C.

* Casting its return value can mask a failure to #include
<stdlib.h>, which leads to undefined behavior.

* If you cast to the wrong type by accident, odd failures can
result.
{
b2 = b1 + 10000;
b3 = b2 + 10000;

/* use b1, b2 and b3 here */

free( b1 );
}


This code looks fine.


Well, the devil is in the details, as they say. If your (the OP's)
definition of using b1 involves changing its value (that is, making it
point anywhere else) then you can no longer call free with it. In fact,
you would have a memory leak at that point. (I know Ben already knows
this. He was just assuming that the OP wouldn't modify b1.)

But if you never use b1 on the left side of an assignment, or otherwise
change where it points, then you are OK.

Mac
--

Nov 13 '05 #4

P: n/a
On Sat, 22 Nov 2003 22:00:20 GMT, "Pegboy" <pe****@neb.rr.com> wrote:
I need three buffers and am going to allocate memory for them, is it legal
to allocate and free the required memory with one call to malloc and free?

unsigned char *b1, *b2, *b3;

if( (b1 = (unsigned char *)malloc( 30000 )) != NULL )
{
b2 = b1 + 10000;
b3 = b2 + 10000;

/* use b1, b2 and b3 here */

free( b1 );
}


Absolutely. Of course you must test b1 after the malloc before using
it any computation.
<<Remove the del for email>>
Nov 13 '05 #5

P: n/a

"Barry Schwarz" <sc******@deloz.net> wrote in message
news:bp**********@216.39.134.79...
On Sat, 22 Nov 2003 22:00:20 GMT, "Pegboy" <pe****@neb.rr.com> wrote:
I need three buffers and am going to allocate memory for them, is it legalto allocate and free the required memory with one call to malloc and free?
unsigned char *b1, *b2, *b3;

if( (b1 = (unsigned char *)malloc( 30000 )) != NULL )
{
b2 = b1 + 10000;
b3 = b2 + 10000;

/* use b1, b2 and b3 here */

free( b1 );
}


Absolutely. Of course you must test b1 after the malloc before using
it any computation.


Maybe that's why he put the malloc within an if condition

--
poncho


Nov 13 '05 #6

P: n/a

"Pegboy" <pe****@neb.rr.com> wrote in message
I need three buffers and am going to allocate memory for them, is it
legal to allocate and free the required memory with one call to malloc
and free?

Yes, though it is generally not a good idea, since a maintaining programmer
will be puzzled that some dynamic buffers don't seem to be being freed. If
the buffers are large you will also use memory less efficiently due to the
requirement for bigger chunks.

If the buffers are not of the same type you also need to be careful about
alignment.

However if you have a large number of small arrays, maybe stored in the same
structure, then this is legal and might improve efficiency.

Nov 13 '05 #7

P: n/a
Thanks to all, all good points.

"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:bp**********@newsg3.svr.pol.co.uk...

"Pegboy" <pe****@neb.rr.com> wrote in message
I need three buffers and am going to allocate memory for them, is it
legal to allocate and free the required memory with one call to malloc
and free?
Yes, though it is generally not a good idea, since a maintaining

programmer will be puzzled that some dynamic buffers don't seem to be being freed. If
the buffers are large you will also use memory less efficiently due to the
requirement for bigger chunks.

If the buffers are not of the same type you also need to be careful about
alignment.

However if you have a large number of small arrays, maybe stored in the same structure, then this is legal and might improve efficiency.

Nov 13 '05 #8

P: n/a
Groovy hepcat Barry Schwarz was jivin' on 23 Nov 2003 02:39:47 GMT in
comp.lang.c.
Re: malloc() and free()'s a cool scene! Dig it!
On Sat, 22 Nov 2003 22:00:20 GMT, "Pegboy" <pe****@neb.rr.com> wrote:
if( (b1 = (unsigned char *)malloc( 30000 )) != NULL )
^^^ ^^ ^^ ^^^^ ^
Absolutely. Of course you must test b1 after the malloc before using
it any computation.


Wakey wakey, Bazza!

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 13 '05 #9

P: n/a
Groovy hepcat Malcolm was jivin' on Sun, 23 Nov 2003 09:47:48 -0000 in
comp.lang.c.
Re: malloc() and free()'s a cool scene! Dig it!
"Pegboy" <pe****@neb.rr.com> wrote in message
I need three buffers and am going to allocate memory for them, is it
legal to allocate and free the required memory with one call to malloc
and free?
Yes, though it is generally not a good idea, since a maintaining programmer
will be puzzled that some dynamic buffers don't seem to be being freed. If
the buffers are large you will also use memory less efficiently due to the
requirement for bigger chunks.


That's usually more efficient, because it cuts down on allocation
overhead (both time and RAM).
If the buffers are not of the same type you also need to be careful about
alignment.
Even if they are of the same type. But what he's actually doing is
fine.
However if you have a large number of small arrays, maybe stored in the same
structure, then this is legal and might improve efficiency.


How would that improve efficiency when each small buffer has to be
allocated, taking time and space to do so for each one?

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 13 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.