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!
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!
Comment