Hello,

I have observed a data access misaligned address violation during the following data type casting.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
     float mul[19013];
     unsigned int ch_sz=76052, i;
     mul[0]=0;
     for(i=1; i<(ch_sz/4); i++)
     {
	mul[i]=mul[i-1]+(1/(float)(ch_sz/4));
	printf("%f\t", mul[i]);
    }
    return 0;
}

Please help me isolate the issue and address the data alignment violation. I believe the possible reason could be that unsigned int data type is 16 bits and the float data type is 32 bits, and hence could be creating problems, but I'm not completely sure. If so, could anyone suggest a work around the issue.

Thank you.

I have a GNU GCC compiler. Making the concerned variables global, did the trick, but out of curiosity, how did it resolve the issue?
Thank you.

What is your compiler/build target? Have you tried making the array a global?

I have a GNU GCC compiler. Making the concerned variables global, did the trick, but out of curiosity, how did it resolve the issue?

From ages ago, I've assumed that stack sizes > 64KB (DOS days on Borland C++?) might be troublesome. I don't use *nix, but I think I've heard that this is more of a Windows thing. I just try to avoid big stacks out of general defensiveness.

This article has been dead for over six months. Start a new discussion instead.