Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C++ declare a matrix on the heap

matrix declare

  • Please log in to reply
4 replies to this topic

#1 JakeWelton

JakeWelton

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 152 posts
  • Programming Language:Java, C++, C#, PHP, JavaScript, PL/SQL
  • Learning:Objective-C, Python, Perl, Ruby, Visual Basic .NET, Haskell

Posted 05 November 2012 - 07:53 AM

Hello,

Okay so i know how to declare a matrix in the stack by using the following statement:

int matrix[5][10];

But my question is this. How can i do the same but by using the stack????

Any ideas??

Thanks,
Jake
  • 0

sig.jpg

"Before you judge someone, walk a mile in their shoes; after that, who cares?! Their a mile away and you've got their shoes!"


#2 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 05 November 2012 - 08:15 AM

I found a good example of how to do this:
http://www.devx.com/tips/Tip/33488

Basically it involves creating a new array of pointers to act as the columns, then creating a new array at each of those pointers to act as the rows.

EDIT: You could also just create a one-dimensional array of size rows * cols, and write a formula to convert a pair of 2d coordinates to a 1d coordinate in this contiguous block of memory.

1d-coord = Y-coord * row-length + X-coord

  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3 Zer033x

Zer033x

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 50 posts
  • Programming Language:C++, C#, Lua
  • Learning:C++, C#, JavaScript, Lua, Others

Posted 09 November 2012 - 11:05 PM

Why does stack or heap matter exactly anyone?
  • 0

#4 JakeWelton

JakeWelton

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 152 posts
  • Programming Language:Java, C++, C#, PHP, JavaScript, PL/SQL
  • Learning:Objective-C, Python, Perl, Ruby, Visual Basic .NET, Haskell

Posted 09 November 2012 - 11:28 PM

The heap has a lot more memory than the stack so if your processing a image or something while requires a lot of memory then the heap would be used. For normal variables the stack is normally enough.
  • 0

sig.jpg

"Before you judge someone, walk a mile in their shoes; after that, who cares?! Their a mile away and you've got their shoes!"


#5 gregwarner

gregwarner

    Obi Wan of Programming

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1586 posts
  • Location:Arkansas
  • Programming Language:C, Java, C++, C#, PHP, Transact-SQL

Posted 11 November 2012 - 08:20 AM

Also, when you're allocating arrays, allocating on the stack requires that you know exactly how much memory you wish to allocate at compile time. Think of it this way--the number that goes between the square brackets has to be a hard-coded number. If you want to allocate some memory, the quantity of which will be arbitrarily decided at run time, you must do it on the heap, since it's impossible to do this on the stack.
  • 0

ti-99-sig.png
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.
– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid






Also tagged with one or more of these keywords: matrix, declare

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download