Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Will it leak the memory?

 
 
Chris Dollin
Guest
Posts: n/a
 
      07-25-2007
Mark Bluemel wrote:

> 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?


I really meant it.

Now, of course, I see it as nonsense. What I intended to say was more
like "If you omit the `p++` /and the free/", then ...", but a demon
flew out of my nose and erased the "and the free" bit.

Thanks for catching my idiocy, Mark.

--
Chris "idiot before 7, fine after 11" Dollin

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

 
Reply With Quote
 
 
 
 
Serve Lau
Guest
Posts: n/a
 
      07-25-2007

"Mark Bluemel" <(E-Mail Removed)> wrote in message
news:f84d5h$o5s$(E-Mail Removed)...
>> 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/>


I find this stuff highly offensive, we need a picture of a dildo coming out
of the machine doing naughty things


 
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