0

I am working on one Embedded C project. I am having trouble understanding the use of array and structure in code.

for example:

Structure:

struct example_struct{
 char a;
 char b;
 char array[4];

}  

Note: The default size for Int and char is 1 byte.

The compiler I am using provide the functionality of memory allocation for variables and other parameters using '@' symbol

for example:

Memory Allocation

int example @ 0x125
// Compiler allocate the RAM memory location 0x125 to my variable "example"  

Issue:

The person who coded this project have used the structure and array as given below

example.h

struct example_struct{
char a;
char b;
char array[4];
}  

Memory.h

volatile struct example_struct node @ 0x130 ;
//allocate memory location 0x130 - 0x135 to node  
volatile char buffer[6] @ 0x130;
//allocate memory location 0x130 - 0x135 to buffer 

Question

1.Instead of using the pointer to access the member of structure is it appropriate to use the array placed on same memory location?

  1. Does it cause the memory issue?

Would you please help me to understand the use of stuct and array in this particular situation.

Thank you

Kunal

8
  • This is very similar to friend/union - at least thats how it looks like to me. BTW - very handy mnemonic to be able to pick where to initialize values. Commented Feb 22, 2016 at 15:05
  • "Does it cause the memory issue?" -- what is the actual issue? Commented Feb 22, 2016 at 15:10
  • The available memory is 367 bytes and similar to this patter, three different structures are used in my code. (each 32 bytes) Commented Feb 22, 2016 at 15:20
  • If I am declaring any global variable and watching it using debugger I can see the change in value. Even if I am not using them anywhere. Commented Feb 22, 2016 at 15:22
  • I don't understand that comment -- also, could you elaborate on if there is a special significance to address 0x130 -- is it a hardware address or IO controller of some kind? Commented Feb 22, 2016 at 15:23

4 Answers 4

2

The (weird) coder that developed that code was "simulating" an union to be able to access the same location as struct example_struct or byte per byte.

Like:

#pragma pack(1)
union struct_and_raw_byte_access
{
   struct example_struct
   {
      char a;
      char b;
      char array[4];
   }structured;
   char buffer[sizeof(struct example_struct)];
};
#pragma pack()

int main(void)
{
    union struct_ad_raw_byte_access temp;
    int i;

    temp.structured.a = 1;
    temp.structured.b = 2;
    temp.structured.array[0] = 3;
    temp.structured.array[1] = 4;
    temp.structured.array[2] = 5;
    temp.structured.array[3] = 6;

    for (i=0; i< sizeof(temp.buffer)/sizeof(temp.buffer[0]); i++)
        printf("buffer[%d] = %d\n", i, temp.buffer[i]);

    return 0;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Unions does not guarantee any particular packing of the bytes, so that should not make a difference.
@Soren I'm not getting you. Could you elaborate?
You have done nothing to accommodate for padding -- the compiler is allowed to add padding bytes between the a, b and the array in the strctured part of of the union -- since it is likely (guess) that the OP has the struct at address 0x130 due to the fact that it is an IO address of some hardware -- then the exact size of the strct is important -- I.e. it must be 6 bytes exactly
@Soren this is exacly why I wrote #pragma pack(1), to ensure 1 byte padding.
2

1.Instead of using the pointer to access the member of structure is it appropriate to use the array placed on same memory location?

2.Does it cause the memory issue?

IMHO you cannot do that. It may work in some archs, but keep in mind that you are trusting the compiler to put everything packed and aligned.

Some compilers have packing directives (pragmas or parameteres), but it is much more safer to use an union.

Comments

1

Make sure that buffer is also a pointer, so allocating "buffer" to absolute address is the same like make a pointer to that address.

volatile char buffer[6] @ 0x130;
//allocate memory location 0x130 - 0x135 to buffer 

In your case, developer; I think; he decided to access strcutre elements using array for work with them in loop or something. So, answering your questions:

  1. No, It is not appropriate, as you said, it is better to make pointer and access this pointer by casting it to (char *)
  2. If you know how you will use "buffer", no memory issue will happen. I mean make sure no out of index will happen.

Comments

1

Your comment "if I am declaring any global variable and watching it using debugger I can see the change in value. Even if I am not using them anywhere" probably means that you are looking at a hardware register of some IO controller.

The

volatile char buffer[6] @ 0x130;

Is intended to map those IO controller addresses to a variable name in your embedded programming. That is the IO controller is presenting status and data at memory address 0x130 to 0x135 -- and the C program maps this to a symbolic value through the declaration.

Since the format of this area is determined by the IO controller (nothing you can do in your C code will change the format), you need to make sure that the strct and the example_struct is exactly 6 bytes long, or if not you have some padding which will kill you.

Test this using your debugger (if it is gdb use can use sizeof, but your debugger may differ)

Also, I recommend that you find some kind of documentation on what is on hardware address 0x130

1 Comment

The memory location I have specified is the Data Memory for my controller. This memory lies in between 0x000h - 0x01ff h. As I define any global variable compiler allocate the data memory between this range. As these are not the IO register the value of any variable should not change until unless updated by any function or part of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.