473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Memory allocation during object formation

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. class harsh
  3. {
  4.  private:
  5.     int acc;
  6.     int pass;
  7.  public :
  8.                    harsh(int a,int p)  //step2
  9.     {
  10.                       acc=a;               
  11.        pass=p;              
  12.                     }
  13.  
  14.  
  15. };
  16. void main()
  17. {
  18.  harsh h1;                               //step1 
  19. }
/*Question.. When the memory is allocated for object h1?
Answer..
1)at step1
2)at step2
*/
*Explain in details using pointer and memory block..
Apr 24 '10 #1
7 2334
Banfa
9,065 Expert Mod 8TB
Its hard to say since you return void from main you have invoked undefined behaviour because main always returns int like so
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     return 0;
  4. }
  5.  
Given that undefined behaviour is invoked the compiler could do anything, it might allocate 0 bytes of memory it might allocate 1Mbyte, it might cause demons to fly out of your nose and it might just act as if undefined behaviour has not been invoked.

What do you think this code does?
What does harsh contain?
What is the size of each of these things?
How big do you think that makes harsh?
Apr 24 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
Read up on constructors.

Constructors do not create objects just as destructors do not destroy objects.

Memory is allocated when objects are defined, this is when they are created.
Apr 24 '10 #3
hype261
207 100+
Because harsh is on the stack the memory is allocated for the harsh object here

void main
{ <--------

the object isn't initialized until it gets to

harsh h1;
Apr 24 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. Because harsh is on the stack the memory is allocated for the harsh object here
  2.  
  3. void main
  4. { <--------
C does this so all your stack variables must appear at the beginning of the function before any instructions.

This cannot be allowed in C++ becuse it permits uninitialized variables to be processed.


Therefore, C++ uses dynamic stack allocation since the size and number of the local variables is not known at compile time. Therefore, the memory is allocated at the place the object is defined followed immediately by a constructor call to initialize the object. That is to say the size of the C++ stack frame is no known at compile time.
Apr 25 '10 #5
hype261
207 100+
Maybe I am incorrect in my understanding, but if a function call stack frame consists of Function arguments , Return address, and Local Variables then how can memory not be allocated when the function enters scope?

In Bruce Eckels book he says:

When a C++ object is created two events occur:

Storage is allocated for the object
The constructor is called to initialize the storage.

Storage can be created on the stack whenever a particular execution point is reached (an opening brace). That storage is released automatically at the complementary execution point (the closing brace).

I could be reading this incorrectly...
Apr 25 '10 #6
Banfa
9,065 Expert Mod 8TB
C does this so all your stack variables must appear at the beginning of the function before any instructions.
Not strictly true variables in C can appear at the top of any block enclosed by braces, that might be a function or an if or for statement or a set of braces put in place specifically to declare another variable (a debug technique rather than good programming practice).

Therefore, C++ uses dynamic stack allocation since the size and number of the local variables is not known at compile time.
This also seems wrong. The compiler does know the size and number of local variables required at a specific point in the code at compile time, what it does not know is the size and number of all the local variables that may be declared in a function at the time the opening brace of the function is parsed.

However by my first statement that is also true of C.

If a given compiler C or C++ output the code after parsing the entire function then I see no reason why it shouldn't allocate all the memory required when the function enters. If the data is for an object that has not actually been declared in the code yet it would have to be considered memory reserved for the object but not actually allocated to the object yet. In place construction would facilitate this.

Not only that but there is nothing in the standard that forbids this since the standard does not even mandate a stack although that isn't to say there are not practical obsticles to this scheme, I particularly have not given any thought to how stack unwinding and object deletion would be handled in the case of an exception.
Apr 25 '10 #7
weaknessforcats
9,208 Expert Mod 8TB
Storage can be created on the stack whenever a particular execution point is reached (an opening brace). That storage is released automatically at the complementary execution point (the closing brace).
I see where our confusion is. In the example, the object was a local stack variable that was in scope. Therefore the memory was allocated when the scope was entered. That is, at the beginning of main(). However, functions can have many levels of braces and at each of those levels stack memory can be allocated. Therefore, all stack memory cannot be allocated when the function is called.

Apparently, I misread your comment which is true for the example
Apr 25 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Chris S. | last post by:
Is it possible to determine how much memory is allocated by an arbitrary Python object? There doesn't seem to be anything in the docs about this, but considering that Python manages memory...
2
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
6
by: chris | last post by:
Hi all, I need to know, what is the difference between dynamic memory allocation, and stack allocation ? 1. If I have a class named DestinationAddress, when should I use dynamic memory...
15
by: berthelot samuel | last post by:
Hi, I'm trying to develop an application for modeling 3D objects from Bezier patches, but I have a memory allocation problem. Here are my structures: typedef struct _vector3 { union { struct...
9
by: Andrew Au | last post by:
Dear all, I am trying to write a piece of software that use Object Oriented design and implement it with C, I did the following == In Object.h == typedef struct ObjectStructure* Object; ...
5
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
13
by: eternalLearner | last post by:
i am writing an client application that connects to the server and then sends data to server. but, the problem is that i get a java.net.ConnectException: Connection refused error. The code i have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.