Question about Memory Leaks with *char

This is a discussion on Question about Memory Leaks with *char within the C++ Programming forums, part of the General Programming Boards category; I've been wondering about memory leaks, and how they are created. Especially reguarding *char variables. First a question, in what ...

  1. #1
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68

    Question about Memory Leaks with *char

    I've been wondering about memory leaks, and how they are created. Especially reguarding *char variables. First a question, in what cases would you need to delete memory in order to keep it from leaking?


    I've been coding something, and I'm curious if it leaks, and why if it does. Suppose I had a global pointer variable. Within my main, I allowcated an array of size 50 for this g_pointer. Then a function has another char pointer variable. It is allowcated an array of 50 within the function. In the function the pointers swap. The code looks like this.

    Code:
    char *g_ch1;
    
    void foo() {
         char *swap;
         char *ch2 = new char[50];
    
          ch2[0] = 'b';
          ch2[1] = 'l';
          ch2[2] = 'e';
          ch2[3] = 'h';
    
          swap = g_ch1;
          g_ch = ch2;
          ch2 = swap;
    }
    
    void main() {
        g_ch = new char[50];
        foo();
       cout<<g_ch1<<endl;
    }
    naturally you'll see "bleh" on the screen. But I'm curious about *ch2. What happens to it? Would it be safe to delete ch2 at the very end of the function because it's not used in the program? Is it not nessary?
    Last edited by Diamonds; 12-15-2002 at 11:58 PM.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You do have a memory leek. Whenever you use new you must delete your memory when you are done with it. Likewise, if you malloc you must free your memory. Memory leaking on a small scale like this is technically no big deal. However, it can be a habit. Windows should discard memory when your application concludes but the issue that needs to be dealt with reguarding memory leaks are programs that leak large amounts of memory or programs that run for a very long time with a memory leak. Or worse yet a combo of the two. If your program is taking up resources without freeing them, that is one less resource that your computer has to work with. And the only way to get back leaked memory is to close the program with the leak.

  3. #3
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68

    Talking great

    great thnx, i knew something was fishy about this mess.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    One more thing is fishy. You should have a return at the end of main.

  5. #5
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68

    what now?

    how can i return when I have no int?
    muhahahaha

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: what now?

    Originally posted by Diamonds
    how can i return when I have no int?
    muhahahaha
    Then you make main's return type int not void. void main is not and never was valid C++ nor C.

  7. #7
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    hehe, i know, joke (notice the 'edit' of the first post)

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    *wipes forehead*

    good thing Salem didn't see that!

  9. #9
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68
    lol
    no joke

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Watch out though, I think Salem senses every void main that goes onto the boards.

  11. #11
    Registered User Diamonds's Avatar
    Join Date
    Oct 2002
    Posts
    68

    preparing for war

    must prepare

    *puts on flame retardent clothing*
    *boots up firewall*
    *grabs fire exinguisher*

    sp?

  12. #12
    Shadow12345
    Guest
    I don't see why you would want to have
    char * x = new char[whatever];

    I only use new when i absolutely need it, and an array is basically just a pointer anyway but you don't have to call delete on it, and in this scenario you aren't asking the user for a specific size of the array so memory doesn't absolutely have to be allocated dynamically.

    just my thoughts, you can all tell me to die and I'll happily go do it

    and return something god dammit

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer memory question
    By Edo in forum C++ Programming
    Replies: 5
    Last Post: 01-21-2009, 02:36 AM
  2. Memory question
    By John_L in forum Tech Board
    Replies: 8
    Last Post: 06-02-2008, 10:06 PM
  3. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM

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