This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Stack overflow issues

Tags: None
(comma "," separated)
jasonmtroos
Registered Member
Posts
2
Karma
0

Stack overflow issues

Mon Apr 30, 2012 8:56 pm
I have a program in which I would like to define types in the following manner:
Code: Select all
typedef Matrix< double, N, N > MatrixNN;
typedef Matrix< double, J, N > MatrixJN;

etc. where J, N, and similar constants are generally < 700 and known at compile-time. The largest matrix would be about 700 x 25. The program was causing a stack overflow (compiled for Windows XP (32-bit) with VisualStudio 2010), which led me to this page: http://eigen.tuxfamily.org/dox/TutorialMatrixClass.html#TutorialMatrixDynamic

Worse, trying to create a very large matrix using fixed sizes could result in a stack overflow, since Eigen will try to allocate the array as a static array, which by default goes on the stack.

Realizing I needed to force these types to be created on the heap, but also considering I would like to have some record of their intended size at compile time, led me to define the following:
Code: Select all
typedef Matrix< double, Dynamic, Dynamic, 0, N, N > MatrixNN;

This solved the problem of the stack overflow, but I've subsequently extended the code, and now I'm running into the same problem again. According to http://eigen.tuxfamily.org/dox/TutorialMatrixClass.html#TutorialMatrixOptTemplParams,

MaxRowsAtCompileTime and MaxColsAtCompileTime are useful when you want to specify that, even though the exact sizes of your matrices are not known at compile time, a fixed upper bound is known at compile time. The biggest reason why you might want to do that is to avoid dynamic memory allocation. For example the following matrix type uses a static array of 12 floats, without dynamic memory allocation:
Code: Select all
 Matrix<float, Dynamic, Dynamic, 0, 3, 4>



My question is this: does Eigen always pre-allocate space on the stack for matrices defined with Max<Cols/Rows>AtCompileTime, or does it only do so for smaller matrices (i.e. according to some optimization rule)? Is there some secret to defining a matrix such that: (1) rows and/or cols are set to constant values at compile time, and (2) space for the matrix is always allocated dynamically from the heap?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Stack overflow issues

Mon Apr 30, 2012 9:13 pm
The Max<Cols/Rows>AtCompileTime are precisely to avoid heap allocation by enforcing stack allocation even for matrices with runtime sizes. What you want is exactly the opposite, however this is not supported by Eigen::Matrix. Why do you need to know the size at compile time? For such large matrices there is really no advantage.
jasonmtroos
Registered Member
Posts
2
Karma
0

Re: Stack overflow issues

Mon Apr 30, 2012 10:47 pm
Thank you for the clarification, that's what I was hoping to understand. As for why I would want to set the size at compile time, I have found that doing so helps me avoid some hard-to-spot coding errors and has proved helpful in keeping my code more readable. Thanks again for the quick and helpful reply.


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]