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

92,739

Solution 1

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).

Solution 2

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

Solution 3

Reproduce this error in g++ on Fedora:

How to reproduce this error as simply as possible:

Put this code in main.c:

#include <stdio.h>
int main(){
    int *foo;
    foo = (int *) std::malloc(sizeof(int));
    *foo = 50;
    printf("%d", *foo);
}

Compile it, it returns a compile time error:

el@apollo:~$ g++ -o s main.c
main.c: In function ‘int main()’:
main.c:5:37: error: ‘malloc’ was not declared in this scope
     foo = (int *) malloc(sizeof(int));
                                     ^  

Fix it like this:

#include <stdio.h>
#include <cstdlib>
int main(){
    int *foo;
    foo = (int *) std::malloc(sizeof(int));
    *foo = 50;
    printf("%d", *foo);
    free(foo);
}

Then it compiles and runs correctly:

el@apollo:~$ g++ -o s main.c

el@apollo:~$ ./s
50
Share:
92,739
Ovilia
Author by

Ovilia

Web designer and programmer. Personal site: http://zhangwenli.com

Updated on June 14, 2020

Comments

  • Ovilia
    Ovilia about 4 years

    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)

  • templatetypedef
    templatetypedef almost 13 years
    If you do need to use a malloc-like function, in C++, consider using the function operator new, which interfaces with the rest of the memory system (it throws exceptions, calls the new handler if memory can't be found, etc.)
  • Sander De Dycker
    Sander De Dycker almost 13 years
    Since using #include <stdlib.h> dumps all declared names in the global namespace, the preference should be to use #include <cstdlib>, unless you need compatibility with C.
  • Keith Thompson
    Keith Thompson about 10 years
    As I understand it, #include <cstdlib> will import malloc and friends into the std namespace, and may or may not import them into the global namespace, while #include <stdlib.h> will import them into the global namespace, and may or may not import them into the std namespace.
  • Ravinder Payal
    Ravinder Payal almost 8 years
    error: invalid conversion from 'void*' to 'char**' [-fpermissive] result = std::malloc(sizeof(char*) * count);
  • Eric Leschinski
    Eric Leschinski almost 8 years
    The error you are posting is unrelated to the malloc issue addressed in my post. If you have a new question please use the "Add Question" button in the upper right.
  • Eric Leschinski
    Eric Leschinski almost 8 years
    malloc always returns void. Please refrain from posting followup questions on answers that have nothing to do with your problem.