will I get Memory leak..

Discussion in 'C Programming' started by gNash, Nov 14, 2007.

  1. gNash

    gNash Guest

    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
     
    gNash, Nov 14, 2007
    #1
    1. Advertising

  2. gNash

    Richard Bos Guest

    gNash <> 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
     
    Richard Bos, Nov 14, 2007
    #2
    1. Advertising

  3. On Nov 14, 12:31 pm, 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.. ??
    > 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.
     
    Spiros Bousbouras, Nov 14, 2007
    #3
  4. gNash

    gNash Guest

    On Nov 14, 5:34 pm, (Richard Bos) wrote:
    > gNash <> 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,
     
    gNash, Nov 14, 2007
    #4
  5. gNash

    James Kuyper Guest

    gNash wrote:
    > On Nov 14, 5:34 pm, (Richard Bos) wrote:
    >> gNash <> 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.
     
    James Kuyper, Nov 14, 2007
    #5
  6. gNash

    gNash Guest

    On Nov 14, 5:50 pm, James Kuyper <> wrote:
    > gNash wrote:
    > > On Nov 14, 5:34 pm, (Richard Bos) wrote:
    > >> gNash <> 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?
     
    gNash, Nov 14, 2007
    #6
  7. gNash <> 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.
     
    Ben Bacarisse, Nov 14, 2007
    #7
  8. gNash

    gNash Guest

    On Nov 14, 5:59 pm, Ben Bacarisse <> wrote:
    > gNash <> 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.
     
    gNash, Nov 14, 2007
    #8
  9. gNash

    James Kuyper Guest

    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.
     
    James Kuyper, Nov 14, 2007
    #9
  10. gNash

    gNash Guest

    On Nov 14, 5:59 pm, Ben Bacarisse <> wrote:
    > gNash <> 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?
     
    gNash, Nov 14, 2007
    #10
  11. gNash

    James Kuyper Guest

    gNash wrote:
    ....
    > 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?


    No. You are confusing three different things: strings, arrays, and
    dynamic memory allocation.
    Many C library functions read or write null-terminated strings. The
    free() function is NOT one of them.

    Arrays have a length that can be determined at the point of declaration.
    Writing a null character to an array does not change its size, it merely
    affects how much of that array will be read by routines that take
    strings as input.

    Dynamic memory allocations have a size that is determined solely by the
    call to malloc(), calloc(), or realloc() that allocated the memory. The
    size is not changed by what you write into that memory, not even if what
    you write is a null character. The free() function deallocates the
    entire amount of memory allocated.
     
    James Kuyper, Nov 14, 2007
    #11
  12. gNash <> writes:

    > On Nov 14, 5:59 pm, Ben Bacarisse <> wrote:
    >> gNash <> writes:

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

    <snip>

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


    Only by looking at the code for every implementation! It just works
    (if used correctly). If you get a copy of K&R ("The C Programming
    Language" by Kernighan and Ritchie) you will find an explanation of
    one way to implement malloc and free as well as having one of the best
    book on C there is.

    --
    Ben.
     
    Ben Bacarisse, Nov 14, 2007
    #12
  13. gNash

    Richard Bos Guest

    gNash <> wrote:

    > On Nov 14, 5:34 pm, (Richard Bos) wrote:
    > > gNash <> 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.
    > >
    > > > }


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


    I did. Learn to read. And learn not to be so bleedin' demanding. You
    don't get to order me around unless you pay me a damn sight more than
    you can afford.

    Richard
     
    Richard Bos, Nov 14, 2007
    #13
  14. gNash

    gNash Guest

    On Nov 14, 6:22 pm, James Kuyper <> wrote:
    > gNash wrote:
    >
    > ...
    >
    > > 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?

    >
    > No. You are confusing three different things: strings, arrays, and
    > dynamic memory allocation.
    > Many C library functions read or write null-terminated strings. The
    > free() function is NOT one of them.
    >
    > Arrays have a length that can be determined at the point of declaration.
    > Writing a null character to an array does not change its size, it merely
    > affects how much of that array will be read by routines that take
    > strings as input.
    >
    > Dynamic memory allocations have a size that is determined solely by the
    > call to malloc(), calloc(), or realloc() that allocated the memory. The
    > size is not changed by what you write into that memory, not even if what
    > you write is a null character. The free() function deallocates the
    > entire amount of memory allocated.



    What will happen if would i say free(str+2)?? will i get leak ?
     
    gNash, Nov 14, 2007
    #14
  15. gNash

    Mark Bluemel Guest

    gNash wrote:

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


    If passed a pointer to a piece of memory which was allocated with
    malloc(), calloc() or realloc(), which has not already been freed by a
    call to free() or realloc, free() returns that memory to the pool which
    malloc() etc allocate from.

    if passed a NULL pointer, free() does nothing.

    If passed any other pointer, the behaviour is undefined.

    If you want to know how free() accomplishes this, you are asking about
    the implementation details, which differ - you'd have to refer to the
    source code for a specific malloc()/calloc()/realloc()/free()
    implementation.
     
    Mark Bluemel, Nov 14, 2007
    #15
  16. gNash

    Mark Bluemel Guest

    gNash wrote:
    >
    > What will happen if would i say free(str+2)?? will i get leak ?
    >

    Read the specification for free() - if you pass it a pointer which isn't
    the starting address of a malloc()/calloc()/realloc() allocated area,
    the behaviour is undefined.

    free() is an "all or nothing" operation - you can't free() all but 2
    bytes of a malloc()ed space.
     
    Mark Bluemel, Nov 14, 2007
    #16
  17. gNash

    Richard Guest

    Ben Bacarisse <> writes:

    > gNash <> writes:
    >
    >> On Nov 14, 5:59 pm, Ben Bacarisse <> wrote:
    >>> gNash <> writes:

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

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

    >
    > Only by looking at the code for every implementation! It just works
    > (if used correctly). If you get a copy of K&R ("The C Programming
    > Language" by Kernighan and Ritchie) you will find an explanation of
    > one way to implement malloc and free as well as having one of the best
    > book on C there is.


    slight pedant point : the second edition!

    http://www.amazon.com/C-Programming-Language-2nd/dp/0131103628
     
    Richard, Nov 14, 2007
    #17
  18. gNash

    gNash Guest

    On Nov 14, 6:27 pm, (Richard Bos) wrote:
    > gNash <> wrote:
    > > On Nov 14, 5:34 pm, (Richard Bos) wrote:
    > > > gNash <> 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.

    >
    > > > > }

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

    >
    > I did. Learn to read. And learn not to be so bleedin' demanding. You
    > don't get to order me around unless you pay me a damn sight more than
    > you can afford.
    >
    > Richard
     
    gNash, Nov 14, 2007
    #18
  19. gNash

    gNash Guest

    On Nov 14, 6:27 pm, (Richard Bos) wrote:
    > gNash <> wrote:
    > > On Nov 14, 5:34 pm, (Richard Bos) wrote:
    > > > gNash <> 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.

    >
    > > > > }

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

    >
    > I did. Learn to read. And learn not to be so bleedin' demanding. You
    > don't get to order me around unless you pay me a damn sight more than
    > you can afford.
    >
    > Richard


    It was request... i was not order.. sorry if do you feel so..

    Gnash.
     
    gNash, Nov 14, 2007
    #19
  20. Mark Bluemel wrote:
    > gNash wrote:
    >>
    >> What will happen if would i say free(str+2)?? will i get leak ?
    >>

    > Read the specification for free() - if you pass it a pointer which isn't
    > the starting address of a malloc()/calloc()/realloc() allocated area,
    > the behaviour is undefined.
    >
    > free() is an "all or nothing" operation - you can't free() all but 2
    > bytes of a malloc()ed space.


    -- but if this is what you want to do, look up the realloc() function.

    --
    Philip Potter pgp <at> doc.ic.ac.uk
     
    Philip Potter, Nov 14, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prashant
    Replies:
    2
    Views:
    315
    jacob navia
    Jun 30, 2004
  2. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    723
    Eric Sosman
    Mar 22, 2005
  3. Richard Heathfield

    Leak or no leak ??

    Richard Heathfield, Jul 10, 2006, in forum: C Programming
    Replies:
    4
    Views:
    365
    Richard Heathfield
    Jul 10, 2006
  4. cham
    Replies:
    5
    Views:
    779
  5. Mark Probert
    Replies:
    4
    Views:
    341
    Mark Probert
    Feb 9, 2005
Loading...

Share This Page