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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:24:05+00:00 2026-05-14T21:24:05+00:00

Consider the following public method that adds an integer variable to a vector of

  • 0

Consider the following public method that adds an integer variable to a vector of ints(private member) in a class in C++.

KoolMethod()
{
  int x;
  x = 10;
  KoolList.Add(x);
}
Vector<int>KoolList;

But is this a valid addition to a vector ??? Upon calling the method, it creates a local variable. The scope of this local variable ends the moment the execution control leaves the method. And since this local variable is allocated on a stack(on the method call), any member of KoolList points to an invalid memory location in deallocated stack which may or may not contain the expected value of x. Is this an accurate description of above mechanism ??

Is there a need for creating an int in heap storage using “new” operator everytime a value needs to be added to the vector like described below ????:

KoolMethod()
{
  int *x = new int();
  *x = 10;
  KoolList.Add(x);
}
 Vector<int*>KoolList;   
  • 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-14T21:24:06+00:00Added an answer on May 14, 2026 at 9:24 pm

    But is this a valid addition to a vector?

    Yes, a (standard library) vector stores copies.

    Is there a need for creating an int in heap storage using “new” operator

    If you don’t want the objects to be copied or to work with polymorphic objects (see object slicing) you’d use pointers. In that case you should preferably avoid dealing with deallocation manually and use wrappers (smart pointers or pointer containers) though to get exception safety back.

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

Sidebar

Related Questions

Consider the following class: public class MyIntSet { private List<int> _list = new List<int>();
Consider the following Java method: public static void f(int n) { if (n<=1) {
Consider the following method in Java: public static boolean expensiveComputation() { for (int i
Consider the following class public class Class1 { public int A { get; set;
Consider the following code in a DLL: public class ReceivingClass { private Assembly myAssembly;
Consider the following code snippet: class Test { public int Length{ get; set; }
Consider the following method that stops a service: Public Function StopService(ByVal serviceName As String,
Consider the following Entity: @Entity public class Employee { @Id @GeneratedValue private Long id;
Consider the following case: public class A { public A() { b = new
Consider the following code: public class MyClass { public static string MyStaticMethod() { //string

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.