1

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.

Mark Z.
  • 655
  • 2
  • 9
  • 12
  • Closely related (almost a duplicate) http://stackoverflow.com/questions/3278864/what-is-the-difference-between-header-file-and-namespace – jogojapan Jul 11 '12 at 03:49
  • Also here: http://stackoverflow.com/questions/5115556/c-using-namespace-and-include. I am going to vote to close as duplicate. – jogojapan Jul 11 '12 at 03:52
  • Avoid putting using directives in headers. – GManNickG Jul 11 '12 at 06:07
  • 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. – Mark Z. Jul 12 '12 at 04:20

4 Answers4

3

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.

Community
  • 1
  • 1
jogojapan
  • 68,383
  • 11
  • 101
  • 131
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++.

Dongie Agnir
  • 612
  • 4
  • 11
  • 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. – Andy Ross Jul 11 '12 at 03:37
  • @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. – GManNickG Jul 11 '12 at 06:06
  • Why in Microsoft Vitual Stdio there is nothing wrong? Does VC set cstdlib as default? Thanks. – Mark Z. Jul 12 '12 at 04:21
1

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.

user541686
  • 205,094
  • 128
  • 528
  • 886
0

Use 'new' for memory allocation. 'using namespace std' has nothing to do with stdlib.h IF you still want to use malloc() add this on the top

#include<cstdlib>

Good luck!

Anirudh Rayabharam
  • 737
  • 1
  • 6
  • 12