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 6349813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:41:22+00:00 2026-05-24T21:41:22+00:00

Here is the situation : I use a malloc to allocate memory for a

  • 0

Here is the situation :

I use a malloc to allocate memory for a struct.
The struct contains various items such as pointers, string variables and vectors.

The fact is, when we use malloc, no constructors are called. Using a code similar to the following one, I’ve experienced some situation where some variables worked while others didn’t.

Note : The following code doesn’t compile. It’s purpose is only to illustrate the situation.

struct MyStruct 
{
    MyClass*    mFirstClass;
    bool        mBool;
    std::string mString;
    std::vector<MyClass> mVector;
};


int main()  
{  

    MyStruct* wMyStructure;  
    wMyStructure = (MyStruct*) malloc (sizeof(MyStruct));  

    MyClass wMyClassObject;

    wMyStructure->mFirstClass = new MyClass();  
    wMyStructure->mFirstClass->func();  
    wMyStructure->mBool = false;  
    wMyStructure->mString = "aString";  
    wMyStructure->mVector.push_back(wMyClassObject);

    return 0;
}

By using pointers instead of those variables (std::string* mString), followed by a call to the object constructor (mString = new std::string;) Exception are not thrown.

However, I’ve experienced a situation where the mString was used without problem without the constructor being called, but when it came to the vector, the application exit automatically.

This left me with many questions:

  1. When will an object throw an exception if no constructor were used?

  2. In the situation I experienced, only the vector caused problem. Could mString be left as it is or should I call it’s constructor?

  3. What would be the safest way, using malloc, to do the whole thing?

  • 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-05-24T21:41:22+00:00Added an answer on May 24, 2026 at 9:41 pm

    Your code causes undefined behaviour, because your wMyStructure does not point to an object, so you may not use the accessor operator -> on it.

    An object only commences its life after its constructor has completed. Since you don’t call any constructor, you do not have an object.

    (If your struct were a POD, i.e. just consisting of primitive types and PODs, then this would be OK, because PODs have trivial constructors, which do nothing.)

    The concrete problem you’re facing is that the string and vector members of your struct didn’t get to call their constructors, so those members don’t exists, and hence the entire object doesn’t.

    If you want to decouple memory management from object construction, you can use placement syntax:

    // get some memory
    char arena[HUGE_VAL];
    void * morespace = malloc(HUGE_VAL);
    
    // construct some objects
    MyClass   * px = new (arena + 2000) MyClass;  // default constructor
    YourClass * py = new (morespace + 5000) YourClass(1, -.5, 'x');  // non-default constructor
    

    (You have to destroy those objects manually, px->~MyClass(); etc., when you’re done with them.)

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

Sidebar

Related Questions

Here is my situation: I have an application that use a configuration file. The
Here's the situation: We have some generic graphics code that we use for one
Here's my situation. I'm developing an ASP.NET (2.0) app for internal use. In it,
Here's my situation : in the webapp, I use an interceptor to set the
Why does PEP-343 use type() in this situation here? mgr = (EXPR) exit =
Here is the situation: Some pages on my site use different js files than
Here is my situation: I have an interface that each of my controls use
Here's my situation: I have two pages. I use this line of code to
Trying to learn how to use outparse. So here is the situation, I think
ok here is the situation , I want to use a login form for

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.