Results 1 to 4 of 4

Thread: Question about dynamically allocated arrays in C++

  1. #1
    Join Date
    May 2005
    Location
    Seattle, WA
    Beans
    26
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Question about dynamically allocated arrays in C++

    One thing that I thought was interesting was when my CS professor was showing us how to use GDB, where he exploits the fact that there is always a "0" right outside the bounds of a dynamically allocated integer array in order to prove a point in debugging a program. For instance:

    int *array = new int [5];
    int a = array[5];

    And now, a will be equal to 0. My question to you guys is, why is this so in every single execution of the program? I just thought it was extremely peculiar behavior that happens too often for it to be a coincidence...

  2. #2
    Join Date
    Apr 2005
    Location
    Lexington, KY
    Beans
    117

    Re: Question about dynamically allocated arrays in C++

    bren@itu:~/tmp$ cat test2.cpp
    #include <iostream>
    using namespace std;

    int main(void)
    {
    int *array = new int[5];
    int a = array[5];
    cout << a << endl;
    }

    bren@itu:~/tmp$ g++ test2.cpp
    bren@itu:~/tmp$ ./a.out
    135145
    bren@itu:~/tmp$
    random junk here so ubuntuforums doesn't reject my post

  3. #3
    Join Date
    May 2005
    Location
    Seattle, WA
    Beans
    26
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Question about dynamically allocated arrays in C++

    Weird... I actually get the same number on my end as well. I also tried array[6] and it gives 0 every time the program is executed. Same with new int[6] and array[6], it will produce 0 every execution. Why is that? Is it because it allocates the same portion of memory from the heap in every execution?

  4. #4
    Join Date
    Apr 2005
    Location
    Lexington, KY
    Beans
    117

    Re: Question about dynamically allocated arrays in C++

    Quote Originally Posted by Merc248
    Weird... I actually get the same number on my end as well. I also tried array[6] and it gives 0 every time the program is executed. Same with new int[6] and array[6], it will produce 0 every execution. Why is that? Is it because it allocates the same portion of memory from the heap in every execution?
    I think you're right that it takes the same memory from the heap each time. I guess you could make a for loop and repeatedly create new memory without freeing it to test..

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •