Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Will it leak the memory?

Reply
Thread Tools

Will it leak the memory?

 
 
KG
Guest
Posts: n/a
 
      07-24-2007
Hi,
I do have a question.

int main()
{
char *p = (char *)malloc(9);

strcpy(p,"TajMahal");

p++;

free(p);

}

Does this program results in memory leak?
Please explain.

Regards,
Kiran Patil

 
Reply With Quote
 
 
 
 
Chris Dollin
Guest
Posts: n/a
 
      07-24-2007
KG wrote:

> Hi,
> I do have a question.
>
> int main()
> {
> char *p = (char *)malloc(9);
>
> strcpy(p,"TajMahal");
>
> p++;
>
> free(p);
>
> }
>
> Does this program results in memory leak?


It results in undefined behaviour. The only legal arguments to `free`
are null and a pointer returned from malloc that hasn't been freed.

There's no way of telling what that does from the C standard.

If you omit the `p++`, then the program terminates with memory
still allocated. Whether this results in a memory leak depends
on (a) what you mean by "memory leak" and (b) what the surrounding
environment does when the program exits. It is /not/ specified that
all mallocated memory is "returned to the system".

--
Chris "consider embedded systems" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      07-24-2007
KG wrote:
> Hi,
> I do have a question.
>
> int main()
> {
> char *p = (char *)malloc(9);


You haven't included a valid prototype for malloc and your cast has
hidden a potentially value compiler diagnostic.

>
> strcpy(p,"TajMahal");
>
> p++;
>
> free(p);


This results in undefined behaviour. The address you passed to free()
isn't an address of a a block of space allocated with malloc(), calloc()
or realloc().

> }
>
> Does this program results in memory leak?


If you're lucky the program will crash. If you're very unlucky, the
computer will grow arms, produce a knife and stab you.
<http://www.xkcd.com/293/>

> Please explain.


Please explain why you felt it was appropriate to post this nonsense.
 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      07-24-2007
Chris Dollin wrote:
> KG wrote:
>
>
>>Hi,
>>I do have a question.
>>
>>int main()
>>{
>>char *p = (char *)malloc(9);
>>
>>strcpy(p,"TajMahal");
>>
>>p++;
>>
>>free(p);
>>
>>}
>>
>>Does this program results in memory leak?

>
>
> It results in undefined behaviour. The only legal arguments to `free`
> are null and a pointer returned from malloc that hasn't been freed.
>
> There's no way of telling what that does from the C standard.
>
> If you omit the `p++`, then the program terminates with memory
> still allocated. [snip]


Did you really mean this?
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-24-2007
Mark Bluemel said:

> Chris Dollin wrote:
>> KG wrote:


<snip>

>>>p++;
>>>
>>>free(p);
>>>
>>>}
>>>
>>>Does this program results in memory leak?

>>
>>
>> It results in undefined behaviour. The only legal arguments to `free`
>> are null and a pointer returned from malloc that hasn't been freed.
>>
>> There's no way of telling what that does from the C standard.
>>
>> If you omit the `p++`, then the program terminates with memory
>> still allocated. [snip]

>
> Did you really mean this?


In a sense, he might be right, but only if you squint and look at the
language in a funny way. Quoth the Stannit[1]:

"The free function causes the space pointed to by ptr to be deallocated,
that is, made available for further allocation."

So, from the program's perspective, the memory has been deallocated, so
he's wrong. But from the 'program + runtime library' perspective, it
could be (and has been) argued that free() must work in such a way as
to keep the memory available for further allocation *to the program*,
and therefore it remains allocated as far as the host system (e.g. the
OS) is concerned, at least until the program has terminated.

Some people think this is a powerful and meaningful argument, and others
think it's a load of fetid dingo kidneys[2]. Personally, I don't care
either way.

[1] http://en.wikipedia.org/wiki/Evening_Standard#Trivia
[2] http://www.bbc.co.uk/dna/h2g2/A76961

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      07-24-2007
Mark Bluemel <(E-Mail Removed)> writes:

> KG wrote:
>> Hi,
>> I do have a question.
>>
>> int main()
>> {
>> char *p = (char *)malloc(9);

>
> You haven't included a valid prototype for malloc and your cast has
> hidden a potentially value compiler diagnostic.
>
>>
>> strcpy(p,"TajMahal");
>>
>> p++;
>>
>> free(p);

>
> This results in undefined behaviour. The address you passed to free()
> isn't an address of a a block of space allocated with malloc(),
> calloc() or realloc().
>
>> }
>>
>> Does this program results in memory leak?

