2

Is it possible to mimic the behavior of dynamic allocation using the following code. For example we do not know the exact number of the integer that are stored in a file and we are going to read the file and then store it in an array called Hello.

int x;
int n=0;
ifstream input("a.dat");
while (!input.eof())
{
    input >> x;
    n++;
}
input.close();

int Hello[n];
cout << "n= " << n << endl;

int i=0;
while (!input.eof())
{
    input >> Hello[i];
    i++;
}
ollo
  • 24,797
  • 14
  • 106
  • 155
  • I suggest using a vector initialized with `istream_iterator`s. – chris Apr 28 '13 at 04:18
  • possible duplicate of [How to dynamically allocate memory space for a string and get that string from user?](http://stackoverflow.com/questions/8164000/how-to-dynamically-allocate-memory-space-for-a-string-and-get-that-string-from-u) – joce Apr 28 '13 at 04:48

4 Answers4

2

Is it possible to mimic the behavior of dynamic allocation using the following code.

No, the major difference is that the array in your program is stored on stack, whereas all dynamic memory allocations takes place on heap.

What you are exactly doing, in your code is using the VLA feature of C99 standard of C in C++. Compiling with the -pedantic option in g++ compiler will reveal this. Since it is not directly supported by c++, and it is a implementation-specific language extension, its not such a good idea to use it, if you aim to write portable code.

VLA's use alloca() , to allocate memory on stack at runtime, and the disadvantages of such a tecnnique are discussed here.

Further more, VLA's allocate memory on stack during runtime, and if the value exceeds the range, the program simply crashes, while it is ok to quickly create a few bytes of array using VLA's , creating uncertain amounts of large memory may not be safe, and it is best to handle it using dynamic memory allocation.

Community
  • 1
  • 1
Barath Ravikumar
  • 5,658
  • 3
  • 23
  • 39
1
int Hello[n];

is NOT dynamic allocation. It is required that n is a compile time constant if you want to declare Hello in this way.

try:

int* Hello = new int[n];

and don't forget to release the memory when you are done using it:

delete[] Hello;
taocp
  • 23,276
  • 10
  • 49
  • 62
1

This is allowed as an extension by some compilers, but is not strictly part of C++.

int Hello[n];

As an alternative, you can allocate the memory yourself:

int* Hello = new int[n];

And free it yourself also:

delete[] Hello;

But you can avoid manual memory management by usng std::vector from <vector>. One of its constructors accepts an initial size:

vector<int> Hello(n);  // Vector with n elements, all initially 0.

You can also set an initial capacity without resizing, to do the allocation once:

vector<int> Hello;  // Empty vector.
Hello.reserve(n);   // Allocate space for n elements; size() is still 0.

Then read into an int and use push_back to insert values:

int value;
while (input >> value)
    Hello.push_back(value);

Note the use of input >> value as the loop condition—this reads as long as reads are successful. eof() returns true only when the last read operation failed due to unexpected end of file, which is unlikely to be exactly what you want.

Jon Purdy
  • 53,300
  • 8
  • 96
  • 166
0

For a start the second

while (!input.eof())

Will always fail. That terminated the first one and then you set about closing that input stream!

Ed Heal
  • 59,252
  • 17
  • 87
  • 127