pointer to array - memory usage 
Author Message
 pointer to array - memory usage

Dear Reader,

I have a question with respect to the used RAM
during execution of the following compiled C-code
(to ease my description I put line numbers):

-------------------- begin code -------------------
1 #include <stdio.h>
2 #include <stdlib.h>

3   float array[10000][1000][10];
4   float *arrayPtr;
5   int   iCount, jCount, kCount;

6 int main ()
7 {
8   arrayPtr = &array[0][0][0];

9    for ( iCount=0 ; iCount<1000  ; iCount++ ){
10    for ( jCount=0 ; jCount<10000 ; jCount++ ){
11     for ( kCount=0 ; kCount<10    ; kCount++ ){

12      *(arrayPtr + iCount*10000 + jCount*10 + kCount) = 0.0;

13//    *arrayPtr++ = 0.0;

14     }
15    }
16   }

17 return 0;
18 }
--------------------  end code  -------------------

Obviously this code initializes an array by using a pointer.
By using "line 12" where the pointer position is
calculated each loop, the memory use of the executable file is
low (appr. 20 MB). However, when I use "line 13" instead of
"line 12" (change the comment signs) the memory use of the
executable file increases drastically (appr. up to 100 MB).

To compile the C-code I am using the gcc-compiler under Linux.
The memory use I checked with the command "top". The problem is
that by using the "line 13" (which should actually faster run)
my RAM is too low and the computer starts to swap, the run time is
very long.

Does anybody know the reason of this behaviour?
Is this the problem of how the compiler maps the executable
into the RAM?
(A trick would be to increase the RAM, but I want to understand
the difference in using the two different pointer usages with respect
to the memory!)

Thanks for Your efforts

Cordially

Thomas
OO
OO
--



Sat, 15 Oct 2005 23:46:06 GMT  
 pointer to array - memory usage

Quote:

