Problem with memory

Hi,

I have a problem with memory. I use Code Blocks. I created a few arrays(char array1[n][n], int array2[n*n]). For example for n=700 my program crashes on the second array. How to fix it?
If you declared these inside a function, then I'm pretty sure your stack is not 2 meg in size. Probably the best thing to do is grab memory from the heap using new operator.
Could you explain me how to do it?
I'm not exactly sure how to do multi dimensional arrays with new, but this is basically how it would work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

 using namespace std;
 
	const int n = 700;
	
int main (void) {
	int Size = n * n;
	
	char *Array = new char [Size];
	int *Array2 = new int [Size];

	cout << "\n\t\t" << hex << "Array = " << &Array << "  Array2 = " << Array2 << endl;
	
	delete [] Array;
	delete [] Array2;
	return 0;
}
It works, but very very slowly. For small n it works much slower than old algorithm. Is there any other solution?
Last edited on
This is my templated multi-dimensional array class. I have it coded out to 4 dimensions, but you seem to only need two. Should work for any simple variable type or object. Output is at end of program. Let me know how it works...

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <cstdlib>
#include <cstdio>
#include <string>
#include <memory.h>


template <class datatype> class CArray
{
 public:
 CArray(int i) : pObjs(0)                         // One Dimensional Array Constructor
 {
  this->pObjs=new(std::nothrow) datatype[i]();
  d1=i, d2=0, d3=0, d4=0;
  iNumObjects=i;
 }

 CArray(int i, int j) : pObjs(0)                  // Two Dimensional Array Constructor
 {
  int iNumElements=i*j;
  this->pObjs=new(std::nothrow) datatype[iNumElements]();
  d1=i, d2=j, d3=0, d4=0;
  iNumObjects=i*j;
 }

 CArray(int i, int j, int k) : pObjs()           // Three Dimensional Array Constructor
 {
  int iNumElements=i*j*k;
  this->pObjs=new(std::nothrow) datatype[iNumElements]();
  d1=i, d2=j, d3=k, d4=0;
  iNumObjects=i*j*k;
 }

 CArray(int i, int j, int k, int l) : pObjs(0)    // Four Dimensional Array Constructor
 {
  int iNumElements=i*j*k*l;
  this->pObjs=new(std::nothrow) datatype[iNumElements]();
  d1=i, d2=j, d3=k, d4=l;
  iNumObjects=i*j*k*l;
 }

 datatype& operator()(int i)                      // One Dimensional Accessor
 {
  return pObjs[i];
 }

 datatype& operator()(int i, int j)               // Two Dimensional Accessor
 {
  return pObjs[i*d2 + j];
 }

 datatype& operator()(int i, int j, int k)        // Three Dimensional Accessor
 {
  return pObjs[i*d2 + j + k*d1*d2];
 }

 datatype& operator()(int i, int j, int k, int l) // Four Dimensional Accessor
 {
  return pObjs[i*d2 + j + k*d1*d2 + l*d1*d2*d3];
 }

 bool blnMemoryIsGood()
 {
  return !!pObjs;
 }

 int UBound(int iDim)
 {
  if(iDim==1)
     return d1-1;
  if(iDim==2)
     return d2-1;
  if(iDim==3)
     return d3-1;
  if(iDim==4)
     return d4-1;
  else
     return 0;
 }

 void ClearMemory()   // Non-class types like ints, doubles, etc., need to be initialized
 {                    // to zero.  This must not be done for class types.
  memset(this->pObjs, 0, sizeof(datatype)*this->iNumObjects);
 }

 ~CArray()
 {
  delete [] this->pObjs;
 }

 private:
 int       iNumObjects; // We'll need this to zero memory for non-class types
 datatype* pObjs;       // pointer to the base memory allocation for array
 int       d1;          // Dimension #1
 int       d2;          // Dimension #2
 int       d3;          // Dimension #3
 int       d4;          // Dimension #4
};


template<typename t2> void Output(CArray<t2>& Ar2)
{
 int i,j;

 for(i=0; i<=Ar2.UBound(1); i++)
 {
     for(j=0; j<=Ar2.UBound(2); j++)
     {
         printf("%s\t",Ar2(i,j).c_str());
     }
     printf("\n");
 }
 printf("\n\n");
}


int main(void)
{
 int c=4,r=3;
 CArray<std::string> ar_2(r,c);

 if(ar_2.blnMemoryIsGood())
 {
    char szBuffer[16];
    printf("ar_2() Allocated OK!\n");
    printf("ar_2.UBound(1) = %d\n",ar_2.UBound(1));
    printf("ar_2.UBound(2) = %d\n\n",ar_2.UBound(2));
    for(int i=0; i<=ar_2.UBound(1); i++)
    {
        for(int j=0; j<=ar_2.UBound(2); j++)
        {
            ar_2(i,j)="(";
            sprintf(szBuffer,"%d",i);
            ar_2(i,j)=ar_2(i,j)+szBuffer+",";
            sprintf(szBuffer,"%d",j);
            ar_2(i,j)=ar_2(i,j)+szBuffer+")";
        }
    }
 }
 Output(ar_2);
 getchar();

 return 0;
}

#if 0
ar_2() Allocated OK!
ar_2.UBound(1) = 2
ar_2.UBound(2) = 3

(0,0)   (0,1)   (0,2)   (0,3)
(1,0)   (1,1)   (1,2)   (1,3)
(2,0)   (2,1)   (2,2)   (2,3)
#endif 
Last edited on
It works, but very very slowly. For small n it works much slower than old algorithm. Is there any other solution?
Slower that what? I thought you said the code didn't compile/run?

On a 64bit system char array[n][n] where n is 700 requires 1 * 700 * 700 bytes (490,000 bytes). And int array[n * n) requires 8 * 700 * 700 bytes (3,920,00 bytes).

Now, if that code is rewritten to get the memory from the heap (rather than the stack as in your code), the OS will need to find 4.3G of space for those arrays.

How much free memory do you have on your computer? You can check it out with Task Manager. The very first time you run your program, the OS will need to find all that memory to run your program. On, subsequent runs, it'll be faster to start up as the OS will find the space left over from the last run.

Do you really need to spend 4.5G on two arrays? Can't you find a cheaper way to run your program?

Remember, MS-DOS apps ran in 512K of base memory with possibly 2M of extended memory.

Win16 apps ran in around 4M of extended memory.

Win32 only provides applications with 2G of memory (that's Windows XP). You're spending 4.5G on two arrays.
An int on x64 is still only 4 bytes, isn't it kbw? So 490,000 bytes needed for the two dimensional char array, and another 1,960,000 bytes needed for array[700*700]. If my arithmetic is anywhere near correct that comes to 2,450,000 bytes, which of course is still working the stack pretty hard. :)
Topic archived. No new replies allowed.