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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:23:11+00:00 2026-05-14T22:23:11+00:00

vector< int > vect; int *int_ptr = new int(10); vect.push_back( *int_ptr ); I under

  • 0
vector< int > vect;
int *int_ptr = new int(10);
vect.push_back( *int_ptr );

I under stand that every “new” needs to be followed by a “delete” at some point but does the clear() method clean this memory?

What about this method of doing the same thing:

vector< int > vect;
int int_var = 10;
vect.push_back( int_var );

From what I understand, clear() calls the variables destructors, but both vect.push_back() methods in this example push an object on the vector, not a pointer. so does the first example using an int pointer need something other than clear() to clean up memory?

  • 1 1 Answer
  • 3 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-14T22:23:12+00:00Added an answer on May 14, 2026 at 10:23 pm

    The first method leaks because the vector never takes ownership of the allocated pointer. In fact, it doesn’t contain a pointer at all, only a copy of the value.

    The second method does not leak, as no memory is dynamically allocated (except internally in the vector — it will handle that memory itself).

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

Sidebar

Related Questions

I am testing a function vect_dbg(vector<int>) that returns the contents of a STL vector
I have a function that creates a 2D vector void generate(int n) { vector<
vector<int> l; for(int i=0;i<10;i++){ l.push_back(i); } I want the vector to only be able
std::vector<const int> vci; vci.push_back(1); vci[0] = 2; With the element type being const int
If I have a vector<string*> *vect or a map<pair<string*, int*>, string*> *map , how
I am new to boost and metaprogramming so my problem is this: typedef fusion::vector<int,
I read a integer binary file to int vector. When I use the Sort
Suppose we'd like to remove duplicate values from a vector of int s. The
vector<int>& mergesort(vector<int> &a) { if (a.size() == 1) return a; int middle = a.size()
Example code : int main() { std::vector<int> v1{1, 2, 3, 4, 5, 6, 7,

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.