[BEGINNER] dynamic memory dumb question

This is a discussion on [BEGINNER] dynamic memory dumb question within the C++ Programming forums, part of the General Programming Boards category; Hello forum users, I'm just a beginner learning all these things about pointers, dynamic memory etc.. The point is: I'm ...

  1. #1
    Registered User arthurhess's Avatar
    Join Date
    Nov 2009
    Posts
    4

    Unhappy [BEGINNER] dynamic memory dumb question

    Hello forum users,

    I'm just a beginner learning all these things about pointers, dynamic memory etc..

    The point is: I'm writing some code to get pratice with the language. I'm now learning about Dynamic memory (actually I'm learning about everything, that's all new stuff to me)

    Ok, guilty, I talk too much. I'll show you my piece of code:

    Code:
    int* bfThis(int *nArray, int nSize)
    {
         int *newArray = new int[nSize];
         nArray += nSize - 1;
         for (int nCounter = nSize - 1, i = 0; nCounter >= 0; nCounter--, nArray--, i++) *(newArray+i) = *nArray;
         return newArray;
    }
         
         
    int main()
    {
        int nTest[] = {11,10,9,8,7,6,5,4,3,2,1,0};
        int* hehe = bfThis(nTest, 12);
        for (int i = 0; i < 12; i++) cout << hehe[i];
        cout << endl;
        delete[] hehe;
        for (int i = 0; i < 12; i++) cout << hehe[i];
        cout << endl;
        system("pause");
        return 0;
    }
    Please, what I want to know is: am I doing right the new&delete thing? I tought that setting free the memory space and initializing some strings the '0 1 2 3 4...' should disappear...

    So, please, can someone tell me what I'm doing so wrong?

    Thank you very much for the attention, readers. And sorry for my bad english, I'm a foreign speaker...

    Arthur

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Until something overwrites those memory locations, the data will stay as it was prior to the delete. The delete command just says that you are done using that portion of the heap and that some other memory allocation request further down the road is free to claim it and use it as necessary. The delete does not perform any wiping/clearing/erasing of memory.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User arthurhess's Avatar
    Join Date
    Nov 2009
    Posts
    4
    wow So that means I'm doing it right?!

    Thank you for your answer, pelicanpie (whatever it tastes like)

    --EDIT: thanks hk_mp5kpdw!

    Arthur

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. Newbie question: pointers, other program's memory
    By xxxxme in forum C++ Programming
    Replies: 23
    Last Post: 11-25-2006, 12:00 PM
  3. a Question regarding Dynamic memory.
    By gftmc in forum C++ Programming
    Replies: 7
    Last Post: 10-31-2006, 03:15 PM
  4. Dynamic memory confusion
    By ripper079 in forum C++ Programming
    Replies: 5
    Last Post: 11-04-2002, 05: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