I am trying to create a very large 3D array. I am currently using a psuedo-3D array with a 1D array. Here is what I have so far:

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <istream>
#include <string>
#include <vector>
#include <numeric>
using namespace std;


const int depth = 500;
const int height = 500;
const int width = 500;
int jimmy [height*width*depth];
int n,m,k;
int x = 2, y = 1, z = 2;
// m=x, n=y, k=z

int main ()
{
	for (k=0; k < depth; k++)
		for (n=0; n < height; n++)
			for (m=0; m < width; m++)
			{
				jimmy[m + n * width + k * width * height] = (m+1)*(n+1)*(k+1);
			}

		

			system("pause");
			return 0;

};

This is working fine so far. However, I would like to set the depth, height and width to 1000 or greater. When I make an array this big I get the following error:

Error 1 error C2148: total size of array must not exceed 0x7fffffff bytes

What would be the easiest way to build an array this large (1000x1000x1000) while avoiding this problem?

Recommended Answers

All 4 Replies

What does this supposed to do??? Make a physical 3d character on the screen??? Sorry, im a noob when it comes to 3d

Can you tell us what you want to do with the array? We may be able to suggest a better solution.

I am trying to create a very large 3D array. I am currently using a psuedo-3D array with a 1D array. Here is what I have so far:

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <istream>
#include <string>
#include <vector>
#include <numeric>
using namespace std;


const int depth = 500;
const int height = 500;
const int width = 500;
int jimmy [height*width*depth];
int n,m,k;
int x = 2, y = 1, z = 2;
// m=x, n=y, k=z

int main ()
{
	for (k=0; k < depth; k++)
		for (n=0; n < height; n++)
			for (m=0; m < width; m++)
			{
				jimmy[m + n * width + k * width * height] = (m+1)*(n+1)*(k+1);
			}

		

			system("pause");
			return 0;

};

This is working fine so far. However, I would like to set the depth, height and width to 1000 or greater. When I make an array this big I get the following error:

Error 1 error C2148: total size of array must not exceed 0x7fffffff bytes

What would be the easiest way to build an array this large (1000x1000x1000) while avoiding this problem?

Ah a tremendous array! I've never tried to allocate 4 GB on the stack. Was this your intent to use that much memory? Try to allocate the memory dynamically, at a sacrifice of some speed. Out of curiousity let me know the completion time of your operations if you get it to work.

int* jimmy = new int[1000*1000*1000];

Be aware that it appears [m + n * width + k * width * height] will exceed the size of your array and crash your program. You cannot access past [width*height*depth - 1];

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.