Malloc problem

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

Malloc problem

Jump to solution
1,289 Views
danielecortella
Contributor V

Hello,

i have this function:

IOTHUB_CLIENT_CONFIG* config = malloc(sizeof(IOTHUB_CLIENT_CONFIG));

        if (config == NULL)

        {

            /* SRS_IOTHUBCLIENT_LL_12_012: [If the allocation failed IoTHubClient_LL_CreateFromConnectionString  returns NULL]  */

            LogError("Malloc failed\r\n");

            return NULL;

        }

and always the malloc return a null pointer. Why? Isn't the code correct?

I didn't use MQX or FreeRTOS

Thanks

0 Kudos
Reply
1 Solution
1,013 Views
danielecortella
Contributor V

I have solved,

the heap size was 0 so i have set it to a different  value.

Thanks

View solution in original post

0 Kudos
Reply
5 Replies
1,014 Views
danielecortella
Contributor V

I have solved,

the heap size was 0 so i have set it to a different  value.

Thanks

0 Kudos
Reply
1,013 Views
danielecortella
Contributor V

Hello,

i have take this sample from here http://www.cplusplus.com/reference/cstdlib/malloc/  only for be sure that there isn't a my error. I have to use the malloc because is used in a library (not mine) that i need to use. I didn't find any information in this forum, normally the malloc library is include

0 Kudos
Reply
1,013 Views
Kan_Li
NXP TechSupport
NXP TechSupport

Hi Daniele Cortellazzi,

What IDE are you using? and which part of Kinetis are you using ? Would you please help to clarify? Thanks for your patience!


Have a great day,
Kan

NXP Technical Support
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos
Reply
1,013 Views
danielecortella
Contributor V

Hello,

i'm using KDS3.0 with a kl46. If i try this code:

buffer = (char*) malloc (i+1);
  if
(buffer==NULL) exit (1);

same problem, null pointer are returned.

Thanks

0 Kudos
Reply
1,013 Views
bobpaddock
Senior Contributor III

What is the value is 'i'?

Are you sure that malloc is actually implemented in the library that you are using?

Some libraries actually only return NULL as it is not really implemented.

There may be a library flag in the IDE or Makefile that gets a real malloc.

Also check the linker script.  Is there really a Heap for malloc to draw memory from?

In general malloc in embedded systems is a bad idea and why MISRA discourages its use.

0 Kudos
Reply