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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:36:01+00:00 2026-06-08T05:36:01+00:00

EDIT : thank you for the answers! I declared the tellSomething method with a

  • 0

EDIT: thank you for the answers! I declared the tellSomething method with a std::string return type while it should have been void!

I was tripping myself up and blamed the poor guiltless delete operator :)!


Let’s consider a pointer to a dynamically allocated vector which contains pointers to dynamically allocated objects:

// Create the vector of pointers
std::vector<A *>* v = new std::vector<A *>;

// Create two objects
A *a1 = new A;
A *a2 = new A;

// Populate the vector
v->push_back(a1);
v->push_back(a2);

// Delete the vector
delete v;

// Try accessing one of the objects
a1->tellSomething();    --> // Segmentation fault

As expected, if I delete the vector, the delete on the contained objects is not called (I also verified that A::~A() is never called in the above code), however, the last instruction gives a segmentation fault.

What I expect from the delete v is two things:

  • The destructor for every contained object is called
  • The container is deallocated

But in this case the contained objects are pointers, so no destructor is called.

Also, a1 is not NULL at the end of the listing.

So, why the segmentation fault?


Complete example here: http://ideone.com/r8YC0.


Note: I don’t usually use raw pointers with STL containers, please, consider this code as a purely theoretic example to help me understanding the logic of the delete v instruction.

  • 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-08T05:36:04+00:00Added an answer on June 8, 2026 at 5:36 am

    The crash you get is completely unrelated; you declared tellSomething as returning a std::string, but you never return anything, so you go into undefined behavior-land; the fact that the program crashed after deallocating the vector is sheer luck, it could have crashed even at the first call to tellSomething.

    Fixing that problem makes your program run fine (although you are leaking a1 and a2).

    By the way, this teaches you to turn up all the warnings: with -Wall that code would have given you an explicit warning about this potential problem:

    matteo@teolapmint ~/cpp $ g++ -Wall testwarns.cpp 
    testwarns.cpp: In member function ‘std::string A::tellSomething()’:
    testwarns.cpp:12:5: warning: no return statement in function returning non-void [-Wreturn-type]
    

    (just for the record: personally I recommend to compile with -Wall -Wextra -ansi -pedantic, often one warning can save you a lot of debug time).

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

Sidebar

Related Questions

EDIT: I have fixed all but two warnings now, so thank you all for
I have an array of type string which I want to re-use inside a
YES, I have been looking for answers and couldn't find anything that worked or
Why new()/delete() is slower than malloc()/free()? EDIT: Thanks for the answers so far. Please
Code as reference: http://jsbin.com/aboca3/2/edit In this example above (thank you SLaks) I am truncating
Please let me know if you have any idea about it. Thanks EDIT What
I have this piece of code: while(fileStream>>help) { MyVector.push_back(help); } ...and lets say that
I'm looking to access the generic type of a declared field during runtime. I
I have a basic question on Java ArrayList . When ArrayList is declared and
I realize that this question has been answered before, and I have looked through

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.