Ok so this thread is predominantly because I have a problem that may-or-may-not be more complicated than a blog, but it's really open to any discussion about computer science-y things.
I've been working in C for a little while, and I'm currently playing around with SDL (media library, primarily target at game development). I'm kind of teaching myself how to do a few things as I go, using the Quake 3 source code and Stachexchange as a general points of reference. I've just written a memory manager of sorts, and spent the last 3 days debugging it. I discovered that I'm totally fucking stupid, but I almost have it working.
I think I'm still having a few issues with it but I'm not really sure why.
(Sorry in advance for the images, I need to find a way to turn off C++ syntax highlighting for my pure-C)
Basically, I have a struct "new" with a field
uint32_t pixels. New represents an image to be drawn to the screen, and pixels is its raw image data. At the moment I am trying to fill
pixels with a colour
uint32_t col, in this case 0xFF00FFFF.
Width and
height are both equal to 40, so the loop runs for i < 1600. Whenever I try to run this I get a segmentation fault when i = 3. All of the memory in this example is preallocated into a "block" when the program starts, and I check to make sure that the section allocated for pixels isn't NULL. At this point it's probably worth mentioning that the block is allocated as a void pointer, then cast to a
uint32_t*, which might have something to do with the problem somewhere up the line.
I tested to make sure that nothing had gone wrong in the block, and that there was enough space actually there for 1600
uint32_t's.
If it can be iterated through and read every value in
pixels then I should be able to write to them using that same method, right?
This freaked me out at first, but from the look of the dissasembly it appears that the second printf() gets shuffled down to after the allocation. Is it possible that could be significant?
Basically I'm thinking this has to be a memory alignment error from casting out of a void pointer. Am I correct? Given that
pixels comes from the start of a block I don't really understand how this happened, but is it possible that, because the data was allocated to a void pointer it started life out of alignment for a uint_32?
Can anyone give me a blanket-idea of what could have happened here? Alternatively, you could all ignore me. I could probably post this somewhere better but I'm too lazy to make a new account anywhere.
*Implicit casting is still casting.