Simple array with access violation errors.

Hello,

I have a rather large project which uses a float array for OpenGL but I believe I am reproducing the problem with a simple example below.

Essentially I am getting access violation and HEAP CORRUPTION errors when I execute the code below.

I frankly am not sure why this is happening because all I am doing is simply creating, iterating, and then deleting a simple float array.

Any ideas?

Thank you:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
#include <sstream>

int main(int argc, char** argv) {

	std::cout << "Please enter in the size of the array you would like to create:\n\n";

	std::string strSizeOfArray;

	std::cin >> strSizeOfArray;

	std::stringstream NumberConverter(strSizeOfArray);

	int iSizeOfArray = 0;

	NumberConverter >> iSizeOfArray;

	float* FloatArray = new float(iSizeOfArray);

	std::cout << "An array of " << strSizeOfArray << " float variables was created. Now we will now iterate through this array 10,000 times.\n\n";

	for (int o = 0; o < 10000; ++o) {

		for (int i = 0; i < iSizeOfArray; ++i) {

			FloatArray[i] += 1.25f;

		}

	}

	std::cout << "We have iterated over " << strSizeOfArray << " float variables in the array 10,000 times.\n\n Now we will print the contents of the array:\n\n";

	for (int i = 0; i < iSizeOfArray; ++i) {

		std::cout << "The " << i << " element in the array has the value: " << FloatArray[i] << "\n";

	}

	std::cout << "Now we are deleting the array.";

	delete[] FloatArray;

	std::cout << "Exiting...";

	system("pause");

}
float* FloatArray = new float(iSizeOfArray);

You're not creating an array with the above statement. Or is that a typo?
You are right, changing that line allowed the array to be created and I don't get the access violation error...

Corrected code below:

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>
#include <sstream>

int main(int argc, char** argv) {

	std::cout << "Please enter in the size of the array you would like to create:\n\n";

	std::string strSizeOfArray;

	std::cin >> strSizeOfArray;

	std::stringstream NumberConverter(strSizeOfArray);

	int iSizeOfArray = 0;

	NumberConverter >> iSizeOfArray;

	float* FloatArray = new float[iSizeOfArray];

	std::cout << "An array of " << strSizeOfArray << " float variables was created. Now we will now iterate through this array 10,000 times.\n\n";

	for (int i = 0; i < iSizeOfArray; ++i) {

		FloatArray[i] = 0.0f;

	}

	for (int o = 0; o < 10000; ++o) {

		for (int i = 0; i < iSizeOfArray; ++i) {

			FloatArray[i] += 2.93f;

		}

	}

	std::cout << "We have iterated over " << strSizeOfArray << " float variables in the array 10,000 times.\n\n Now we will print the contents of the array:\n\n";

	for (int i = 0; i < iSizeOfArray; ++i) {

		std::cout << "The " << i << " element in the array has the value: " << FloatArray[i] << "\n";

	}

	std::cout << "Now we are deleting the array.";

	delete[] FloatArray;

	std::cout << "Exiting...";

	system("pause");

}
Topic archived. No new replies allowed.