Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > will I get Memory leak..

Reply
Thread Tools

will I get Memory leak..

 
 
gNash
Guest
Posts: n/a
 
      11-14-2007
Hi all,

void main()
{
char *fp;
fp=malloc(26);
strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
fp[10]='\0';
free(fp);
}

Please refer the program for my questions..

1. Can any one tell me if would i assign '\0' at middle of
dynamically assigned memory area will i get memory leak.. ??
2 . Will "free()" delete all the memory which allocated by
dynamically even NULL values has been added in middle of that.??
3. How let i know that will i get memory leak in a program?? any
compiler option are there ?? Can i use splint tool ?

Thanks,
Ganesh

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      11-14-2007
gNash <(E-Mail Removed)> wrote:

> void main()


This is wrong. It should be

int main()

or by preference,

int main(void)

> {
> char *fp;
> fp=malloc(26);
> strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");


This is wrong. You copy 27 characters (one whole alphabet, plus the
terminating null character) into space which can only hold 26 bytes.

> fp[10]='\0';
> free(fp);


These, however, are both absolutely fine, and should cause no problems
whatsoever, once you fix the bugs in the lines above them.

> }


Richard
 
Reply With Quote
 
 
 
 
Spiros Bousbouras
Guest
Posts: n/a
 
      11-14-2007
On Nov 14, 12:31 pm, gNash <(E-Mail Removed)> wrote:
> Hi all,
>
> void main()
> {
> char *fp;
> fp=malloc(26);
> strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
> fp[10]='\0';
> free(fp);
> }
>
> Please refer the program for my questions..
>
> 1. Can any one tell me if would i assign '\0' at middle of
> dynamically assigned memory area will i get memory leak.. ??
> 2 . Will "free()" delete all the memory which allocated by
> dynamically even NULL values has been added in middle of that.??
> 3. How let i know that will i get memory leak in a program?? any
> compiler option are there ?? Can i use splint tool ?


Richard Bos has answered your main questions. I will simply
add that the character with value 0 is referred to as NUL not
NULL. For detecting memory leaks valgrind has a good reputation.

 
Reply With Quote
 
gNash
Guest
Posts: n/a
 
      11-14-2007
On Nov 14, 5:34 pm, (E-Mail Removed) (Richard Bos) wrote:
> gNash <(E-Mail Removed)> wrote:
> > void main()

>
> This is wrong. It should be
>
> int main()
>
> or by preference,
>
> int main(void)
>
> > {
> > char *fp;
> > fp=malloc(26);
> > strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");

>
> This is wrong. You copy 27 characters (one whole alphabet, plus the
> terminating null character) into space which can only hold 26 bytes.
>
> > fp[10]='\0';
> > free(fp);

>
> These, however, are both absolutely fine, and should cause no problems
> whatsoever, once you fix the bugs in the lines above them.
>
> > }

>
> Richard



Hi Richard..

the int main(void) and malloc(26+1) it is not my doubt my doubts
are posted clearly.. Please reply for it.

Thankingyou,

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-14-2007
gNash wrote:
> On Nov 14, 5:34 pm, (E-Mail Removed) (Richard Bos) wrote:
>> gNash <(E-Mail Removed)> wrote:
>>> void main()

>> This is wrong. It should be
>>
>> int main()
>>
>> or by preference,
>>
>> int main(void)
>>
>>> {
>>> char *fp;
>>> fp=malloc(26);
>>> strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");

>> This is wrong. You copy 27 characters (one whole alphabet, plus the
>> terminating null character) into space which can only hold 26 bytes.
>>
>>> fp[10]='\0';
>>> free(fp);

>> These, however, are both absolutely fine, and should cause no problems
>> whatsoever, once you fix the bugs in the lines above them.
>>
>>> }

>> Richard

>
>
> Hi Richard..
>
> the int main(void) and malloc(26+1) it is not my doubt my doubts
> are posted clearly.. Please reply for it.


He did. He justed added extra advice which you clearly need, since you
weren't following it.
 
Reply With Quote
 
gNash
Guest
Posts: n/a
 
      11-14-2007
On Nov 14, 5:50 pm, James Kuyper <(E-Mail Removed)> wrote:
> gNash wrote:
> > On Nov 14, 5:34 pm, (E-Mail Removed) (Richard Bos) wrote:
> >> gNash <(E-Mail Removed)> wrote:
> >>> void main()
> >> This is wrong. It should be

>
> >> int main()

>
> >> or by preference,

>
> >> int main(void)

>
> >>> {
> >>> char *fp;
> >>> fp=malloc(26);
> >>> strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
> >> This is wrong. You copy 27 characters (one whole alphabet, plus the
> >> terminating null character) into space which can only hold 26 bytes.

>
> >>> fp[10]='\0';
> >>> free(fp);
> >> These, however, are both absolutely fine, and should cause no problems
> >> whatsoever, once you fix the bugs in the lines above them.

>
> >>> }
> >> Richard

>
> > Hi Richard..

>
> > the int main(void) and malloc(26+1) it is not my doubt my doubts
> > are posted clearly.. Please reply for it.

>
> He did. He justed added extra advice which you clearly need, since you
> weren't following it.



Thanks for advice.. but i have little confusion when i add '\0' and
free() will clear whole memory since '\0' representing end of array.
am i right?




 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-14-2007
gNash <(E-Mail Removed)> writes:

> void main()
> {
> char *fp;
> fp=malloc(26);
> strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");


You should get into the habit now or always checking the return from
malloc. Also, 26 is not enough for this string. You need space for
27 bytes.

> fp[10]='\0';
> free(fp);
> }
>
> Please refer the program for my questions..
>
> 1. Can any one tell me if would i assign '\0' at middle of
> dynamically assigned memory area will i get memory leak.. ??


