Solved

In C++, how much more do dynamic multi-dimensional arrays use?

Posted on 2014-03-06
7
549 Views
Last Modified: 2014-03-06
In looking at the available Physical Memory in Task Manager while my program is executing in the debugger, I've realized that the dynamically allocated multi-dimensional arrays in my VS2012 C++ application use A LOT MORE MEMORY than I would expect.  

Let's assume for this discussion that my computer needs 8 bytes to store a variable of data type "double".  Well, I would've expected that the amount of memory (ie, the heap) needed for a two-dimensional array of size [1000][50] (using the "new" keyword) would simply be 1000 * 50 * 8 bytes =  400,000 bytes (ie, 400 K).  Well, I must be misunderstanding the algorithm, because more than twice this much memory is actually be "taken" from the supply of available memory.

So, if someone can explain the algorithm for the use of dynamically allocated memory with regard to arrays, and in particular multi-dimensional arrays, I would greatly appreciate it.
0
Comment
Question by:cjm20
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39909255
Are you using "managed c++" or unmanaged in your project properties? The memory management stuff that VC++ tacks on can be rather large.

The way operating systems allocate memory is not simple at all. Your program may ask for 400k and the OS might give it a 1MB block.

In Task Manager, go to the Performance tab, click Resource Monitor...
Then click on the Memory tab. See if the numbers there for your process make more sense.
0
 
LVL 10

Expert Comment

by:aboo_s
ID: 39909276
I think it depends on the architecture of your machine OS , 32bit or 64bit depends how large is one word in your machine's memory.
0
 
LVL 10

Expert Comment

by:aboo_s
ID: 39909281
if a variable needs 8 bytes to be stored and a word in your machine is 32bytes long than 8 bytes are stores in 32.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 39909314
A 64bit system will generally use 64bits for its default allocation "word"
But 64 bits is 8 bytes. Also arrays are typically packed anyway.
If you declare 1 character, you'll see that it takes up 8 bytes.
If you declare an array of 5 characters... 8 bytes
11 characters... 16 bytes, etc etc. It will typically round up to the nearest 8 bytes.

But the memory that the OS allocates to your process is a different story. If you declare a large array, it can give you twice that memory in case you allocate more so it can be efficient and give you memory near what you already have. Looking at the free physical memory isn't really a good indication of how much memory a c++ object is taking up.
0
 
LVL 34

Expert Comment

by:sarabande
ID: 39909388
if you build the debug configuration the extra space is reserved for the debugger.

Sara
0
 

Author Closing Comment

by:cjm20
ID: 39909978
Thanks Tom (and to everyone else who chimed in).  The Resource Viewer in Task Manger is nice, but still didn't help pinpoint exactly why the excess memory for my dynamic arrays is, well, so excessive.  But i think you've hit on it when you mentioned the OS trying to be efficient in its anticipation of the application allocating MORE memory than it just asked for.  This makes sense.  The OS, for my big arrays, is allocating about 2.5 times the amount I would think my array would need.  I've also read elsewhere that for multi-dimensional dynamically-sized arrays, the pointers to the arrays within arrays also need to reside in memory. Said pointers for an array of the "double" data types take 4 bytes I'm told, so with a very large array, this is more a lot more overheard.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39910061
Sara is right too. If you were running a debug build, then that would explain most of the extra memory.

In C++ a two dimensional array like that does not store pointers to "arrays within arrays." If you had some type of two dimensional array that had rows of different lengths that would apply, but double x[1000][50] is just going to give you one array. x[ i ][ j ] just translates to something like x[i*50+j].

On a 64 bit system, pointers take up 8 bytes.
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question