Malloc problem

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 
1,821件の閲覧回数
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 件の賞賛
返信
1 解決策
1,545件の閲覧回数
danielecortella
Contributor V

I have solved,

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

Thanks

元の投稿で解決策を見る

0 件の賞賛
返信
5 返答(返信)
1,546件の閲覧回数
danielecortella
Contributor V

I have solved,

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

Thanks

0 件の賞賛
返信
1,545件の閲覧回数
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 件の賞賛
返信
1,545件の閲覧回数
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 件の賞賛
返信
1,545件の閲覧回数
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 件の賞賛
返信
1,545件の閲覧回数
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 件の賞賛
返信