Declare a two-dimension array

O

ottawajn

Hi, There,

I want to declare a two-dimension array by
"float coeftemp1 [1024][512];" in Dev c++. It doesn't work.

But, if I change it to "float coeftemp1 [1024][500];".
It works. Could any one know the reason?

Thank you,




//**************************
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
float coeftemp1 [1024][512];
system("PAUSE");
return EXIT_SUCCESS;
}
//**************************
 
D

Default User

ottawajn said:
Hi, There,

I want to declare a two-dimension array by
"float coeftemp1 [1024][512];" in Dev c++. It doesn't work.

But, if I change it to "float coeftemp1 [1024][500];".
It works. Could any one know the reason?

Thank you,




//**************************
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
float coeftemp1 [1024][512];
system("PAUSE");
return EXIT_SUCCESS;
}
//**************************

You are likely hitting an implementation-specific limitation on
automatic memory. Something along the lines of (but not necessarily)
using up all the available stack space.


A possible solution is some sort of dynamic array. As is often the
case, the recommended first approach is std::vector.





Brian
 
W

wahaha

I think Brian is exactly right. Assume your float is 8 bytes and
1024x512x8 = 4k byte. This might be the maximum automatic space. Try to
use another method such as:

float **coeftemp1 ;
int i;

coeftemp1 = (float *)malloc(1024 * sizeof(int *));

for (i = 0; i < 1024; i++){
coeftemp1 = (float)malloc(512 * sizeof(float));




"ottawajn дµÀ£º
"
 
M

Micah Cowan

wahaha said:
"ottawajn дµÀ£º
"
Hi, There,

I want to declare a two-dimension array by
"float coeftemp1 [1024][512];" in Dev c++. It doesn't work.

But, if I change it to "float coeftemp1 [1024][500];".
It works. Could any one know the reason?

Thank you,

I think Brian is exactly right. Assume your float is 8 bytes and
1024x512x8 = 4k byte. This might be the maximum automatic space. Try to
use another method such as:

float **coeftemp1 ;
int i;

coeftemp1 = (float *)malloc(1024 * sizeof(int *));

for (i = 0; i < 1024; i++){
coeftemp1 = (float)malloc(512 * sizeof(float));


Please refrain from top-posting.

This being comp.lang.c++, the new operator is frequently more useful
than malloc(). Ignoring that, your usage of malloc() above is very
dangerously broken (and would probably fail to compile, anyway). The
types you're using to cast the result of malloc are completely wrong,
as is your sizeof(int *).

For this reason and several others, C++ users might prefer to do
something like:

float (*coeftemp1)[512] = new float[1024][512];

or, probably more commonly, with a change in how elements are accessed:

float *coeftempl = new float[1024 * 512];

(hopefully, substituting declared constants for the meaningless magic
numbers above).

Perhaps better, depending on the situation, would be to use a
std::vector<float>.

HTH,
Micah Cowan
 
B

Bo Yang

ottawajn :
Hi, There,

I want to declare a two-dimension array by
"float coeftemp1 [1024][512];" in Dev c++. It doesn't work.

But, if I change it to "float coeftemp1 [1024][500];".
It works. Could any one know the reason?

Thank you,




//**************************
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
float coeftemp1 [1024][512];
There, it is too big too put this in your stack.
So, the OS may terminate your app.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top