pointers and dynamic memory allocation

Hello all, I have a question about dynamic memory allocation (if I'm in the wrong forum please let me know!). I wrote a small practice program to try to understand why the dynamic allocation operator (as my book calls it) 'delete' is necessary. This program just accepts a string and copies it to the 'ast[y]' array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<iostream>
#include<iomanip>
#include<stdlib.h>
using namespace std;
int main()
  {
  int x, y, z, yy;
  string a;
  for (yy=0; ; yy++)
    {
    cout<<"\nenter string: "; cin>>a;
    y=a.size();
    char ast[y];
    a.copy(ast,y,0);
    for (z=0; z<=y+10; z++)
      {
      cout<<setw(10)<<"ast["<<z<<"]="<<ast[z];
      }
    cout<<endl;
    }
  return 0;
  }


Every time the loop is executed, the array ast[y] is redefined with a new number of elements (as far as I can see) according to a test run:
enter string: uuuuuuuu
ast[0]=u      ast[1]=u      ast[2]=u      ast[3]=u      ast[4]=u      ast[5]=u
ast[6]=u      ast[7]=u      ast[8]=�      ast[9]=�      ast[10]=�      ast[11]=�
ast[12]=�      ast[13]=�      ast[14]=      ast[15]      ast[16]=|      ast[17]=�
ast[18]=

enter string: rrrr
ast[0]=r      ast[1]=r      ast[2]=r      ast[3]=r      ast[4]=@      ast[5]=�
ast[6]=      ast[7]      ast[8]=�      ast[9]=�      ast[10]=�      ast[11]=�
ast[12]=�      ast[13]=�      ast[14]=

enter string: jjjjjjjjjjjjj
ast[0]=j      ast[1]=j      ast[2]=j      ast[3]=j      ast[4]=j      ast[5]=j
ast[6]=j      ast[7]=j      ast[8]=j      ast[9]=j      ast[10]=j      ast[11]=j
ast[12]=j      ast[13]=�      ast[14]=      ast[15]      ast[16]=|      ast[17]=�
ast[18]=�      ast[19]=�      ast[20]=|      ast[21]=�      ast[22]=�      ast[23]=�

enter string: ee 
ast[0]=e      ast[1]=e      ast[2]=      ast[3]=      ast[4]=@      ast[5]=�
ast[6]=      ast[7]      ast[8]=�      ast[9]=�      ast[10]=�      ast[11]=�
ast[12]=�


I purposedly made so that the program displays more elements than were defined for the array.

My question is, if it is possible to redefine a variable during, at least this program's run, what is the advantage in using pointers and the allocation operators 'new' and 'delete' to create and delete a new variable/array/object.

Also, on another practice program, I deleted a variable but could still access the data that was in that variable before using 'delete', so how exactly does 'delete' work??

Many thanks in advance
Last edited on
This is not standard c++ code -> char ast[y]; (because y is not a constant). Maybe your compiler allows it but not all compilers do, so it's not a good idea. Use char * ast = new char[y]; instead.

EDIT:

rdangelo wrote:
I deleted a variable but could still access the data that was in that variable before using 'delete', so how exactly does 'delete' work??

This is a very rough description but think of new and delete operators as markers of memory areas. When you reserve memory with new you effectively tell your program/OS that you want that memory to be modified only by you. When you delete that memory you effectively remove that restriction. Of course you can access that address and view its contents but nobody knows what is stored there. Maybe it's as you left it, maybe it's not...
Last edited on
Thanks for the reply! now it became crystal clear how 'new' and 'delete' works, thanks!

Now, going back to the first question, seems like your answer hits the heart of it, but I'm still on my way to fully understand. From my understanding, at compile time, the complier does the same as 'new' and 'delete', but the restriction remains for the lifetime of the vairable/program, and we are not allowed to remove that restriction manually (with 'delete') during the program's run. But the program that I wrote seems to do just that (I'm using Ubuntu Lynux 9.04 and gedit)! So from your answer, that's not normal because the compiler cannot reserve a fixed amount of storage space for ast[y] before the program is run (y is not a constant!). So I'm not sure how my program is compiled, but from what I've read so far, the advantage of using pointers is that it allows a programmer to "manage" computer memory manually during a program's run by reserving memory spaces and removing them at will.
I hope that's correct!
It is correct.

Take a look at this to learn more about variable length arrays:
http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC75

Notice that a variable length array is still stored on the stack, while new/malloc reserve memory on the heap.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

int main()
{
    int static_array[10];
    int * int_pointer=new int[10];

    unsigned int size;

    cout << "enter size: ";
    cin >> size;

    int var_len_array[size];

    cout << "address of static_array: ";
    cout << static_array << endl;

    cout << "address of var_len_array: ";
    cout << var_len_array << endl;

    cout << "address pointed at by int_pointer: ";
    cout << int_pointer << endl;

    delete[] int_pointer;
    cin.get();
    return 0;
}
Why not use a std::vector<char> or std::deque<char> and avoid the worry of using new and delete for your dynamic array needs? You are already using std::string so you might as well take the next step and learn to use the other containers. vector and deque are the first two on the list of container references.
http://cplusplus.com/reference/stl/
That was a lot of new material, I'm still going to look at all of it more carefully (and maybe even post about it!).
I see that m4ster's program is like mine in that it defines a variable length array during the program's run. The terms 'stack' and 'heap' are still relatively new to me so I have some more reading to do on those, and maybe even find out how my computer is compiling a variable length array.
'vector' and 'deque' are actually new terms to me, my book doesn't seem to go that far so I'll be doing some advanced reading soon.

Thanks again!
It is correct.

Take a look at this to learn more about variable length arrays:
http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC75
I'm afraid not. That link describes C99, however C and C++ are not compatible in this regard. The code you pasted is illegal in C++ - it did not compile on 2 standards conforming compilers which I tried it on.
I gather that you didn't read the whole topic. Because if you did, you would see that I already mentioned that in my first reply. The link is a list of the gcc extensions to the language.
Topic archived. No new replies allowed.