473,324 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Declare a two-dimension array

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;
}
//**************************

Dec 14 '06 #1
4 2594
ottawajn wrote:
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

Dec 14 '06 #2
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[i] = (float)malloc(512 * sizeof(float));


"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;
}
//**************************
Dec 14 '06 #3
wahaha wrote:
"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[i] = (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

Dec 14 '06 #4
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.
system("PAUSE");
return EXIT_SUCCESS;
}
//**************************
Dec 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Divick | last post by:
Hi all, does any one know what is the right way to forward declare classes within namespaces. Though I have been using the syntax as follows but it doesn't sound good to me. namespace...
2
by: N. Demos | last post by:
I have a user control with code behind of which two instances are created/declared in my aspx page. The aspx page has code behind also, as I need to access methods of the usercontrols on page...
10
by: Bob Hollness | last post by:
Hi all. I have a Sub that calls another sub. Both subs use a common object, so I used Public to declare it at the top of the module, as below. Public Writer As StreamWriter =...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
5
by: peppi911 | last post by:
Hi, is it possible to create a cursor from a dynamic string? Like: DECLARE @cursor nvarchar(1000) SET @cursor = N'SELECT product.product_id FROM product WHERE fund_amt > 0' ...
7
by: Tracks | last post by:
I have old legacy code from vb5 where data was written to a file with a variant declaration (this was actually a coding error?)... in vb5 the code was: Dim thisdata as integer Dim thatdata...
2
by: freegnu | last post by:
how to declare a friend function that can access two class it will look like the following class A { private: int i; public: A(){} ~A(){} friend void call(A &a, B &b);
1
by: ares.lagae | last post by:
- I have a typelist and I want to declare a member variable for each of the types. How can I do that? E.g. I have the typelist "typedef boost::mpl::vector<int, float> types;" and I want to declare...
4
by: rasmidas | last post by:
There are two different directories. sparse/src/stk/dmg_apiutil and sparse/src/stk/dmg_meta_doc Inside these two directories there are lot of C++ files. I have written a function:
5
by: dancer | last post by:
Using ASP.net 1.1 and VB Is it possible to declare variables in their own subroutine? I had my DIM statements within a sub that worked just fine. I wanted to be able to use them in another...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.