Would this cause a memory leak?

A

alexrixhardson

Hello guys,

Please, be gentle, as I am a C newby :). I am wondering whether this
piece of code leaks memory:

somestring = (char *)malloc(initialsize + 1);
....
somestring = (char *)realloc(somestring, newsize + 1);
....
somestring = somestring + moveforward;
....
free(somestring);


Thanks, Alex
 
M

Mark Bluemel

Hello guys,

Please, be gentle, as I am a C newby :). I am wondering whether this
piece of code leaks memory:

somestring = (char *)malloc(initialsize + 1);
...
somestring = (char *)realloc(somestring, newsize + 1);
...
somestring = somestring + moveforward;
...
free(somestring);

Virtually the same question was asked not long ago - see the Google
Groups stuff here <http://tinyurl.com/yquy75>.

Summary - it won't leak, it will break.
 
S

santosh

Hello guys,

Please, be gentle, as I am a C newby :). I am wondering whether this
piece of code leaks memory:

somestring = (char *)malloc(initialsize + 1);
...
somestring = (char *)realloc(somestring, newsize + 1);

Since realloc returns NULL on failure, *but* leaves the original memory
untouched, you should always save the address of your original block before
trying to reallocate it. Otherwise there'll be no way to get access to it,
if realloc overwrites the address with 0.
...
somestring = somestring + moveforward;
...
free(somestring);

Yes. free can take only a value returned by a previous call to
malloc/realloc/calloc. Anyother value is a sure-fire way towards undefined
behaviour, which need not necessarily be a memory leak.

Note also that casting the return value of malloc/realloc/calloc is not
necessary in C, and doing so can hide certain errors.
 
A

alexrixhardson

Cheers. I now realise the awful quality of my posted code!

I've also noticed in the link you suggested, that this is bad:

char *p = (char *)malloc(9);

What would be the prefered way?

Thanks, Alex
 
S

santosh

Cheers. I now realise the awful quality of my posted code!

I've also noticed in the link you suggested, that this is bad:

char *p = (char *)malloc(9);

What would be the prefered way?

char *p = malloc(ELEMENTS * sizeof *p);

For the case of char, sizeof is by definition one, so it can be omitted. The
main advantage to this method, apart from the removal of the cast of the
return value, is that if the type of *p happens to change, the statement is
automatically updated. No manual change is necessary.

Note that many programmers do it the other way -

char *p = malloc(ELEMENTS * sizeof(char));

The really inadvisable thing is the cast of the return value.
 
E

Eric Sosman

Mark Bluemel wrote On 08/01/07 11:58,:
Virtually the same question was asked not long ago - see the Google
Groups stuff here <http://tinyurl.com/yquy75>.

Summary - it won't leak, it will break.

It could leak if the realloc() fails: Then `somestring'
will be set to NULL, and if `somestring' was the only
pointer to the original block (which is still allocated) ...

But as you say, it *will* break (if `moveforward' is
non-zero).
 
A

Army1987

Cheers. I now realise the awful quality of my posted code!

I've also noticed in the link you suggested, that this is bad:

char *p = (char *)malloc(9);

What would be the prefered way?

char *p = malloc(9);
(or malloc(9 * sizeof *p) if you want it to fix itself should you
change the type of *p).
 
P

pete

santosh said:
char *p = malloc(ELEMENTS * sizeof *p);

I always do it that way, even if it's something like:

*buff++ = malloc(ELEMENTS * sizeof **buff++);
For the case of char, sizeof is by definition one,
so it can be omitted. The main advantage to this method,
apart from the removal of the cast of the return value,
is that if the type of *p happens to change, the statement is
automatically updated. No manual change is necessary.

Note that many programmers do it the other way -

char *p = malloc(ELEMENTS * sizeof(char));

I think that

#include <stdlib.h>

somestring = malloc(initialsize + 1);

is probably more appropriate for the string work that OP is doing.
The really inadvisable thing is the cast of the return value.

There must also be

#include <stdlib.h>

otherwise the code will be undefined
and the compiler may suggest using a cast.
 
A

alexrixhardson

Thank you all for your comments once again - it's alot of help for me.

p.s.: bit off-topic: which book about C would you recommend?
 
R

Richard

Thank you all for your comments once again - it's alot of help for me.

p.s.: bit off-topic: which book about C would you recommend?

Very on topic. And Keith's suggestion is pretty common place throughout
the C programming world.

I don't know how they did it, but they just got it "right". It is an
excellent introduction to programming in its own right - regardless of C
being the main subject. It is well paced, with well thought out examples
without too much anal retentiveness which plagues other such books.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top