gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > Memory Alignment

#174162 - JackUzi - Fri May 21, 2010 4:20 am

I'm now working on the presumption that a memory alignment problem is causing my program to hang (although I'm not really sure it seems logical given it only hangs on ARM).

From what I've read the basic idea to make sure that each chunk of RAM falls on a 32bit/4 char boundary. Is that right?

Anyway, in my code I have been changing all char arrays and char mallocs to use a multiple of four. What I'm now worried about are all those string literals. For example:

Code:


function(char *blah) {
puts(blah);
}

function("TEST");


I'm no C expert, but it is my understanding that the literal TESTING will be allocated on the fly resulting in a five char chunk of RAM (with the terminating null). Does this mean that every time I use a string literal I have to do something like this: "TEST\0 " ie, 4 characters, null, two spaces, then the system added null to make a total of eight bytes?

Stuart

#174163 - elhobbs - Fri May 21, 2010 4:40 am

No. Chars do not need to be aligned. Shorts need to be aligned on 2 byte boundary and ints need to be aligned to a 4 byte boundary. As I indicated before this is handled by the compiler except in situations where you force the compiler to do something - like cast a char array to be an array of integers or force structures to be byte aligned.

#174166 - JackUzi - Fri May 21, 2010 7:28 am

Thanks for the info. Sorry, I missed your reply to the other post and essentially asked something you had already answered - I misinterpreted the topic list.

Thanks for the lid closing tip too, that is a good one to know. I just tried it then and the screen never goes off so it looks like good old fashioned crash.

Stuart