DYNAMIC MEMORY ALLOCATION. Something is wrong with the program. It's not giving out the correct output.

1
2
3
4
5
10
15
17
5
6

The numbers above are stored in a text file called "text.txt".
I want to transfer the data in an array using pointers or dynamic memory allocation.

This is the code that i've written;
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
29
30
31
32
33
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	ifstream fin("text.txt");
	if(!fin.is_open())
	{
		cout << "File not open";
	}
	
	int count = 1;
	int size = 0;
	int *array = new int[count];
	
	while(!fin.eof())
	{
		fin >> array[size];
		size++;
		count++;
		array = new int[count];
	}
	count--;
//	size--;
	
	cout << endl;
	
	for(int i=0 ; i<size ; i++)
	{
		cout << array[i] << " ";
	}
	delete[] array;
}

The for-loop at the end prints random crap I don't know why. If i do this;
1
2
3
4
5
6
7
8
while(!fin.eof())
{
	fin >> array[size];
	cout << array[size];
	size++;
	count++;
	array = new int[count];
}

If i cout in the loop, the correct data is printed. The data isn't being stored in the array, how can i fix that?
Lets create a similar issue:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

int main() {
  int foo;
  int bar;

  int * array = &foo;
  array[0] = 42;
  std::cout << array[0] << '\n';  // fine, we see 42

  array = &bar;
  std::cout << array[0] << '\n';  // why no more 42?

  return 0;
}


Yours is worse, for your program leaks memory.


You have two options:

1. Use a container like std::vector that does the tedious work properly on your behalf.

2. Copy data from old array to new array and then deallocate the old array.
Please explain how this is a similar issue? You're using a pointer to point at 2 different addresses separately.

For me these are 2 different scenarios.

And I'm not supposed to use vector.
Last edited on
You're using a pointer to point at 2 different addresses separately.

So do you too. Different details, but same scenario.

Lets pretend that there are two values in the file. Lets "unroll" the loop:
int * array = new int [1]; // Address 1
// first iteration
fin >> array[0];
array = new int[2]; // Address 2
// second iteration
fin >> array[1];
array = new int[3]; // Address 3[/code]
Each call to new returns an address of different block of memory.
The code above allocates three blocks -- three separate arrays: for one, two and three integers.

Storing a new address to "array" overwrites the previous address. The previous block is still allocated, but nothing stores the address and it cannot be deallocated any more. Your delete [] deallocates the last allocated block that you still have an address for.
Okay! I get it now.

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
29
30
31
32
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
	ifstream fin("text.txt");
	if(!fin.is_open())
	{
		cout << "File not open";
	}
	
	int count = 1;
	int size = 0;
	int *array = new int[count];
	
	while(!fin.eof())
	{
		fin >> array[size];
		size++;
		count++;
	}
	count--;
//	size--;
	
	for(int i=0 ; i<size ; i++)
	{
		cout << array[i] << " ";
	}
	delete[] array;
	
	return 0;
}


Is this what you're talking about?

The for-loop is working now. Now the problem is that it only works if i have 5 numbers in the file. It crashes if there are more than 5 numbers.
No. It merely appears to work. Everything above one number in file is already undefined behaviour because you did allocate space for one integer only.

1
2
3
4
5
6
7
8
9
10
int value = 0;
while ( fin >> value ) {
  if ( /* the array is already full */ ) {
    // allocate new, larger array
    // copy all values from array to the new array
    // deallocate the old array
    // make pointer 'array' point to the new array 
  }
  // write value to vacant spot in the array
}
Topic archived. No new replies allowed.