Part Number: MSP430FR2433
Other Parts Discussed in Thread: MSP430FR2633
Tool/software: TI C/C++ Compiler
Hi Compiler team,
Using MSP430 compiler V20.2.2.LTS, optimization = off, MSP430FR2633
I have an interesting problem that is most likely me doing something wrong. I have an application where I use a 2D array of pointers to structures that hold measurement values and an algorithm navigates through the array to read measurements. I simulate that with the code shown. What I am seeing is the first row is de-referenced correctly, but subsequent rows are not. I have included a simple example in main.c that demonstrates the issue.
Also, in my sensor struct if I use assignment .pElements = Elements, I get a compiler warning, where as if I use what is shown in the code below, no warning, but in either case the results are wrong.
=========================
#include <msp430.h>
#include <stdint.h>
typedef struct
{
int16_t value;
}tElement;
typedef struct
{
tElement** pElements;
}tSensor;
// 1. create 12 objects representing the sensor's 12 elements
tElement a = {.value = 0};
tElement b = {.value = 1};
tElement c = {.value = 2};
tElement d = {.value = 3};
tElement e = {.value = 4};
tElement f = {.value = 5};
tElement g = {.value = 6};
tElement h = {.value = 7};
tElement i = {.value = 8};
tElement j = {.value = 9};
tElement k = {.value = 10};
tElement l = {.value = 11};
// 2. Create array of pointers to each element (used for later processing)
tElement* Elements[3][4] = {{&a,&b,&c,&d},{&e,&f,&g,&h},{&i,&j,&k,&l}};
// 3. Create the sensor and assign 2D array to pointer
tSensor sensor = {
.pElements = Elements[0],
};
// This demonstrates how the 2D array is not correctly being de-referenced starting with row 1
int main(void)
{
volatile uint16_t w,x,y,z,ww,xx,yy,zz,www,xxx,yyy,zzz;
WDTCTL = WDTPW | WDTHOLD;
// We use a pointer to our sensor in our application so use pointer here
tSensor* pSensor = &sensor;
// traverse through 2D array and verify correct values are read
w = pSensor->pElements[0][0].value; // = 0 correct
x = pSensor->pElements[0][1].value; // = 1 correct
y = pSensor->pElements[0][2].value; // = 2 correct
z = pSensor->pElements[0][3].value; // = 3 correct
ww = pSensor->pElements[1][0].value; // = 1 not correct, should be 4
xx = pSensor->pElements[1][1].value; // = 2 not correct, should be 5
yy = pSensor->pElements[1][2].value; // = 3 not correct, should be 6
zz = pSensor->pElements[1][3].value; // = 4 not correct, should be 7
www = pSensor->pElements[2][0].value; // = 2 not correct, should be 8
xxx = pSensor->pElements[2][1].value; // = 3 not correct, should be 9
yyy = pSensor->pElements[2][2].value; // = 4 not correct, should be 10
zzz = pSensor->pElements[2][3].value; // = 5 not correct, should be 11
while(1);
==========================================
When I run the code, I can then stop and examine the values. You can see starting with variable ww, the values are not correct.
Here is a memory dump of these variables
