error when using g++: 'malloc' was not declared in this scope

15,174

Solution 1

Namespaces and include files are two totally different things. You need to

#include <stdlib.h>

or, equivalently,

#include <cstdlib>

in order to get access to the declarations in that header file.

Your using-declaration

using namespace std;

on the other hand, means that you can use identifiers that are part of the namespace std, i.e. that were declared inside

namespace std {
  /*...*/
}

without prepending std:: each time.

For example, if you include <string>, you can use the data type std::string, but if you also add using namespace std;, you can use that data type simply as string.

Note, however, that malloc is not defined inside any namespace, so in order to use that, you need only to include stdlib.h.

Note For the difference between stdlib.h and cstdlib, see here.

Solution 2

malloc() is defined in <cstdlib> so you must include it at the top of your file.

using namespace std; just tells the compile that you're using that particular namespace, and has nothing to with including any library methods.

In any case, you really should be using new rather than malloc for dynamic allocation when using C++.

Solution 3

using namespace std; tells the compiler, I'm "using" the std namespace, so pretend like I'm in the std namespace for lookups and don't ask me to say std:: every time.

The trouble is, there is nothing in the std namespace (yet)!

You need to #include the header for malloc to be declared.

And even then, it's global -- not in std.

Share:
15,174
Mark Z.
Author by

Mark Z.

Updated on June 04, 2022

Comments

  • Mark Z.
    Mark Z. about 2 years

    I am practicing g++ to compile my code yet the error "malloc was not declared in this scope" keeps emerging at the beginning. The pieces of my code related to this error looks like:

    /*------Basic.h--------*/
    using namespace std;
    
    /*------A.h------------*/
    class A{
     private:
      double* _data;
     public:
     A(int N);
    }
    
    /*------A.cpp----------*/
    A::A(int N){
      _data=(double*)malloc(N*sizeof(double));
    }
    

    This problem never emerges when I use Microsoft Virtual Stdio. I therefore tried to add a line

    #include <stdlib.h>
    

    to Basic.h, and the error disappears. Now I am wondering why this kind of thing happens. Hasn't "namespace std" already include stdlib.h? Thanks a lot.

    • jogojapan
      jogojapan almost 12 years
      Closely related (almost a duplicate) stackoverflow.com/questions/3278864/…
    • jogojapan
      jogojapan almost 12 years
      Also here: stackoverflow.com/questions/5115556/…. I am going to vote to close as duplicate.
    • GManNickG
      GManNickG almost 12 years
      Avoid putting using directives in headers.
    • Mark Z.
      Mark Z. almost 12 years
      I was keeping searching questions about "malloc" instead of "namespace std "so I didn't find any closely related question. Thanks for your links, jogojapan.
  • newpxsn
    newpxsn almost 12 years
    stdlib.h is a perfectly acceptable alternative to cstdlib. Which to use is mostly a style thing, and has more to do with whether you're using more C or C++ APIs in your code. Likewise blanket pronouncements about new vs malloc aren't really helpful. C++ is far too broad a language for that kind of pedantry.
  • GManNickG
    GManNickG almost 12 years
    @AndyRoss: malloc allocates raw memory, new allocates memory and correctly constructs objects. C++ needs the differentiation necessarily. Most of the time, people want a new object, not raw memory.
  • Mark Z.
    Mark Z. almost 12 years
    Why in Microsoft Vitual Stdio there is nothing wrong? Does VC set cstdlib as default? Thanks.