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

  • Home
  • SEARCH
  • 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 6752583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:03:42+00:00 2026-05-26T13:03:42+00:00

Basically, I have a block of memory allocated using malloc that I want to

  • 0

Basically, I have a block of memory allocated using malloc that I want to start placing objects into using placement new. I know that the destructors for these objects will have to be explicitly called when I’m deleting them, but I want to make sure that I understand this completely, and that I’m going about it the right way. I’m almost certainly going to scrap this approach and go about things in a more straightforward manner, but I figured that I’d ask just for understanding’s sake.

I have a class WordIndex that contains the following private member variables:

struct Word
{
    std::string word;
    std::vector<unsigned int> index;
};

Word* m_words;
std::string* m_lines;

Memory is allocated/reallocated for both pointers using malloc/realloc, and objects are being created using the following code (they’re not overwriting any preexisting objects):

new (&m_words[pos]) Word();
new (&m_lines[m_numoflines]) std::string();

I have a destructor as follows, but I’m almost 100% sure that it’s not doing things correctly, despite not showing any errors when running.

WordIndex::~WordIndex()
{
    unsigned int i;
    for( i = 0; i < m_numofwords; i++)
    {
        m_words[i].~Word();
    }

    free(m_words);
    free(m_lines);
}

My questions are as follows:

  1. Considering the Word struct has a vector, is it enough to simply call ~Word, or do I need to call the destructor for the vector separately beforehand?
  2. Do I need to call a destructor for a string (both for m_lines and for Word.word), and if so, how do I do that? It doesn’t appear to have a function like ~string that I can call.
  3. Have I missed anything else? I’m fairly certain that I have.

Thanks in advance!

  • 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-26T13:03:43+00:00Added an answer on May 26, 2026 at 1:03 pm
    1. Considering the Word struct has a vector, is it enough to simply call ~Word, or do I need to call the destructor for the vector
      separately beforehand?

    You can write a destructor for your struct if you want, but in this case it’s not necessary.

    1. Do I need to call a destructor for a string (both for m_lines and for Word.word), and if so, how do I do that? It doesn’t appear to
      have a function like ~string that I can call.

    Yes, you do. The destructor should be there, it’s typedefed from basic_string, so you should be calling ~basic_string() on it.

    1. Have I missed anything else? I’m fairly certain that I have.

    The destructor for string.

    Also, make sure you calculate the space for malloc correctly, you don’t show that code, but verify its what its supposed to be.

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

Sidebar

Related Questions

Basically I have a block of html that I want to echo to the
I have a block of code that basically intializes several classes, but they are
I have a block of html in a string that is basically a list
Basically in order to have a profile Block u need - let's say profile
I have a signal that blocks SIGINT and basically says Sorry, you can't quit.\n
I basically have a server set up and I'm accepting new clients(UNIX) and i'm
Basically I have a script in Python that grabs the text from an open
This is a pretty straight-forward problem. I basically have a div block on a
i need to have reliable measurement of allocated memory in a linux process. I've
I am dealing with some HTML text that I basically have read access to.

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.