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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:39:29+00:00 2026-05-16T16:39:29+00:00

i have the following struct: struct Message { Agent *_agent; double _val; }; and

  • 0

i have the following struct:

struct Message {
    Agent *_agent;
    double _val;
};

and the following Ptrs array:

typedef Message* MessageP;
MessageP *_msgArr;
_msgArr = new MessageP[MAX_MESSAGES];

this is the method that inserts a Message to the array:

void Timing::AddMessage(Agent * const agentPtr, double val) {

    MessageP msgPtr = new Message;
    assert(msgPtr != 0);

    //assign values:
    (*msgPtr)._agent = agentPtr;
    (*msgPtr)._val = val;

    //add to messages array:
    assert(_msgArr != 0 && _waitingMsgs<MAX_MESSAGES);
    _msgArr[_waitingMsgs] = msgPtr;
    _waitingMsgs++;

}

My question is about the deletion of this array. I would like to delete the array and all allocated structs.
if i write:

delete [] _msgArr  

will this delete also each allocated struct or will free only the allocated memory for the array?

Is the correct way is to go over the entire array with a for loop and write

delete _msgArr[i]

and at last wite delete [] _msgArr to delete the allocated array
?

thanks!

  • 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-16T16:39:30+00:00Added an answer on May 16, 2026 at 4:39 pm

    The delete [] will call the destructors on the struct pointers, which doesn’t dispose of the structs or the _agent members, which itself points to memory. You could call delete _msgArr[i]._agent and then delete _msgArr[i] in a loop, which will dispose of the Agent and then the Message.

    First, though, you need to know who should get rid of the Agents, and when. If these are owned by another data structure, you shouldn’t get rid of them when getting rid of _msgArr, and looping over _delete _msgArr[i] followed by delete [] _msgarr; is all you need.

    If you do need to delete the Agents also, you have three reasonable choices.

    First, you can give Message a destructor that will delete its _agent. It should also have a copy constructor and assignment operator defined then, either to pass ownership or to copy, or else define them as private so any attempt to use them will be a compile-time error.

    Second, you could change the Agent * to a smart pointer, so that the extra memory will be deleted when the Message goes away.

    Third, you could go through the loop I suggested above when getting rid of the array.

    Unless you have good reasons to keep the code C-compatible, I’d suggest that you use a std::vector<boost::shared_ptr<Message> >, and have Message contain a boost::shared_ptr<Agent> rather than an Agent * (if you don’t have to dispose of the Agents, Agent * is fine). At that point, you don’t need to worry: when _msgArr goes out of scope, all the memory is cleaned up properly.

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

Sidebar

Ask A Question

Stats

  • Questions 515k
  • Answers 515k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use the :gt() (greater-than-index) selector, like this: jQuery(".toexpand:gt(0)").hide();… May 16, 2026 at 6:30 pm
  • Editorial Team
    Editorial Team added an answer @aaa carp is correct that is an implicit rule, but… May 16, 2026 at 6:30 pm
  • Editorial Team
    Editorial Team added an answer Yes and no. There are a lot of images in… May 16, 2026 at 6:30 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have the following C++ struct: typedef struct FormulaSyntax{ WORD StructSize; short formulaSyntax [2];
I have the following structure, typedef struct _MainWin { GtkWindow parent; GtkWidget* scroll; GtkWidget*
I have the following CPP code snippet and the associated error message: Code snippet
I have a struct struct request { int code; char *message; }; that I'd
I have successfully created the message queue by using the following command: msgIdHareTurtle =
Consider I have the following struct : struct IDirect3D { IDirect3D() : ref_count_(0) {}
I have defined the following struct to represent an IPv4 header (up until the
Say I have the following code: struct date { int day; int month; int
Suppose there exists a function which returns a message say of the following format:
Following code fails with a error message : t.cpp: In function `void test()': t.cpp:35:

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.