I'm using g++ under Fedora to compile an openGL project, which has the line:
textureImage = (GLubyte**)malloc(sizeof(GLubyte*)*RESOURCE_LENGTH);
When compiling, g++ error says:
error: ‘malloc’ was not declared in this scope
Adding #include <cstdlib>
doesn't fix the error.
My g++ version is: g++ (GCC) 4.4.5 20101112 (Red Hat 4.4.5-2)
You should use
new
in C++ code rather thanmalloc
so it becomesnew GLubyte*[RESOURCE_LENGTH]
instead. When you#include <cstdlib>
it will loadmalloc
into namespacestd
, so refer tostd::malloc
(or#include <stdlib.h>
instead).You need an additional include. Add
<stdlib.h>
to your list of includes.Reproduce this error in g++ on Fedora:
How to reproduce this error as simply as possible:
Put this code in main.c:
Compile it, it returns a compile time error:
Fix it like this:
Then it compiles and runs correctly: