Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

g++ error: ‘malloc’ was not declared in this scope

Tags:

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)

like image 343
Ovilia Avatar asked Aug 10 '11 07:08

Ovilia


People also ask

What is the difference between malloc and New in C++?

You should use new in C++ code rather than malloc so it becomes new GLubyte* [RESOURCE_LENGTH] instead. When you #include <cstdlib> it will load malloc into namespace std, so refer to std::malloc (or #include <stdlib.h> instead).

Is 'aligned_alloc' not declared in this scope?

'aligned_alloc' was not declared in this scope#117 Closed lsmainzeropened this issue Mar 27, 2018· 3 comments Closed 'aligned_alloc' was not declared in this scope#117

Is the variable “B” out of the scope of the program?

As you can see that the compiler also identified the line which has the error and also it has indicated that the variable “b” is out of the scope in the program. We have posted an image below in which you can clearly see that the Arduino IDE has highlighted the variable that it is unable to understand.


2 Answers

You should use new in C++ code rather than malloc so it becomes new GLubyte*[RESOURCE_LENGTH] instead. When you #include <cstdlib> it will load malloc into namespace std, so refer to std::malloc (or #include <stdlib.h> instead).

like image 158
user786653 Avatar answered Sep 20 '22 13:09

user786653


You need an additional include. Add <stdlib.h> to your list of includes.

like image 35
dragonroot Avatar answered Sep 20 '22 13:09

dragonroot