Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Will it leak the memory?

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

> 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%).


"In jokes" amongst a group of smart ass cognoscenti are not funny - they
are tedious to the extreme. And of that, I am 100% assured :-;
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      07-24-2007
Richard said:

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


The comp.lang.c newsgroup is a mirror. What you see is what you bring
with you.

> Why do it?


Precision, my dear chap - precision. Or at least the hope of it.

> Your knowledge of C is very good


Yes, I know.

> - but there are certain things best left unsaid.


Don't let that stop you from saying them. It hasn't yet.

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

> Richard Heathfield <(E-Mail Removed)> writes:
>


<snip>

>> 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%).

>
> "In jokes" amongst a group of smart ass cognoscenti are not funny -


I wouldn't know. I don't know any smart ass cognoscenti. Asses are not
renowned for their smartness or their cognizance. In-jokes, however,
can be very funny indeed for those who are "in".

(Normally I don't approve of in-crowds, because they are by nature
exclusive. But techie in-crowds are different - there are no barriers
to entry except one's own unwillingness to learn.)


> they are tedious to the extreme. And of that, I am 100% assured :-;


Well, as long as you're happy...

--
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
 
Enzo Canuzzi
Guest
Posts: n/a
 
      07-24-2007
> I wouldn't know. I don't know any smart ass cognoscenti. Asses are not
> renowned for their smartness or their cognizance.


So i am not sure about the cognizance part. With a broad definition of
cognizance and taken into acount that the asshole, which is a public
member of the ass class, has a dense nerve network of the sense of
touch. So as the ass takes cognizance of something going in or out, you
can define some sort of cognizance of the ass.
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      07-24-2007
Enzo Canuzzi wrote:
>
> > I wouldn't know. I don't know any smart ass cognoscenti. Asses are not
> > renowned for their smartness or their cognizance.

>
> So i am not sure about the cognizance part. With a broad definition of
> cognizance and taken into acount that the asshole, which is a public
> member of the ass class, has a dense nerve network of the sense of
> touch. So as the ass takes cognizance of something going in or out, you
> can define some sort of cognizance of the ass.


C doesn't have classes, let alone public/private members of such a
thing. Perhaps you want C++ instead?

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

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

"Richard" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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/>


I make gambling machines for a living, I can assure you we have the funniest
examples of undefined behaviour around.Well funny for the player, not the
programmer.
One example is that when a game started some memory was allocated with
malloc for a struct that has a function pointer in it which would be called
every second. The function would draw prizes (or 0) every second and store
them in static variables. Then when you leave the game and entered again the
struct would be allocated again etc. So when the allocation would fail
because of a memory leak somewhere else no new prizes would be generated and
the game would take the same prize every second, the ones last stored in the
static variables. So it could happen that a player would win a jackpot every
time he let the reels spin!
It's a player's dreeam to find bugs like that, but a game programmer's
nightmare.


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

^^^^^^^^
This is bad form, perhaps brought on by your
error of not including <stdlib.h>.
>
> strcpy(p,"TajMahal");
>
> p++;
>
> free(p);

^^^^^^^
This is an error. p does not contain a value returned from
malloc.
>
> }
>
> Does this program results in memory leak?


It results in any damn thing the implementation decides. If malloc
actually tries to free() space using the pointer p, which has a value
not returned from malloc, the most likely result is the complete
corruption of the memory management section.

And if free() did what some people naively hope it would, freeing some
space (of what size?) starting at one past the orignal value of p,
obviously at least one allocated char would not be freed.

> Please explain.


The attempt to use free() with a pointer with a value not returned from
malloc is a gross error. There is no correct answer to your question
other than to say that the code is hopelessly broken.

>
> Regards,
> Kiran Patil
>

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

[...]
>> 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.


Well then, I'll make sure to limit my future attempts at humor to
things that can plausibly happen in real life.

Let's see now ...

A guy walks into a bar. He orders a drink, drinks it, pays for it,
and leaves.

HAHAHAHAHAHAHAHAHA!!

The nasal demons metaphor has been explained here before, but if you
still don't get it, I'm sure someone would be willing to explain it to
you again.

Until then, consider this. Users often make assumptions about what a
program or system cannot possibly do. Such assumptions are frequently
wrong. This does not imply that a C program can make demons fly out
of your nose, or that at toaster can grow an arm and stab you, merely
that assumptions about what a system cannot possibly do are frequently
wrong. This has real-world consequences; for example, malware often
works by taking advantage of such assumptions.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Clever Monkey
Guest
Posts: n/a
 
      07-24-2007
Richard wrote:
> 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.
>

That is one sort of undefined behaviour. You can't guarantee that a
toaster will not grow arms and stab you. Sure, it's highly /unlikely/,
but this is why it is funny when it does happen.

Otherwise, we are just laughing at a a guy who was stabbed in the face.
This is not funny. What were you thinking?

Again: UB is UB. You can put any real parameters around UB. It is
still within the realm of all the possibilities that UB will result in
purple monkeys flying out of one's ass, which is the standard
measurement of undefined behaviour around here.

I think I'd rather take my chances with the toaster, but undefined
behaviour means we /can't make that choice/.
--
clvrmnky <(E-Mail Removed)>

Direct replies will be blacklisted. Replace "spamtrap" with my name to
contact me directly.
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      07-25-2007
Enzo Canuzzi <(E-Mail Removed)> wrote:

> > I wouldn't know. I don't know any smart ass cognoscenti. Asses are not
> > renowned for their smartness or their cognizance.

>
> So i am not sure about the cognizance part. With a broad definition of
> cognizance and taken into acount that the asshole,


You cut a hole in your ass? What has the poor animal done to deserve
such treatment?

> which is a public member of the ass class, has a dense nerve network
> of the sense of touch.


Well, I bet it does! _You_ would have sore nerves, too, if someone cut a
hole in you.

> So as the ass takes cognizance of something going in or out,


And now you're torturing the poor dear by taking things in and out of
it? That's torture, that is. I'm calling the RSPCA on you.

Richard
 
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