No.

> 2 . Will "free()" delete all the memory which allocated by
> dynamically even NULL values has been added in middle of that.??


Yes. BTW, NULL is not the same as '\0'. That is a null byte.

> 3. How let i know that will i get memory leak in a program?? any
> compiler option are there ?? Can i use splint tool ?


There is an excellent tool called "valgrind" that can do this (and
more) to help you find memory allocation errors. splint (and friends)
can tell you about possible portability issues and about errors that
can be detected without running your program.

--
Ben.
 
Reply With Quote
 
gNash
Guest
Posts: n/a
 
      11-14-2007
On Nov 14, 5:59 pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> gNash <(E-Mail Removed)> writes:
> > void main()
> > {
> > char *fp;
> > fp=malloc(26);
> > strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");

>
> You should get into the habit now or always checking the return from
> malloc. Also, 26 is not enough for this string. You need space for
> 27 bytes.
>
> > fp[10]='\0';
> > free(fp);
> > }

>
> > Please refer the program for my questions..

>
> > 1. Can any one tell me if would i assign '\0' at middle of
> > dynamically assigned memory area will i get memory leak.. ??

>
> No.
>
> > 2 . Will "free()" delete all the memory which allocated by
> > dynamically even NULL values has been added in middle of that.??

>
> Yes. BTW, NULL is not the same as '\0'. That is a null byte.
>
> > 3. How let i know that will i get memory leak in a program?? any
> > compiler option are there ?? Can i use splint tool ?

>
> There is an excellent tool called "valgrind" that can do this (and
> more) to help you find memory allocation errors. splint (and friends)
> can tell you about possible portability issues and about errors that
> can be detected without running your program.
>
> --
> Ben.



Thank you all.. i am clear..

Thank you again..
Ganesh.

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-14-2007
I strongly recommend re-reading Richard Bos' response, but from your
comments about it I thought you might benefit from a response that more
directly addresses your questions.

gNash wrote:
> Hi all,
>
> void main()
> {
> char *fp;
> fp=malloc(26);
> strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");
> fp[10]='\0';
> free(fp);
> }
>
> Please refer the program for my questions..
>
> 1. Can any one tell me if would i assign '\0' at middle of
> dynamically assigned memory area will i get memory leak.. ??


Writing a null character into dynamically allocated memory will not, in
itself, cause a memory leak.

The problem with your code, on the other hand, is that your call to
strcpy() writes a null character to fp[26], which is one position past
the end of the allocated memory. As a result of that error, the behavior
of your entire program is undefined, which means that anything could go
wrong A memory leak is very definitely a possibility from making that
kind of mistake, but most of the other possible consequences of that
mistake are much worse than memory leaks.

> 2 . Will "free()" delete all the memory which allocated by
> dynamically even NULL values has been added in middle of that.??


Your code wrote one null value at a position one past the end of the
allocated memory, and a second null character at the beginning of the
allocation. It didn't write any null characters into the middle of the
allocation. Calling free() does not delete memory.

Writing null characters into the allocated memory has no affect on the
behavior of free(), regardless of where you write them, so long as it is
inside the allocation. Because of your mistake, the call to free() could
do anything, including sending insulting e-mail to your mother. However,
if you hadn't written a null character one past the end of the allocated
memory, the behavior of free() would have been to deallocate the memory,
making it available for further allocation.

> 3. How let i know that will i get memory leak in a program?? any
> compiler option are there ?? Can i use splint tool ?


Memory leaks occur at run time, not at compile time. Compilers can't
catch memory leaks, though some of the better ones can catch some of the
mistakes that result in memory leaks.

There are compilers that provide options that provide run-time help with
leak detection. There are debugging versions of the malloc() family of
functions. There are tools which run your program inside an environment
that allows you to monitor memory allocations and detect links. However,
in order to recommend an appropriate tool, we need to know what compiler
and operating system you're using.
 
Reply With Quote
 
gNash
Guest
Posts: n/a
 
      11-14-2007
On Nov 14, 5:59 pm, Ben Bacarisse <(E-Mail Removed)> wrote:
> gNash <(E-Mail Removed)> writes:
> > void main()
> > {
> > char *fp;
> > fp=malloc(26);
> > strcpy(fp,"ABCDEFGHIJKLMNOPQRSTUVWXYZ");

>
> You should get into the habit now or always checking the return from
> malloc. Also, 26 is not enough for this string. You need space for
> 27 bytes.
>
> > fp[10]='\0';
> > free(fp);
> > }

>
> > Please refer the program for my questions..

>
> > 1. Can any one tell me if would i assign '\0' at middle of
> > dynamically assigned memory area will i get memory leak.. ??

>
> No.
>
> > 2 . Will "free()" delete all the memory which allocated by
> > dynamically even NULL values has been added in middle of that.??

>
> Yes. BTW, NULL is not the same as '\0'. That is a null byte.
>
> > 3. How let i know that will i get memory leak in a program?? any
> > compiler option are there ?? Can i use splint tool ?

>
> There is an excellent tool called "valgrind" that can do this (and
> more) to help you find memory allocation errors. splint (and friends)
> can tell you about possible portability issues and about errors that
> can be detected without running your program.
>
> --
> Ben.



Could you any one please tell me how is free() is working?


 
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
Re: Why does this memory intensive C++ program get poor memory accessspeed? Ian Collins C++ 1 03-28-2010 10:51 PM
How do I get an out-of-memory error memory usage dump? Todd Java 4 09-05-2007 03:08 PM
Differences between Sony Memory Stick & memory Stick Pro vs Memory Stick Duo? zxcvar Digital Photography 3 11-28-2004 10:48 PM
Memory Control - Can you get the memory usage of the interpreter? Ian Python 3 07-10-2004 09:16 PM



Advertisments