|
-
September 25th, 2008, 12:07 PM
#1
Why do we dont need to mention the sizeof the class when using 'new' operator
Hi, Can anyone give me detailed information why we don't need to pass size when using new operator
for example
class A
{
int x;
public:
A()
{
x = 20;
}
}
int main()
{
A *a1 = new A(); // here how does the compiler know how much memory to allocated. Is it the comipler speciality or the new operator ?
}
-
September 25th, 2008, 12:18 PM
#2
Re: Why do we dont need to mention the sizeof the class when using 'new' operator
The compiler knows the size
-
September 25th, 2008, 12:27 PM
#3
Re: Why do we dont need to mention the sizeof the class when using 'new' operator
Can you explain me how the compiler can able to knew the size ?
-
September 25th, 2008, 12:54 PM
#4
Re: Why do we dont need to mention the sizeof the class when using 'new' operator
well, not in detail, but when you say
"new A()",
you have to include the definition of class A, either via a header file or in your .cpp file. So the compiler can find everything it needs to determine the size of class A.
-
September 25th, 2008, 12:54 PM
#5
Re: Why do we dont need to mention the sizeof the class when using 'new' operator
Because you just told it you want a new "A". It knows how big an A is.
-
September 25th, 2008, 02:16 PM
#6
Re: Why do we dont need to mention the sizeof the class when using 'new' operator
The compiler is capable of figuring this stuff out. After all, if you wanted to allocated an A with malloc, you'd just do
Code:
A* a = (A*)malloc(sizeof(A));
You're still trusting the compiler to figure how much memory to allocate for the A object, via the sizeof operator. New just lets the compiler figure it out internally.
-
September 25th, 2008, 05:17 PM
#7
Re: Why do we dont need to mention the sizeof the class when using 'new' operator
 Originally Posted by rsodimbakam
Hi, Can anyone give me detailed information why we don't need to pass size when using new operator
By defining A you've revealed all the information needed.
This is why private variables (like your int x) also have to be in the definition. In principle those should be hidden from view because it's an implementation detail but they have to be included so the compiler, among other things, can calculate the size of an object.
-
September 26th, 2008, 04:04 AM
#8
Re: Why do we dont need to mention the sizeof the class when using 'new' operator
 Originally Posted by Speedo
After all, if you wanted to allocated an A with malloc, you'd just do
Code:
A* a = (A*)malloc(sizeof(A));
A dangerous thing to do! Ok if the class only contains POD types, but as soon as someone adds a non-POD type, then it will break.
EDIT: Actually, it's not ok at all because the constructor of 'A' will not be called when using 'malloc'.
Last edited by JohnW@Wessex; September 26th, 2008 at 05:22 AM.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
September 26th, 2008, 05:55 AM
#9
Re: Why do we dont need to mention the sizeof the class when using 'new' operator
 Originally Posted by Speedo
The compiler is capable of figuring this stuff out. After all, if you wanted to allocated an A with malloc, you'd just do
Code:
A* a = (A*)malloc(sizeof(A));
You're still trusting the compiler to figure how much memory to allocate for the A object, via the sizeof operator. New just lets the compiler figure it out internally.
As John@Wessex has pointed out, this would only allocate space for an A object, but it will not construct an A object in the allocated space, for that, you need to use placement new.
Anyway an "equivalent" (sort of, but not quite) version of the new expression using malloc would be:
Code:
A* a = (A*)malloc(sizeof(A));
if(a)
{
try
{
new(a) A();
}
catch (...)
{
free(a);
throw;
}
}
else
{
throw std::bad_alloc();
}
And to delete the object you would need to do the following:
Code:
if (a)
{
a->~A();
free(a);
}
You should be aware that malloc never calls the constructor and free never calls the destructor, therefore you should prefer to get into the habbit of using the typesafe new/new[] and delete/delete[] expressions in C++ over malloc/free. If you don't use the new and delete expressions then you will have to do something like the above every time you want to allocate a non-POD type.
If you decide to do the above anyway, then you should be aware that using realloc on memory occupied by non-POD types will constitute undefined behaviour.
-
September 26th, 2008, 06:51 AM
#10
Re: Why do we dont need to mention the sizeof the class when using 'new' operator
 Originally Posted by rsodimbakam
Can you explain me how the compiler can able to knew the size ?
Which compiler? Which version? g++, Visual C++, Digital Mars C++, Comeau C++, CodeWarrior, Visual Age C++, etc...?
That is compiler dependent on how a compiler does what it does. The official rules of the C++ language states that a compiler, regardless of how they do it, must know the size of types at compile time. How a certain compiler happens to do this depends on the compiler.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|