Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8210327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:02:15+00:00 2026-06-07T10:02:15+00:00

I am practicing g++ to compile my code yet the error malloc was not

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:02:18+00:00Added an answer on June 7, 2026 at 10:02 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am practicing this https://developer.mozilla.org/en/Canvas_tutorial/Basic_animations Clock tutorial. Everything in the code is clear to
Firstly this is not a homework question. I am practicing my knowledge on java.
Been practicing with those system calls, but I stucked into this code: #include <stdio.h>
while practicing recursive functions, I have written this code for fibonacci series and (factorial)
I'm practicing for an exam and trying to figure this out. I'm just not
I am practicing sending emails with Google App Engine with Python. This code checks
I am practicing NHibernate recently and it does not look that much easy. (This
Started practicing with XML and C# and I have an error message of There
I am practicing at getting consistent with my error handling, and I keep hoping
I was practicing some programming problems and tried to code the popular reverse words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.