3

I came across a memory problem when I declare a variable:

std::pair<int, int> best_cost[n][m][DIR]; 

In this case, n and m are both const int 1066 and DIR is 8. By running Valgrind I get an error:

Invalid write of size 8 ==73213== at 0x15D3B75E: simple_planner::SimplePlanner::aStar(geometry_msgs::PoseStamped_<std::allocator<void> > const&, geometry_msgs::PoseStamped_<std::allocator<void> > const&, std::vector<geometry_msgs::PoseStamped_<std::allocator<void> >, std::allocator<geometry_msgs::PoseStamped_<std::allocator<void> > > >&) (in /home/yyu/devel/lib/libsimple_planner.so) ==73213== Address 0xfe0f340 is on thread 8's stack 

Am I declaring the variable wrong?

My end use of this variable is to get the direction on a specific coordinate.

0

1 Answer 1

5

Going by your numbers, best_cost is (4+4)*1066*1066*8 = 72 MB. This is well above the default stack size limit for most platforms. Moving this allocation to the heap or the static section should allow you to safely use it.

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

8 Comments

And how exactly can I move it to heap or static section ? :)
Either move the definition of best_cost to the top level (static section) or use the new operator to create your structure (heap). See this post for more information.
BTW, on linux the default stack size is 10 MB on windows it is 1 MB.
@Botje: No new please, std::vector<..> or std::unique_ptr<std::array<..>> instead.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.