Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

ahuo-dev
Copy link

@ahuo-dev ahuo-dev commented Jan 2, 2025

Add malloc.h into StandardCLibrary.h. Some C code include malloc.h for malloc func.

@thetic
Copy link
Contributor

thetic commented Jan 2, 2025

Per the standard, malloc is declared in stdlib.h1 which is already included.

Footnotes

  1. https://en.cppreference.com/w/c/memory/malloc

@basvodde
Copy link
Member

basvodde commented Jan 2, 2025

@ahuo-dev Could you share what environment you were trying to solve what problem, then we can look at whether this is the best fix.

@ahuo-dev
Copy link
Author

ahuo-dev commented Jan 2, 2025

Per the standard, malloc is declared in stdlib.h1 which is already included.

Footnotes

  1. https://en.cppreference.com/w/c/memory/malloc

@ahuo-dev Could you share what environment you were trying to solve what problem, then we can look at whether this is the best fix.

@basvodde My project use amazonaws kinesis video stream code. I have a compile error when enable cpputest memory leak check . and I found amazonaws kinesis video stream code use malloc header with malloc.h. see this code line 523 at https://github.com/awslabs/amazon-kinesis-video-streams-pic/blob/master/src/common/include/com/amazonaws/kinesis/video/common/CommonDefs.h

@basvodde
Copy link
Member

basvodde commented Jan 9, 2025

Ok, but the include of #malloc.h in the CommonDef.h shouldn't need to trigger you to include it in the SatndardCLibrary.h, right?

Could you share what the compile error is that you are getting?

@ahuo-dev
Copy link
Author

Ok, but the include of #malloc.h in the CommonDef.h shouldn't need to trigger you to include it in the SatndardCLibrary.h, right?

Could you share what the compile error is that you are getting?

Compile error message :
"nclude/CppUTest/MemoryLeakDetectorMallocMacros.h:36:47: error: expected declaration specifiers or ‘...’ before string constant
36 | #define malloc(a) cpputest_malloc_location(a, FILE, LINE)"

and run command "gcc -E " to expand all macro. I see malloc function prototype expand to cpputest_malloc_location, like this :
extern void *

38 "/usr/include/malloc.h"

        cpputest_malloc_location(

38 "/usr/include/malloc.h" 3 4

        size_t __size

38 "/usr/include/malloc.h"

        , "/usr/include/malloc.h", 38)

38 "/usr/include/malloc.h" 3 4

                               __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__malloc__))
 __attribute__ ((__alloc_size__ (1))) ;

Normally it should be like this:
extern void *malloc (size_t __size) __THROW attribute_malloc
attribute_alloc_size ((1)) __wur;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants