5

I'm computing some values and stocking them in a variable using a function like what is below:

array<array<double,1000>,1000> index;
sum(double A, ..., array<array<double, 1000>,1000> & index);

I make a quick watch on the index array of array and it's filled with values just in the execution of the above declaration. It's OK

But! As soon as I call another function in which I use the index array, whose declaration is as follow:

average(..., array<array<double,1000>,1000> index, ...) 

I'm getting an Unhandled exception (Stack Overflow) which redirects me to an asm file (chkstk.asm):

 test    dword ptr [eax],eax     ; probe page.

Any idea how to resolve this?

5 Answers 5

4

By default each thread in Win32 has 1 MB of stack space, and a million doubles would take up 8 MB of stack space. The solution is to allocate them from the heap using new.

Sign up to request clarification or add additional context in comments.

4 Comments

So the solution is to opt for Dynamic arrays of arrays how can the array<...<...,..>> can be written in that case with new?
@MelMed: I'd suggest vector<array<double,1000>> index(1000); Using new directly is usually a bad idea.
@MelMed: The solution is to alter the average function to receive a pointer.
why new? Why not use vector<array<>>?
3

A dynamic way (to avoid stack overflow) of declaring 1000 x 1000 doubles in a 2d fashion is

std::vector<std::array<double,1000>> index(1000);

-edit- As Mike Seymour already suggested in the comments. Honour to whom honour is due... ;)

Index is of type std::vector<std::array<double,1000>> and therefore you'll need to have your arguments like that.

void average (..., std::vector<std::array<double,1000>> & index, ...)

7 Comments

Keep in mind that >> will not work on C++98, just incase the OP is using that. You'll need > >
@BalogPal Do you mean the average is taking index (by value) rather than & index (by reference, which could do with being const &)?
And how to access to that kind of variable in a two for Loop? I used to make with the array of array A[i][j] What about now?
@MelMed Using operator[] will return a reference to a std::array<double,1000> which in turn is accessible using operator[] giving a double element each. Therefore you can still use index[i][j] to access your elements.
that will keep working. You even get ability to fast swap(). But you may be subject to accidental resize().
|
0

Well, since you don't pass a pointer to "index" to the average function, your object (i.e. array) will be reconstructed (copied) on the stack. I don't know exactly what the stack size is by default, but you use up ~8MB just for your array, which is a bad idea.

Comments

0

Ok, let's assemble all the pieces:

  • sizeof(array,1000>) is about 8Mbytes
  • usual stacks are smaller, so you face problems when
    1. create such a local variable
    2. pass it by value

If the stack has less remaining space behavior is undefined. In debug build you may be lucky to get a relevant message to figure it out. In typical release build stack checks are turned out and putting you in the wild.

As your object now sits at namespace, the immediate problem can be cured by just passing it by reference. What is usually a good idea anyway.

If you can't avoid the listed problematic uses you must reduce the class size. The simplest solution is to make it vector<array<double,1000>> that you create with the size constructor, immediately having the whole size.

Comments

0

I used an object ( Model model = new Model; ) to store some vectors (arrays).

float fc1[input_dim][hidden_layer_dim_1] = { 0.0 }; float fc2[hidden_layer_dim_1][hidden_layer_dim_2] = { 0.0 }; float fc3[hidden_layer_dim_2][output_dim] = { 0.0 };

I got this error because dimensions are a little bit big :). I solved it with this change:

Model model; ->> Model *model = new Model;.

When i used pointer, the problem was solved. Of course variables are changes with this:

model.hidden_layer_dim_1 -> model->hidden_layer_dim_1

Comments

Your Answer

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.