473,788 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2617
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(5 12 * 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(5 12 * 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<flo at>.

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
2660
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 myVeryOwnNamespace { class myClass1; }
2
1980
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 submit. I've read several post here and articles on the web on this topic. What little I have learned from them is that you have to pre-compile the usercontrol in order to access it in the aspx code behind. I did this, compiling the usercontrol...
10
2159
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 = File.CreateText(Application.StartupPath & "\Update.txt") The first time my Sub runs it works fine. But the second time it fails telling me "Cannot write to a closed TextWriter. I am guessing that this is
23
3861
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? Here's what I thought should work, but apparently doesn't: class Foo; void f1(Foo* p)
5
13844
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' DECLARE ic_uv_cursor CURSOR FOR @cursor
7
2149
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 Dim someother as integer thatdata = ubound( Array1 )
2
2053
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
2786
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 member variables with type "int" and "float". - I have a typelist and I want to declare a variable based on each of the types types. How can I do that? E.g. I have the typelist "typedef boost::mpl::vector<int, float> types;" and I want to...
4
1464
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
2187
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 sub, so I isolated them in their own subroutine. But when I call that subroutine within another sub and then try to use those variables, it does not remember that they have been declared. I get the message, "Name is not declared."
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10113
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5402
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.