473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Will it leak the memory?

KG
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

Jul 24 '07 #1
16 1647
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

Jul 24 '07 #2
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.
Jul 24 '07 #3
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?
Jul 24 '07 #4
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
Jul 24 '07 #5
Richard Heathfield <rj*@see.sig.invalidwrites:
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.
Jul 24 '07 #6
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.

Jul 24 '07 #7
Richard Heathfield <rj*@see.sig.invalidwrites:
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.
--
Jul 24 '07 #8
Richard said:
Richard Heathfield <rj*@see.sig.invalidwrites:
>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
Jul 24 '07 #9
Richard said:
Richard Heathfield <rj*@see.sig.invalidwrites:
<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
Jul 24 '07 #10
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.
Jul 24 '07 #11
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: <mailto:Th*************@gmail.com>

Jul 24 '07 #12

"Richard" <rg****@gmail.comwrote in message
news:4x************@gmail.com...
Mark Bluemel <ma**********@pobox.comwrites:
>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.
Jul 24 '07 #13
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
Jul 24 '07 #14
Enzo Canuzzi <ca*****@gmx.netwrote:
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
Jul 25 '07 #15
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

Jul 25 '07 #16

"Mark Bluemel" <ma**********@pobox.comwrote in message
news:f8**********@aioe.org...
>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
Jul 25 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Debian User | last post by:
Hi, I'm trying to discover a memory leak on a program of mine. I've taken several approaches, but the leak still resists to appear. First of all, I've tried to use the garbage collector to...
8
by: ranjeet.gupta | last post by:
Dear All Is the Root Cause of the Memory corruption is the Memory leak, ?? suppose If in the code there is Memory leak, Do this may lead to the Memory Corruption while executing the program ? ...
4
by: Don Nell | last post by:
Hello Why is there a memory leak when this code is executed. for(;;) { ManagementScope scope = new ManagementScope(); scope.Options.Username="username"; scope.Options.Password="password";...
13
by: Boni | last post by:
I use 3-d party component. In this component I must pass a reference to my object. The problem is that this component has an ugly bug.When this component is disposed, it incorrectly don't delete...
23
by: James | last post by:
The following code will create memory leaks!!! using System; using System.Diagnostics; using System.Data; using System.Data.SqlClient; namespace MemoryLeak
0
by: nejucomo | last post by:
Hi folks, Quick Synopsis: A test script demonstrates a memory leak when I use pythonic extensions of my builtin types, but if I use the builtin types themselves there is no memory leak. ...
9
by: Joakim Hove | last post by:
Hello, I have the following code: foo_ptr * alloc_foo(int size, ...) { /* This function allocates an instance of the foo_ptr type, and returns a pointer to the newly allocated storage. */ }
20
by: gNash | last post by:
Hi all, void main() { char *fp; fp=malloc(26); strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ"); fp='\0'; free(fp); }
22
by: Peter | last post by:
I am using VS2008. I have a Windows Service application which creates Crystal Reports. This is a multi theaded application which can run several reports at one time. My problem - there is a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.