LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   "warning: Cast increases required alignment of target type [-Wcast-align] (https://www.linuxquestions.org/questions/linux-newbie-8/warning-cast-increases-required-alignment-of-target-type-%5B-wcast-align%5D-4175436006/)

lohith.nayak 11-06-2012 11:31 PM

"warning: Cast increases required alignment of target type [-Wcast-align]
 
Hello,

I am porting code from (x86, ppc) platform to ARM-15.
It uses LINUX os, GCC compiler.
Goal is to write platform independent code. When I compile my code (on ARM), I am getting following warning,
"Cast increases required alignment of target type [-Wcast-align]".

Code looks like this.

typedef struct s_1{

u32 a:24;
u32 r_1:8;
u32 b:25;
u32 r_2:2;
u32 c:3;
u32 r_3:2;
u32 r_4:16;
u32 d:16;
u32 r_5:32;
u8 arr[32];
} s_1_t;


typedef struct s_2 {
u32 a;
u32 b;
u16 c;
u16 d;
u16 e;
u8 f;
u8 g;
u8 h;
u8 i;
} s_2_t;



function(s_2_t * ptr)
{
s_t s_t_temp;

*(u32 *) &s_t_temp.arr[0] = 0;
*(u32 *) &s_t_temp.arr[4] = 0;
*(u32 *) &s_t_temp.arr[8] = 0;
*(u32 *) &s_t_temp.arr[12] = 0;

*(u32 *) &s_t_temp.arr[16] = htonl((ptr->f <<24) | (ptr->g << 16) | (ptr->h << 8 ) | ptr->i));

}

I am getting warning on each assignment statements.

I tried using "__attribute__ ((aligned (4))) " at arr[32]. But still getting this warning.

Please let me know if I am posting a thread in wrong place.
Could you please let me know other options to resolve this???

Thanks in advance,

druuna 11-08-2012 08:02 AM

@lohith.nayak: You've marked this as SOLVED, but you don't provide an answer. Can you provide the solution so others can benefit from it?

Anyway: Off the zero-reply list.

johnsfine 11-08-2012 08:29 AM

Quote:

Originally Posted by lohith.nayak (Post 4823856)
...
typedef struct s_1{
...
} s_1_t;
...
s_t s_t_temp;
...

1) When posting code, please use CODE tags to make it more readable.

2) Try harder to make the portion of code you post consistent with the code you tested when getting the symptoms you reported.

In this case, you seem to have messed up the name of that type (s_1_t vs. s_t) in whatever edits you thought were necessary to cut down from the code you tested to the portion you wanted to post.

It is best to cut down to a small complete program that demonstrates the same symptom. Anything complete enough to let others test and duplicate the same symptom is a much better basis for us giving you a good answer.

lohith.nayak 11-08-2012 09:19 PM

Hello,

Ok. I will use CODE TAGS while posting the code.

I used workaround solution.

Code:


S_t s_t_temp;
u32 * ptr = (u32 *)((void *)(&s_t_temp.arr[0]));

/**(u32 *) &s_t_temp.arr[0] = 0;*/
    ptr[0] = 0;
/* *(u32 *) &s_t_temp.arr[4] = 0;*/
    ptr[1] = 0;
/* *(u32 *) &s_t_temp.arr[8] = 0;*/
    ptr[2] = 0;

/* *(u32 *) &s_t_temp.arr[12] = 0;*/
    ptr[3] = 0;

/* *(u32 *) &s_t_temp.arr[16] = htonl((ptr->f <<24) | (ptr->g << 16) | (ptr->h << 8 ) | ptr->i));*/
    ptr[4] = htonl((ptr->f <<24) | (ptr->g << 16) | (ptr->h << 8 ) | ptr->i));

Though it solved the warning, I believe its not an efficient code as casting reduces the performance.
I read in some article its good to define u32 in struct member and then casting it back to u8 whenever is necessary.
If anybody suggest any solution, I would be very thankful.

Thanks.


All times are GMT -5. The time now is 08:07 AM.