6

I have an array problem that i want to overcome, if i change the value of const int "are" to 2048 the program runs fine but at 8192 or even at 4096 ( just 130,000 elements) it does not work and breaks. How do i get around this ?

#include <iostream>
#include <fstream>
#include <windows.h>
#pragma warning (disable : 4820 4619 4668 4101)
HANDLE ghEvents;
const int arc = 2048; 
const int are = 8192;
struct DataStructure_init {
 int main_seq[are][32];
 int main_seq2[are][32];
 int main_seq3[are][32];
 int main_lim[are];
};
struct DataStructure_trus {
 int net[arc]; 
 int r6[arc];
 int thr[arc];
};
int ftrus (unsigned char cmain[],int array_inst[],DataStructure_trus& va);
int finit (DataStructure_trus va,DataStructure_init& in);
using namespace std;
int main()
{
 unsigned char cmain[are];
 int array_inst[64]={0};
 DataStructure_trus va; 
 DataStructure_init in;
 ftrus(cmain,array_inst,va);
 finit(va,in);
 cin.get();
}
int finit (DataStructure_trus va,DataStructure_init& in)
{
 int nb=0,flag=0,lock=0;
 for(int i=0;i<are;i++){
 for(int j=0;j<24;j++){
 in.main_seq[i][j]=va.thr[(i*24)+j];
 }
 }
 return 0;
}
int ftrus (unsigned char cmain[],int array_inst[],DataStructure_trus& va)
{
 int g=0; 
 ifstream in("C:\\Dev-Cpp\\DCS\\Decom\\trus.txt", ios::binary);
 unsigned char c;
 while( in.read((char *)&c, 1) )
 { 
 cmain[g]=c;
 if(cmain[g]==' ' && cmain[g-1]=='t' && cmain[g-2]=='e' && cmain[g-3]=='n') {array_inst[1]=g+1;}
 else if(cmain[g]==' ' && cmain[g-1]=='r' && cmain[g-2]=='h' && cmain[g-3]=='t') {array_inst[9]=g+1;array_inst[21]=g-7;}
 g++;
 }
 array_inst[29]=g-2;
 for(int i=0;i<64;i++){va.r6[i]=0;}
 for(int i=array_inst[1];i<array_inst[21];i++){
 if(cmain[i]=='1'){va.net[va.r6[1]]=1;va.r6[1]++;}
 else {va.net[va.r6[1]]=0;va.r6[1]++;}
 }
 for(int i=array_inst[9];i<array_inst[29];i++){
 if(cmain[i]=='1'){va.thr[va.r6[9]]=1;va.r6[9]++;}
 else {va.thr[va.r6[9]]=0;va.r6[9]++;}
 }
 return 0;
}
asked Jan 26, 2012 at 10:10
7
  • Interesting that you write "just 130,000 elements". On what basis do you assume that this is not a lot? Commented Jan 26, 2012 at 10:17
  • So can i do something like this create a pointer in the data structure and allocate the space dynamically in the main ? Commented Jan 26, 2012 at 10:23
  • en.cppreference.com/w/cpp/container/vector jcatki.no-ip.org/fncpp/Resources Commented Jan 26, 2012 at 10:26
  • @ Lightness Races : Well i've had no problems creating single sized arrays with 250,000 elements, so i assumed 130,000 was not much and thought the system should have handled that.i didn't realize there was a stack limit. Commented Jan 26, 2012 at 10:36
  • 250,000 whats? 130,000 ints is likely to be at least 520K. Depending on the place in your program where you use it, and how much memory is taken by other things on the stack, that could push you over the top of your stack. Commented Jan 26, 2012 at 10:40

5 Answers 5

5

Allocate the array dynamically, since there are often limits on how much data you can have on the stack (which is where automatic local variables typically end up):

unsigned char* cmain = new unsigned char[are];
answered Jan 26, 2012 at 10:13
Sign up to request clarification or add additional context in comments.

10 Comments

I was scared of doing that because my program is much bigger and crashes a lot, what happens if i haven't deleted the memory and my program crashes before that, would the processor de-allocate the memory ?
@Gambit: Make your program not crash.
@GambitKing: All memory allocated by a process is freed when it terminates. So, a program crash automatically frees your memory.
@GambitKing, if your program crashes, everything will be taken from it.
Is there any free program that i can use that that frees the memory after these kind of crashes ?
|
3

What everyone else said: you're trying to allocate a lot of stuff on the stack. A lot.

Instead, dynamically-allocate the memory buffer... by using a standard container for memory management:

std::vector<unsigned char> cmain(are);
answered Jan 26, 2012 at 10:18

3 Comments

Whenever i use vectors my programs runs twice as slow, i know its my implementation that is the problem though.
Then you're using vectors incorrectly. When you properly reserve capacity as possible, vector usage has the same speed as that of arrays. Standard containers are the correct approach here. They take care of allocation and of clean-up for you. And they're free.
Also, if you enable optimizations it should run as fast as with a regular C-style array, since, once the operator[] calls are inlined, the generated code should be the same.
2

You don't have to put the arrays on the stack in main(), you can just as well allocate them statically before entering the function. That will put them in an area that is not limited by the default stack size.

unsigned char cmain[are];
int array_inst[64]={0};
DataStructure_trus va;
DataStructure_init in;
int main() {
 ftrus(cmain,array_inst,va);
 finit(va,in);
 cin.get();
 } 
answered Jan 26, 2012 at 10:16

1 Comment

this works wonders, declaring the data structure outside the main, even with "are" as 20,000 the program executes without a glitch.
1

You can start with allocating DataStructure_* not in the stack. For instance by prepending the static keyword.

static DataStructure_trus va; 
static DataStructure_init in;
answered Jan 26, 2012 at 10:14

6 Comments

... which will result in an enormous executable. The right solution here is to use the heap.
@MatteoItalia, that's why "start with". This is an attempt to minimize changes.
@LightnessRacesinOrbit, you may be misguided now into being proud of your witty comment.
Was there any to be offensive? :)
@MatteoItalia: why an enormous executable? That's an uninitialized declaration, isn't it?
|
1

You are putting the data structure on the stack in main, and it is pretty huge. You can either increase the stack size (depends on your system), or allocate the structure on the heap with new or malloc.

answered Jan 26, 2012 at 10:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.