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

The Archive Base Latest Questions

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

I am alittle bit confused on topic of allocating objects on heap vs allocating

  • 0

I am alittle bit confused on topic of allocating objects on heap vs allocating on stack, and when and how delete() should be called.

For example I have class Vector. I would like to make an array of these.

I could either do this

Vector** v = new Vector*[100]; //create an array of 100 pointers to 100 Vector objects

This as I understand would allocate everything (well except pointer addresses) on a heap?
So to free memory I would need to:

for (int i = 0; i < 100; ++i)
{
   delete(v[i]);
}
delete(v);

OR just

delete(v);

is enough?

Now another example:

Vector* v = Vector[100];

Whats happening in this case? Where does allocation happen? Heap or stack?
Do I still need to call

delete(v);

But this is not all question, sorry for long post..

example:

class Vector
{
  int x, y, z;
}

Vector* v = new Vector();

wheres x, y, z allocated? Heap or stack?

or how about this:

class Vector2
{
   int items[10];
}

Vector2* v2 = new Vector2();

where are items[10] allocated?
How do I delete v2? Do i need custom destructor?

Also last but not least how about this one:

class Vector3
{
   int* items;
}

Vector3 v3 = Vector3();

Where is items pointer stored? heap or stack? How do I delete this?

Thanks and sorry for long question. Ive been having trouble with this stuff for long time and couldnt really find any complete explanation on line.

  • 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-23T20:14:01+00:00Added an answer on May 23, 2026 at 8:14 pm

    I’ll start from the beginning…

    Vector** v = new Vector*[100];
    

    Allocates an array of 100 pointers to objects of type Vector on the heap
    It returns one pointer- v – the you can use to keep track of this array of pointers.

    Delete this array of 100 points with:

    delete[] v;
    

    (Use the delete operator- delete for a single allocated object, delete[] for an array)

    Next case (I’m assuming you mean new Vector[100]:

    Vector* v = new Vector[100];
    

    You allocated an array of 100 Vectors on the heap and got a pointer to its start location- v.
    Delete this array with:

    delete[] v;
    

    Next…

    class Vector
    {
      int x, y, z;
    }
    
    Vector* v = new Vector();
    

    This allocates an object of class Vector on the heap and gives you a pointer to keep track of it. Because you allocated the entire object on the heap, x, y, and z are all allocated on the heap.

    Delete it with:

    delete v;
    
    
    class Vector2
    {
       int items[10];
    }
    
    Vector2* v2 = new Vector2();
    

    This one is a bit trickier but I’m going to reason it out…

    Classes are blueprints. You haven’t allocated any memory at all until you instantiate the class somehow, in this case on the heap. Because the class is a blueprint, items could not have been allocated until you created an object of class Vector2 on the heap. I think we can reasonably infer that items is thus allocated on the heap.

    Delete v2 with:

    delete v2;
    

    And finally:

    class Vector3
    {
       int* items;
    }
    
    Vector3 v3 = Vector3();
    

    You allocated all of class Vector3 on the stack, the pointer inside of it items is also allocated thus. Nothing went on the heap, so don’t delete it.

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

Sidebar

Related Questions

I'm a little bit confused by this scenario: I have a class that implements
I am a little bit confused with Doctrine class. I created a class and
Ok, I'm a little bit confused here. I'm trying to select a DAO class
I have a jquery code, but I'm a little bit confused on how can
I am a little bit confused about the native heap on android, I would
I am a little bit confused about the ComponentName class in Android. There are
I'm a little bit confused here. I have 2 models: User Ticket A Ticket
I'm a little bit confused about using the class GregorianCalendar and Date . What
Hi I'm working with boolean values and I'm a little bit confused. I have
I am a little bit confused how SVN related settings should be configured assuming

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.