This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Compiler/MSP430F5419A: how struct is compiled in c++ program?

Part Number: MSP430F5419A


Tool/software: TI C/C++ Compiler

I have some questions about the compilation of structs.

the main problem is that i want yo make a method to copy a value to a struct  through a pointer and it can't be done.

the function have three arguments like this.

int ReadRegister (unsigned char *Buff, int start_register, unsigned int count)

i want to put the read value to a struct wich is pointed by Buff. may i can clarify my doubts with a fragment of code:

typedef struct
{ 
  unsigned char Family_Id;
}InfoId_t;


int ReadRegister (unsigned char *Buff, int start_register, unsigned int count) 
/*----------------------------------------------------------------------------*/
/** \brief
    Reads a register from the device as many bytes as you would like.
*******************************************************************************/
{
    BuffT[0] = start_register & 0xff; //LSB
        BuffT[1] = (start_register >> 8) & 0xff; //MSB

    Write(TACTI2C_SLVADDR, BuffT, 2); // This write register you want to read

    Read(SLAVEADDRESS, RxBuff, count) 

         for(int i=0; i<count; i++)
          {
            *Buff = RxBuff[i];
             Buff++;
          }

    return OK;  
}
void main()
{
 InfoId_t info;
unsigned char *ucRxBuff;
ucRxBuff = &info.Family_Id

error = ReadRegister (ucRxBuff, 0x00, sizeof(InfoId_t));
 return 0;
}



the problem in here is that the values are read correctly but when i put the value passing through by pointer info.Family_Id has no value associated.

anyone knows how is this posible? i mean, i suspect that the compiler have some part of fault but but i am not sure. The values are read correctly because i can see making debug all the values in an array
putted inside the fucntion read(), but when i go out the method the value are not copied to info.Family_Id.

Regards



  • Buff is a pointer to a single unsigned char. Why are you incrementing it and stomping all over the memory next to it?
  • Hello Kernel,

    A few suggestions here.

    I would stick to C and not C++ in MSP430 programming. C++ tends to add too much overhead for these relatively small memory sized devices.

    This maybe a simplified scenario, but with the example you gave, you don't need a struct with only one variable here.

    As Keith alluded to, by incrementing the pointer here, you could be you could be changing adjacent memory. It would be better to index buff as an array. In that same way, whatever pointer you send to the function, needs to be a pointer to an allocated array. The compiler doesn't know how much space you need for this buffer, so you need to allocate it at the start of your program. Otherwise, the compiler/linker could place other variables/data/code beside the pointer.

**Attention** This is a public forum