> 9    for ( iCount=0 ; iCount<1000  ; iCount++ ){
> 10    for ( jCount=0 ; jCount<10000 ; jCount++ ){
> 11     for ( kCount=0 ; kCount<10    ; kCount++ ){
> 12      *(arrayPtr + iCount*10000 + jCount*10 + kCount) = 0.0;

That does not initialize the entire array.  Both of the triples
iCount=1,jCount=0,kCount=0 and iCount=0,jCount=1000,kCount=0
access arrayPtr[10000].
--



Sun, 16 Oct 2005 07:20:25 GMT  
 pointer to array - memory usage

Quote:

> Obviously this code initializes an array by using a pointer.
> By using "line 12" where the pointer position is
> calculated each loop, the memory use of the executable file is
> low (appr. 20 MB). However, when I use "line 13" instead of
> "line 12" (change the comment signs) the memory use of the
> executable file increases drastically (appr. up to 100 MB).

The reason for that is that your program has a bug.  Carefully check
your array dimensions against the ranges of your for() loops and
you'll see where it is.  Your "line 12" version only initializes one
tenth of the actual array.

--

Even if all the snow were burnt, ashes would remain.
--



Sun, 16 Oct 2005 07:20:36 GMT  
 pointer to array - memory usage

Quote:

>I have a question with respect to the used RAM
[snip]
>3   float array[10000][1000][10];
>5   int   iCount, jCount, kCount;

An aside:  why bother with such confusing names?  Just make it "i, j, k".

Quote:
>9    for ( iCount=0 ; iCount<1000  ; iCount++ ){
>10    for ( jCount=0 ; jCount<10000 ; jCount++ ){
>11     for ( kCount=0 ; kCount<10    ; kCount++ ){
>12      *(arrayPtr + iCount*10000 + jCount*10 + kCount) = 0.0;
>13//    *arrayPtr++ = 0.0;

>Obviously this code initializes an array by using a pointer.
>By using "line 12" where the pointer position is
>calculated each loop, the memory use of the executable file is
>low (appr. 20 MB). However, when I use "line 13" instead of
>"line 12" (change the comment signs) the memory use of the
>executable file increases drastically (appr. up to 100 MB).

Consider your program.  If you've actually used all the memory you declare,
you would use 10000 * 1000 * 10 floats, or 100 million * 4 bytes = about
400 megabytes.  If your program is using less than that, you obviously
have a bug!

So, the case where your program uses only 20 MB must display some bug.
Sure enough, your multipliers are wrong.  Check the numbers again, and
verify that the constants you multiply by are correct.

As another comment, I would highly recommend that you use #define rather
than writing "10000" in 3 different places.  With #define, you can
change just one spot and be sure you haven't missed any; if you have to
change the variable in 3 different place you are sure to be bitten by a
typo.

Quote:
>Does anybody know the reason of this behaviour?
>Is this the problem of how the compiler maps the executable
>into the RAM?

It turns out that Linux doesn't allocate physical RAM (the value shown
by top under "RSS") until your program actually touches it (by setting
it to 0.0).

Quote:
>(A trick would be to increase the RAM, but I want to understand
>the difference in using the two different pointer usages with respect
>to the memory!)

You have a bug.  Once you fix the bug, your two versions will have the
same memory requirement.

-andy
--



Sun, 16 Oct 2005 07:20:58 GMT  
 pointer to array - memory usage
On 29 Apr 2003 15:46:06 GMT,

Quote:
> Dear Reader,

> I have a question with respect to the used RAM
> during execution of the following compiled C-code
> (to ease my description I put line numbers):

> -------------------- begin code -------------------
> 1 #include <stdio.h>

Why did you include this header?

Quote:
> 2 #include <stdlib.h>

> 3   float array[10000][1000][10];

You're asking for an object with 100000000 * sizeof(float) bytes.
There is no guarantee that that is possible. On my platform that
translates roughly to 381 MB. Also see later comments.

You know that all elements of this array will already be initialised
to 0.0, right?

Quote:
> 4   float *arrayPtr;
> 5   int   iCount, jCount, kCount;

> 6 int main ()
> 7 {
> 8   arrayPtr = &array[0][0][0];

> 9    for ( iCount=0 ; iCount<1000  ; iCount++ ){
> 10    for ( jCount=0 ; jCount<10000 ; jCount++ ){

You have the boundaries for iCount and jCount mixed up.

Quote:
> 11     for ( kCount=0 ; kCount<10    ; kCount++ ){

> 12      *(arrayPtr + iCount*10000 + jCount*10 + kCount) = 0.0;

Where the 10000 here is, of course (10 * 1000), not the 10000 from the
first index. It's potentially confusing when you use numbers like
this.

Quote:
> 13//    *arrayPtr++ = 0.0;

Why are you using a pointer anyway? Why not simply (assuming you fix
up the boundary conditions in the loop conditionals):

array[iCount][jCount][kCount] = 0.0;

Much more readable (and maintainable) than the calculation, and more
natural than the pointer increment.

Quote:
> 14     }
> 15    }
> 16   }

> 17 return 0;
> 18 }
> --------------------  end code  -------------------

> Obviously this code initializes an array by using a pointer.
> By using "line 12" where the pointer position is
> calculated each loop, the memory use of the executable file is
> low (appr. 20 MB). However, when I use "line 13" instead of
> "line 12" (change the comment signs) the memory use of the
> executable file increases drastically (appr. up to 100 MB).

I'd say that this is an implementation detail of your implementation,
or maybe it has to do with the fact that you got your loop boundaries
wrong, which means that when you position the pointer yourself, you
actually don't initialise all elements. Maybe your implementation does
something nifty with large objects like this, and doesn't actually
claim memory for it from the OS until you address elements. You should
maybe ask about that in a group that talks about your implementation.

For me (after fixing the relevant bits) there is no difference.

Martien
--
                        |
Martien Verbruggen      | The four hor{*filter*} of the apocalypse are
Trading Post Australia  | called Abort, Retry, Ignore and Fail.
                        |
--



Tue, 18 Oct 2005 15:27:06 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Thanks: pointer to array - memory usage

2. Thanks: pointer to array - memory usage

3. Array memory usage

4. Pointers, return values, memory usage.

5. Dynamically allocated array of pointers and their usage.

6. Free memory, memory usage

7. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

8. Array of pointers, pointer to array...

9. array pointer/pointer array

10. arrays pointers array of pointers

11. pointer-to-pointer usage

12. pointer to array - physical memory

 

 
Powered by phpBB® Forum Software