>
> If you're lucky the program will crash. If you're very unlucky, the
> computer will grow arms, produce a knife and stab
> you. <http://www.xkcd.com/293/>


Oh god no. I wondered how long it would take before this became the new
"in joke" for your "undefined" warnings.

Look, it isn't funny because a toaster will NOT, under any
circumstances, grow arms and stab you. It might electrocute you.


>
>> Please explain.

>
> Please explain why you felt it was appropriate to post this nonsense.


--
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      07-24-2007
Richard Heathfield <(E-Mail Removed)> writes:

> Mark Bluemel said:
>
>> Chris Dollin wrote:
>>> KG wrote:

>
> <snip>
>
>>>>p++;
>>>>
>>>>free(p);
>>>>
>>>>}
>>>>
>>>>Does this program results in memory leak?
>>>
>>>
>>> It results in undefined behaviour. The only legal arguments to `free`
>>> are null and a pointer returned from malloc that hasn't been freed.
>>>
>>> There's no way of telling what that does from the C standard.
>>>
>>> If you omit the `p++`, then the program terminates with memory
>>> still allocated. [snip]

>>
>> Did you really mean this?

>
> In a sense, he might be right, but only if you squint and look at the
> language in a funny way. Quoth the Stannit[1]:


Why would you say this? For the sake of this question he is totally
incorrect.

When he has called "free" the memory is then unavailable in any decent
program. End of subject.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-24-2007
Richard said:

<snip>

> When he has called "free" the memory is then unavailable in any decent
> program.


When he has called free, the memory is available for reallocation. But
you do have a point, in that it is perfectly reasonable to call it
"unavailable" too, in the sense that the program cannot use it again
unless *alloc returns a pointer to it again.

I covered all this in my earlier reply, which I recommend that you read.

.
> End of subject.


Dream on. Neither you nor I decide when a Usenet subject is closed.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      07-24-2007
Richard said:

> Mark Bluemel <(E-Mail Removed)> writes:
>

<snip>

>> If you're lucky the program will crash. If you're very unlucky, the
>> computer will grow arms, produce a knife and stab
>> you. <http://www.xkcd.com/293/>

>
> Oh god no. I wondered how long it would take before this became the
> new "in joke" for your "undefined" warnings.
>
> Look, it isn't funny because a toaster will NOT, under any
> circumstances, grow arms and stab you. It might electrocute you.


What you mean is that you don't think it's funny. But it is clear that
at least some others here do think it's funny. The Standard is silent
on the matter, so we are forced to resort to empirical observation and
statistical analysis. After some rather tedious number crunching, the
details of which need not concern us here, I can announce with 95%
confidence that the joke is at least 82% funny (+/- 0.5%).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Richard
Guest
Posts: n/a
 
      07-24-2007
Richard Heathfield <(E-Mail Removed)> writes:

> Richard said:
>
> <snip>
>
>> When he has called "free" the memory is then unavailable in any decent
>> program.

>
> When he has called free, the memory is available for reallocation. But
> you do have a point, in that it is perfectly reasonable to call it
> "unavailable" too, in the sense that the program cannot use it again
> unless *alloc returns a pointer to it again.
>
> I covered all this in my earlier reply, which I recommend that you
> read.


I did. And am incredulous that yet again you seek to show off and
obfuscate. Why do it? Your knowledge of C is very good - but there are
certain things best left unsaid.

>
> .
>> End of subject.

>
> Dream on. Neither you nor I decide when a Usenet subject is closed.


It is the end of subject with regard to a nOOb asking that particular
question.
--
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Leak or no leak ?? Richard Heathfield C Programming 4 07-10-2006 11:37 AM
Firefox memory leak ttfg Firefox 4 03-18-2005 06:32 PM
Firefox Memory Leak? Scott Moseman Firefox 2 02-09-2005 03:52 PM
Wireless Zero Configuration Memory Leak?? =?Utf-8?B?Umlja3NjaHVsdHox?= Wireless Networking 3 01-19-2005 11:26 PM
datagrid memory leak? =?Utf-8?B?Y3liZXJzdHJpa2U=?= ASP .Net 0 01-03-2005 10:17 PM



Advertisments