Problems with using float malloc()

HI

chip : NRF52832

tool : segger embedded studio

SDK : 17.0.0

I have a problem using malloc().
I simply tested it like this:

#define ds_len 500

float* test_data= (float*)malloc(sizeof(float)*ds_len);

for(i=0; i<ds_len;i++)

{

      test_data[i] = 99;

      sprintf(ch, "%.2lf",test_data[i]);

      println(ch);

}

for(i=0;i<ds_len;i++)

{

      sprintf(ch, "%.2lf",test_data[i]);

      println(ch);

}

I allocated memory as a float, saved the data and read it back.
However, the values of the saved data and the output data are different.

Allocating memory with 'int' works normally.
please check.

Parents Reply
  • Hi

    I am not able to run your project. Nonetheless, upon inspection I can see that you are using the same code in the main.c file as you have mentioned in your original post.

    However, I am able to allocate floating point variables using malloc and then store values in them and then read and display (see the snapshot below). However, as I mentioned earlier, I was getting issues of printing floating point variables, so I have reverted to using NRF_LOG_FLOAT macro to print floating point on the console.

    You can see in the code that the temp is a floating point variables which should contain the values read from the allocated memory for float (that is from the test_data).

    Regards,

    Naeem

Children
Related