CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2008
    Posts
    48

    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 ?
    }

  2. #2
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Why do we dont need to mention the sizeof the class when using 'new' operator

    The compiler knows the size

  3. #3
    Join Date
    Sep 2008
    Posts
    48

    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 ?

  4. #4
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    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.

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  6. #6
    Join Date
    Aug 2007
    Posts
    858

    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.

  7. #7
    Join Date
    Nov 2003
    Posts
    1,405

    Re: Why do we dont need to mention the sizeof the class when using 'new' operator

    Quote 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.

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Why do we dont need to mention the sizeof the class when using 'new' operator

    Quote 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

  9. #9
    Join Date
    May 2007
    Location
    Scotland
    Posts
    1,164

    Re: Why do we dont need to mention the sizeof the class when using 'new' operator

    Quote 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.

  10. #10
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why do we dont need to mention the sizeof the class when using 'new' operator

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured