AnsweredAssumed Answered

Compiler Code Optimization - - On accessing a Array in a structure vs a simple array

Question asked by arbitrer on Oct 14, 2015
Hi
I want to know whether there is penalty on accessing an array from a structure vs accessing by a simple array.

Here is my snippet which i used when I faced the problem
unsigned char array_1[10240];
unsigned char array_2[10240];
typedef struct
{
  unsgined char array_2[10240]
}simple_queue;
   
typedef struct
{
  simple_queue simple_arr[3]
}simple_stack;
  
simple_stack  struct_var1;
... ....
 memcpy(char array_2,  char array_1, 10240 );
  
 memcpy(&struct_var1.simple_arr.array_2[0],  char array_1, 10240 );
....

So I could see that first memcpy() took 90microsec, while the second mempy() took 310microsec.

I changed the unsigned char to
int and I could see that each memcpy() took 80 microsec.

So I assume this is related to compiler optimization..

Can someone please explain to me how compiler optimization can lead to this behaviour?

Outcomes