memory allocation for objects

0 votes
When we instantiate a variable in C++, such as int x within a function (i.e. x is a local variable), it is allocated on the process's stack.

However, if we do int *x= new int, heap space is granted. So here are my inquiries:

What about objects of multiple classes (c++ classes or user specified classes)?

Where are their items created?

As an example:

Employee is a class, and we declare it emp;.

Is emp allocated space on the stack or on the heap?

Do all four cells of a get stack space if the declaration int a[4] is within a function?
Aug 11, 2022 in C++ by Nicholas
• 7,760 points
480 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 17, 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

In C++, a variable like int x ...READ MORE

Aug 16, 2022 in C++ by Nicholas
• 7,760 points
687 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