Search code examples
microcontroller

int32_t for stack in 32 microcontroller


In bellow example why we should use int32_t instead of uint32_t ? (platform is ARM 32 bit microcontroller)

struct tcb{
  int32_t *stackPt;       
  struct tcb *nextPt;  
};

It's a part of RTOS tutorial. and tcb is for thread control block . why we should use int32_t* for stack ?


Solution

  • There is no particular reason that you should use pointer to signed rather than unsigned.

    You are probably never going to dereference this pointer directly to access the words on the stack. If you do want to access the data on the stack, some of it will be signed, some unsigned, and some neither (strings etc) so the pointer type will not help you with that.

    When you want to pass around a pointer but never dereference it then one convention is to use a pointer to void, but that convention isn't so popular in embedded system code.

    One reason to use a pointer to a 32-bit integer is to suggest that the pointer is at least word-aligned. If you intend on complying with the ARM EABI (which you should) then the stack should be doubleword (64-bit) aligned at the entry to every EABI compliant function. To hint that that is the case you might want to even use a (u)int64_t pointer. This might be misleading though because not everything on the stack is 64-bit or 32-bit aligned, just the whole frames.