Array passed to function changes despite const qualifier

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Array passed to function changes despite const qualifier

3,031 Views
tristanhegseth
Contributor II

I am having a weird problem where my array of samples (uint16_t) will get altered after data processing. I tried the const qualifier, I tried malloc and memcpy, I tried dummy variables, yet the original sample array still gets affected. The data processing function is a low pass filter, which in this case is operated similar to a moving weighted average. It multiplies some coefficient with the samples around the current sample and sums them together. The only reason I can think of from this function that might change this array is memory being overwritten, but I cannot find out how. I am using the LPCLink2 with the LPC4370 chip. Here is the convolution function.


static void convolute(uint16_t data[SAMPLES_PER_PULSE], const uint16_t const sample[SAMPLES_PER_PULSE]) {

for (int i = 10; i < SAMPLES_PER_PULSE; i++) {

data[i] = coefficients[0] * (sample[i+1] + sample[i-9]) + coefficients[1] * (sample[i+0] + sample[i-8]) + coefficients[2] * (sample[i-1] + sample[i-7]) + coefficients[3] * (sample[i-2] + sample[i-6]) + coefficients[4] * (sample[i-3] + sample[i-5]) + coefficients[5] * (sample[i-4]);

}
for (int i = 0; i < 10; i++) {
data[i] = data[10];
}
}

 

I setup my code to only display the samples array after data processing is done. I run my program with and without this function being called and without this function the samples are their expected values. With this function they are unpredictable and wrong. My full code is included as a file below

0 Kudos
Reply
2 Replies

3,027 Views
ErichStyger
Specialist I

Hi @tristanhegseth ,

'const' in a parameter list only marks things non-writable (that you cannot change the value from the C code), nothing more.

Your code is helpful, but is not complete (cannot be compiled without lots of other things).

What I notice is that you allocate lots of data on the stacks: SAMPLES_PER_PULSE is 256, so for example for update_stats() alone you need more than 1 KByte of stack. Do you have enough stack space allocated? Otherwise your stack might run over and change the data. Use the MCUXpresso Image Info (see https://mcuoneclipse.com/2019/06/16/new-nxp-mcuxpresso-ide-v11-0/ ) and check the costs/size in the 'Call Graph' tab how much stack you need. It could be helpful if you allocate the two arrays used statically (global memory) to narrow down the issue.

Have you used watchpoints? If you know the data changed, you can set a watchpoint there, see https://mcuoneclipse.com/2012/04/29/watchpoints-data-breakpoints-in-mcu10/

I hope this helps,

Erich

0 Kudos
Reply

3,029 Views
tristanhegseth
Contributor II

I was able to fix my issue by disabling timer0 when doing the data processing. The timer interrupt was interrupting the data processing in the middle and rewriting to the samples array before it was done processing