Does this code has memory leakage?

This is a discussion on Does this code has memory leakage? within the C++ Programming forums, part of the General Programming Boards category; Code: void foo() { char x[3]="XY"; cout<<x<<endl; y=x; } int main() { char * y; foo(); cout<<y<<endl; return 0; } ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Does this code has memory leakage?

    Code:
    void foo()
    {
      char x[3]="XY";
      cout<<x<<endl;
      y=x;
    }
    
    int main()
    {
        char * y; 
        foo();
        cout<<y<<endl;
        return 0;
    }

    Interesting thing is that y can be correctly printed, which means the allocated memory is not freed after foo();
    But I use valgrind to check the code and it reports no error:

    ==1529== Memcheck, a memory error detector.
    ==1529== Copyright (C) 2002-2005, and GNU GPL'd, by Julian Seward et al.
    ==1529== Using LibVEX rev 1471, a library for dynamic binary translation.
    ==1529== Copyright (C) 2004-2005, and GNU GPL'd, by OpenWorks LLP.
    ==1529== Using valgrind-3.1.0-Debian, a dynamic binary instrumentation framework.
    ==1529== Copyright (C) 2000-2005, and GNU GPL'd, by Julian Seward et al.
    ==1529== For more details, rerun with: -v
    ==1529==
    XY
    XY
    ==1529==
    ==1529== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 1)
    ==1529== malloc/free: in use at exit: 0 bytes in 0 blocks.
    ==1529== malloc/free: 0 allocs, 0 frees, 0 bytes allocated.
    ==1529== For counts of detected errors, rerun with: -v
    ==1529== No malloc'd blocks -- no leaks are possible.
    Last edited by meili100; 04-08-2008 at 05:27 PM.

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    "No."
    I hate real numbers.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Quote Originally Posted by foxman View Post
    "No."
    If "No" then why cout<<y<<endl can work?

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    If char* x = (char*)malloc(3); then I am sure its memory leaks, but does this code leaks memory?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The y in foo (which isn't declared, which means this program doesn't compile, which means you aren't giving us the code you're actually working with) is not the same as the y in main. x is a variable of automatic duration, which means it goes away--in as much as memory can go away, it's not as your computer slowly self-destructs--at the end of foo.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If there's no new or malloc() calls, how can there be a leak?
    y is just pointing to a random location in memory, so you're printing whatever it happens to point at.
    And as tabstop says, the code is invalid since it can't compile.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Quote Originally Posted by meili100 View Post
    Code:
    void foo()
    {
      char x[3]="XY";
      cout<<x<<endl;
      y=x;
    }
    
    int main()
    {
        char * y; 
        foo();
        cout<<y<<endl;
        return 0;
    }
    This code does not compile. I don't believe it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex and Shared Memory Segment Questions.
    By MadDog in forum Linux Programming
    Replies: 14
    Last Post: 06-20-2010, 04:04 AM
  2. Reallocating Memory
    By Booie2k1 in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 06:09 AM
  3. memory leak in the code?
    By George2 in forum C++ Programming
    Replies: 20
    Last Post: 01-13-2008, 05:50 AM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21