Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Moving Data in Memory Part II




On Oct 22, 2004, at 2:15 PM, Ian Ollmann wrote:


On Oct 22, 2004, at 1:53 PM, Gohara, David wrote:

Hi All,

  Two memory related questions.  One is 2D and the other is 3D.

  The 2D question.  In a 173x173 data array I get the following memory addresses for y,x combinations:

86,86 6bc89d0
73,85 6be3a4c
74,81 6be3cF0
68,82 6c00060

  The address jump from 86,86 to 73,85 doesn't make sense to me (although it's consistent for the 73,85 to 74,81) according to my calculations (which in all honesty could be very wrong).  To get the addresses I used printf("ptr %x\n",ptr);  Any ideas what I've done wrong?  The answer after all the computing is correct, but these addresses seem wrong.

  Regarding a 3D array this is similar to my last question regarding transposing a 2D array.  Except this time I was wondering if it's possible to do this across the third dimension (that is all values in the third direction would become a stride of 1).  I thought about this a while now, but I've now gotten myself completely confused and am asking for help.  Previously I took the easy way and used mtrans() as was kindly suggested.

It seems likely that the elements in your array are 4 byte quantities, the row (or column in FORTRAN) is 173*4 bytes wide (692 bytes). So to get from 73,85 to 74,81, you add 74-73=1 row (or column) and add 81-85= -4 elements: 


    delta = 1 row * 692 bytes/row + -4 * 4 bytes/element = 676 bytes (0x2a4)
 
6be3cF0 - 6be3a4c = 0x2a4

If this logic doesn't extend to other rows, it may be because the array has been allocated as an array of pointers to 1D arrays. If the 1D arrays were allocated with malloc() then they could be all over the map. Having row 73 be at a higher address than row 86 suggests that this is what is going on. 


It can be inefficient to allocate arrays this way due to the cost of multiple calls to malloc and some wasted heap space. If you have code to dynamically allocate multidimensional arrays, it would probably be best to allocate a big 1D array that is height*width*sizeof(element)+rows*sizeof(element*) in size, then set up the array of pointers to point to places within that large allocation.

Ian
_______________________________________________
Do not post admin requests to the list. They will be ignored.
PerfOptimization-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/perfoptimization-dev/email@hidden

This email sent to email@hidden
References: 
 >Moving Data in Memory Part II (From: "Gohara, David " <email@hidden>)
 >Re: Moving Data in Memory Part II (From: Ian Ollmann <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.