473,396 Members | 1,938 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,396 software developers and data experts.

How does new know how much memory to allocate??

Hi,
I was just wondering how the new operator (in C++) knows how much memory to allocate when creating an object of a class such as shown below:

i.e

class Books
{
...
}

int main()
{
....
Books textbook = new Books();
....
}

I have a stronger C background and I know that in C we have to specify the memory size for malloc(), using sizeof()....
But C doesn't have objects so this is where the comparison ends...

Also, I don't think the constructor call can specify the amount of memory for the whole object, since it only initializes the class fields (and even that is not required..), thus even though we are creating a new object, it doesn't make sense to have ... new Books() from a memory allocation standpoint. Please explain this too..
Thanks for your help!
Dec 17 '10 #1

✓ answered by weaknessforcats

Yes, the C++ new operator uses malloc and sizeof just like C to allocte memory for an object. However, C stops there. C++ continues on and calls the constructors for all of the member variables and then calls the class constructor. So in C++ you start witn a fully initialized object whreas in C you need to do the initialization yourself.

2 2600
johny10151981
1,059 1GB
sizeof works all the time :)

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. class destiny
  4. {
  5. private:
  6.     int i;
  7. public : 
  8.     void seti(int j)
  9.     {
  10.         i=j;
  11.     }
  12.     int seti()
  13.     {
  14.         return i;
  15.     }
  16. };
  17. int main()
  18. {
  19.  printf("%d\n",sizeof(destiny));
  20.  return 0;
  21. }
  22.  
I am not a C++ Developer but yet. it worked without any error
Dec 17 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
Yes, the C++ new operator uses malloc and sizeof just like C to allocte memory for an object. However, C stops there. C++ continues on and calls the constructors for all of the member variables and then calls the class constructor. So in C++ you start witn a fully initialized object whreas in C you need to do the initialization yourself.
Dec 17 '10 #3

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

Similar topics

3
by: | last post by:
It looks like I have some kind of memory leak in my Visual Basic program. I am grabing about 250K of memory every 10 seconds. I have all kinds of stuff happening (Threads spinning in the...
9
by: R.Z. | last post by:
i was wondering whether it pays off in terms of memory use to maintain lots of empty deques (it would be convenient for my algorithms but memory use is more important). and does the size of a deque...
15
by: Joe Fallon | last post by:
I would like to know how you can figure out how much memory a given instance of a class is using. For example, if I load a collection class with 10 items it might use 1KB, and if I load it with...
1
by: sethwai | last post by:
Hi, I'm curious how people decide the amount of memory available to db2. For example, we have a machine running AIX 5.3 with 32GB of memory. Assuming that the machine is dedicated to DB2 how...
12
by: elty123 | last post by:
I have a small C# program (about 400 lines of code) that is only 28kb after compiled. However when it runs (takes a whole 5 seconds) it takes up nearly 20MB of memory and I don't see why. ...
2
by: Mufasa | last post by:
I've got a system where one of the processes that is running (not done by me) is causing a memory leak. This is a process I call so I'm trying to find out how much memory it's using. What I want to...
4
by: bipi | last post by:
hi everyone, Can show to me how much memory for C program. (It depend on OS, RAM... or some thing like that). Please help me to understand. Thanks,
4
by: FFMG | last post by:
Hi, My Hosting company is saying that I am using too much memory on the VPS and that I should upgrade. I am currently running at 65%-75%, (according to WHM). It does seem a bit high, but I...
7
by: siddhu | last post by:
Dear experts, If I do free(p); memory pointed by p is freed and is available for further allocations in the process. But how does it decide about how much memory (size) has to to be freed and...
1
by: sanddune008 | last post by:
I would like to know how much memory does the following declatations consume? typedef struct { unsigned int type:2; unsigned int Specific:1; unsigned int direction:1; unsigned int...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.