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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:36:12+00:00 2026-05-22T15:36:12+00:00

I have a class Agent , which contains a pointer (array) of class ValueContainer

  • 0

I have a class Agent, which contains a pointer (array) of class ValueContainer.

Now what I have to do is to create an array of Agent first, and then initialize each one of them independently. Also, Agent has no empty constructor due to its nature.

It’s more or less like this:

Agent agent[n];
for (int i...)
{
    load values from hd;
    create a ValueContainer with this values;
    initialize Agent[i] with loaded values;
    deallocate the upstated ValueContainer;
}
// agent's constructor copies the VC values in his own VC,
// so there is no problem of pointers

Unfortunately, I keep on running in segmentation fault errors. I tried allocating the array with malloc, with the upstated declaration, making it an array of pointers. Still, I don’t have any results.

  • 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-22T15:36:12+00:00Added an answer on May 22, 2026 at 3:36 pm

    Saying that you want to create an array of Agent without initializing those agents is an anachronism. Initialization takes place during construction. You can’t create a thing without doing some kind of initialization.

    In almost every case, the “right” thing to do in C++ is to use a vector or some other Standard container for your array, and then push_back new elements as you create them. You should see if this is the right approach for you. Without knowing anything at all about your application, it probably is.

    Maybe what you mean (or maybe what you want) is to create memory space for the array, and then initialize them as you load them. This is quite rare, but it does happen. In that case, first you can allocate a char buffer that is big enough to hold all your items:

    char buf[correct_size];
    

    …and then use placement-new to initialize your Agents within this buffer:

    new (&buf[byte_index]) Agent(parameter_list);
    

    This is tricky. You have to juggle several things when using placement new:

    1. You need to make sure you use the right byte_index within the buffer.
    2. You need to make sure you deallocate in the correct order: destroy the Agents first, then destroy the buffer they are in.
    3. You have to explicitly call the destructor in order to destroy the Agents:

    Example of #3:

    Agent* agent = reinterpret_cast<Agent*>(&buf[byte_index]);
    agent->~Agent();
    

    This is risky code in about a million different ways. I can’t stress strongly enough how no matter how space-age and tempting it may be to try to do this, you probably should not do it in production code. Using placement new should be a last resort when absolutely nothing else will work.

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

Sidebar

Related Questions

I have a class Agent with a property Id Given a collection of Agents
I have an the following class I'm trying to create a list of with
I have a list with two or more objects of class Agent. Name =
I have developed one windows service through which I am trying to open an
I have a login.jsp page which contains a login form. Once logged in the
I have class method that returns a list of employees that I can iterate
I have: class MyClass extends MyClass2 implements Serializable { //... } In MyClass2 is
I have class A: public class ClassA<T> Class B derives from A: public class
I have class with internal property: internal virtual StateEnum EnrolmentState { get { ..getter
I have class with a member function that takes a default argument. struct Class

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.