1

I'm not sure why my 2d array initialization causes a seg fault, so I have

void viterbi_algorithm(double init[], double A[][26], double B[][2], int obs[], 
                       int mostLikelyStates[]){


   cout << "start" << endl;


   double table1[26][68000];
   double table2[26][68000];

..

If I comment out the two tables, everything will be okay. Am I asking for too much memories?

My error when I ran gdb

Program received signal SIGSEGV, Segmentation fault.
___chkstk_ms () at /usr/src/debug/gcc-4.8.1-3/libgcc/config/i386/cygwin.S:146
146             orq     $0x0, (%rcx)            /* probe there */
1
  • Try to reduce your memory requirement. i.e. replace 68000 with 8000 and check it Commented Feb 24, 2014 at 10:28

4 Answers 4

1

Define these arrays using keyword static

   static double table1[26][68000];
   static double table2[26][68000];

In this case they will be allocated in the static memory.

The other approach is to use standard container std::vector. For example

   std::vector<std::vector<double>> table1( 26, std::vector<double>( 68000 ) );
   std::vector<std::vector<double>> table2( 26, std::vector<double>( 68000 ) );

You also can define them with keyword static

   static std::vector<std::vector<double>> table1( 26, std::vector<double>( 68000 ) );
   static std::vector<std::vector<double>> table2( 26, std::vector<double>( 68000 ) );
Sign up to request clarification or add additional context in comments.

5 Comments

You really should explain the implications of static here. It's not trivial. It means the app will always consume an extra 28mb even when it's not being used. It also makes the function ineligible to be run in parallel or re-entrant.
@ tenfour Well, you can return to using the arrays in the stack if you want.:)
Or a solution that's better than both of those suggestions? Also a vector of vectors / arrays is pointless overhead. Just dynamically allocate the original type of double[26][68000]; there's no need to pull in vector.
@tenfour Why are you writing to me? It is not my question. I only suggested some solurions that to escape stack overflow.
@tenfour Also it is not a good solution tp allocate dynamically such arrays every time when the function is called. First of all it will consume much time and the memory can be fragmented and the request will fail.
1
double table1[26][68000];
double table2[26][68000];

would exceed the stack size of the program. Please allocate it dynamically.

2 Comments

How do I allocate it dynamically?
new double[26][68000]
1

That's way too much memory for the stack. You could allocate the arrays manually on the heap with new[] and delete[], but that's tedious and prone to errors. May I suggest a vector of arrays?

#include <vector>
#include <array>

std::vector<std::array<double, 68000>> table1(26);
std::vector<std::array<double, 68000>> table2(26);

Comments

1

You are simply running out of stack space. You are allocating sizeof(double)*2*26*68000 bytes on the stack. That's over 28mb, when stack space is typically around 2mb.

Instead, allocate the memory dynamically. There are several ways to do this. The simplest is:

std::unique_ptr<double[][68000]> table1(new double[26][68000]);
std::unique_ptr<double[][68000]> table2(new double[26][68000]);

table1[x][y] = 5.0;
....

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.