This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Problem with more member in a structure

Hi there,
I'm using STM32F103 with Keil ver 4.72
I defined a structure like this:

typedef struct
{
 uint16_t X;
 uint16_t Y;
 uint16_t Width;
 uint16_t Height;
 uint16_t BackColor;
 uint16_t TextColor;
 uint16_t TextBackColor;
} button_t;


I use the struct in main file like this:


button_t btn1;

int main()
{
 btn1.X = 100;
 btn1.Y = 100;
 .
 .
 .
 while(1)
 {
 }
}


When I use this struct in program, every things works well.
The problem is when I add more members into the structure, like this:

typedef struct
{
 uint16_t X;
 uint16_t Y;
 uint16_t Width;
 uint16_t Height;
 uint16_t BackColor;
 uint16_t TextColor;
 uint16_t TextBackColor;

 uint16_t TLX;
 uint16_t TLY;
 uint16_t TRX;
 uint16_t TRY;
 uint16_t BLX;
 uint16_t BLY;
 uint16_t BRX;
 uint16_t BRY;
};


When I do that, program compiles without any problem, but when I download it into the MCU, it doesn't work.
I increased Heap Size from 0x0200 to 0x0800, but took the same result. I set the Heap Size in startup.s file. I'm not sure if it's a right way to set that.

Parents
  • Hello,

    char *Text;

    allocates enough memory for a POINTER, not for the string.
    If you do

          Button1.Text[0] = 'A';
          Button1.Text[1] = 'g';
          Button1.Text[2] = 'h';
          Button1.Text[3] = 'H';
          Button1.Text[4] = NULL;
    


    and I assume the pointer is 32bit, access to [0].[3] might not do what you want, but also does not corrupt the memory. But Button1.Text[4] would destroy some memory.

    do

    typedef struct
    {
            uint16_t LocationX;
            uint16_t LocationY;
            uint16_t SizeWidth;
            uint16_t SizeHeight;
            uint16_t BackColor;
            uint16_t TextColor;
            uint16_t TextBackColor;
    
    //      uint16_t PosTLX;
    //      uint16_t PosTLY;
    //      uint16_t PosTRX;
    //      uint16_t PosTRY;
    //      uint16_t PosBLX;
    //      uint16_t PosBLY;
    //      uint16_t PosBRX;
    //      uint16_t PosBRY;
    
            char Text[20]; // max. required number of charactern required in app
    } button_t;
    

    instead

Reply
  • Hello,

    char *Text;

    allocates enough memory for a POINTER, not for the string.
    If you do

          Button1.Text[0] = 'A';
          Button1.Text[1] = 'g';
          Button1.Text[2] = 'h';
          Button1.Text[3] = 'H';
          Button1.Text[4] = NULL;
    


    and I assume the pointer is 32bit, access to [0].[3] might not do what you want, but also does not corrupt the memory. But Button1.Text[4] would destroy some memory.

    do

    typedef struct
    {
            uint16_t LocationX;
            uint16_t LocationY;
            uint16_t SizeWidth;
            uint16_t SizeHeight;
            uint16_t BackColor;
            uint16_t TextColor;
            uint16_t TextBackColor;
    
    //      uint16_t PosTLX;
    //      uint16_t PosTLY;
    //      uint16_t PosTRX;
    //      uint16_t PosTRY;
    //      uint16_t PosBLX;
    //      uint16_t PosBLY;
    //      uint16_t PosBRX;
    //      uint16_t PosBRY;
    
            char Text[20]; // max. required number of charactern required in app
    } button_t;
    

    instead

Children