memory allocation for objects

0 votes
In C++, a variable like int x (which is a local variable because it is instantiated within a function) is allocated on top of the process stack.

However, if we use int *x=new int, heap space is made available.

 

So here are my inquiries:

So here are my inquiries:

1. What about objects belonging to several classes (user-defined or classes given by C++)?

Where do their things actually get created?

For instance:

Employee shall be a class, and Employee shall be declared emp;.

Empty spaces are either supplied on a stack or in a heap.

2.Do all four of the cells in int a[4] of a receive stack space if the declaration is contained within a function?
Aug 16, 2022 in C++ by Nicholas
• 7,760 points
687 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes
All local variables, whether they come from classes or built-in types or whether they are arrays, are on the stack.

On the heap, all dynamic allocations are made.

Naturally, applying a modifier like static to a local variable will cause the variable to be placed elsewhere, preserving it across function calls.

Additionally, to add to your confusion, if you establish a local pointer variable and set it to point to an object that was dynamically generated, for example

Class* a = new Class;

The memory that the variable a points to is on the heap, while the variable itself is on the stack.
answered Aug 19, 2022 by Damon
• 4,960 points

edited 6 days ago
0 votes

It's exactly the same as with normal types.

Class a; //stack. Usage: a.somethingInsideOfTheObject
Class *a = new Class(); //heap. Usage: a->somethingInsideOfTheObject

Note that if the class itself is allocating something on the heap, that part will always be on the heap, for example:

class MyClass
{
public:
    MyClass()
    {
        a = new int();
    }
private:
    int * a;
};

void foo()
{
    MyClass bar;
}

in this case the bar variable will be allocated on the stack, but the a inside of it will be allocated on the heap.

answered Aug 19, 2022 by Damon
• 4,960 points

edited 6 days ago

Related Questions In C++

0 votes
1 answer

static memory allocation like dynamic memory allocation

This declaration int r, c; cin >> r >> ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
1,199 views
0 votes
0 answers

static memory allocation like dynamic memory allocation

int r, c; cin >> r >> c; int ...READ MORE

Jun 6, 2022 in C++ by Nicholas
• 7,760 points
642 views
0 votes
0 answers

How to use the priority queue STL for objects?

class Person { public: int age; }; I'd want to put objects of the type Person in a priority queue. priority_queue< ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
539 views
0 votes
1 answer

Sorting a vector of custom objects

A simple example using std::sort struct MyStruct { ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,401 views
0 votes
1 answer

insert method for doubly linked list C++

I attempted to repair all of your methods, and I believe I succeeded; at least, the current test example prints the proper answer: #include <iostream> #include <vector> using namespace std; struct Node { ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
1,031 views
0 votes
0 answers

C++ pointer to objects

Is it always necessary in C++ to  ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
539 views
0 votes
0 answers

memory allocation for objects

When we instantiate a variable in C++, ...READ MORE

Aug 11, 2022 in C++ by Nicholas
• 7,760 points
480 views
0 votes
1 answer

How can we set environment variables from java?

For setting the environment variable, you can ...READ MORE

answered Jun 21, 2018 in Java by Akrati
• 960 points
4,031 views
0 votes
1 answer

setuptools: build shared libary from C++ code, then build Cython wrapper linked to shared libary

There is a seemingly undocumented feature of setup that ...READ MORE

answered Sep 11, 2018 in Python by Priyaj
• 58,020 points
781 views
0 votes
2 answers

difference between class and instance attributes

Apart from the performance, there is a ...READ MORE

answered Sep 17, 2018 in Python by SDeb
• 13,300 points
1,002 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP