LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a way to get aligned memory

Solved!
Go to solution

I usually use malloc() to get memory. But this time I have to get an aligned memory.

Is there a  function call I can directly use to allocate aligned memory? Otherwise I will have to write one by myself.

 

Any suggestions would be appreciated.

0 Kudos
Message 1 of 3
(3,309 Views)
Solution
Accepted by topic author buddyLV

buddyLV,

Unfortunately, there is no ANSI C or CVI function which will do this automatically. However, you should be able to implement this yourself. At a high level, you should be able to do the following (assuming that the size of data you are interested in is always smaller than your page size):

 

  1. Allocate enough memory to guarantee that your data can fit onto one page. In worst case, a standard allocation would contain all but the last byte on the first page of memory. As a result, you would have dataSize-1 bytes on the first page, but still need dataSize bytes worth of memory on the second page. Allocating 2*dataSize - 1 bytes of memory will guarantee that all of your data will be able to fit on either the first or second page.
  2. Using the first address of memory that you allocated, compute the first address of the next page of memory.
  3. Using the first address of your allocation and the first address of the next page, determine if your data should be placed at the first address of allocation or the first address of the next page.
  4. Use the address determined in step 3 for storing and accessing your data, and use the first address of the allocation when you eventually free the memory.

 


I have found the following discussions on the topic which may be helpful for you as you impmlement this:
http://bytes.com/topic/c/answers/216430-alligned-memory-allocation
http://stackoverflow.com/questions/227897/solve-the-memory-alignment-in-c-interview-question-that-st...

 

 

- John

National Instruments
Applications Engineer
0 Kudos
Message 2 of 3
(3,258 Views)
Thanks. That's exactly what I plan to do.
0 Kudos
Message 3 of 3
(3,237 Views)