Here is my code, I'm almost new to coding and working on my first embedded task, seriously gonna need some help. Thanks in advance.
1 int *xData;
2 char Pixels[246];
3 uint8_t data[128];
4 char* Pixel_data;
5 while(1)
6 {
7 xData= (int*)malloc(sizeof(int));
8
9 ReadSamples(data);
10 int j=0;
11 for (int i=0;i<=127;i+=2)
12 {
13
14 *xData = data[i] + data[i+1];
15 temp_c[j] = (*xData)*(mult);
16 free(xData);
17 j++;
18 }
19 Pixel_data = Create_heatmap(temp_c); //Create_heatmap returns char* ptr of size 246
20 memcpy(Pixels, Pixel_data,246*sizeof(char) + 1);
21 //Transfers the whole converted data
22 HAL_UART_Transmit(&huart1,(uint8_t*) Pixels, sizeof(Pixels), 100);
23 free(Pixel_data);
24 }
- in
line 7
I'm creating a memory forxData
usingmalloc
, Inline 14
I'm feeding some data to it, Inline 16
I'm freeing it usingfree
, I'm freeing thexData
in one iteration offor
loop only but when I see the output as expected. How isxData
getting filled even after freeing the memory allocated? - Can I get better explanation for
line 20
, I havepixels
with size 246 and I'm copying246+1
it's working only with 247, if not 247 a byte of data is missing at end. Why is this? - At
line 9
, I'll be getting 64 readings from a sensor each in12bits
and stores asdata[0] = 8 bits
anddata[1] = 4 bits
. This may befloat/int
. I know that what I'm doing is wrong in 'line 14`. How to overcome this?