Why is the constructor never called ?

Discussion in 'C++' started by codefixer@gmail.com, Mar 17, 2005.

  1. Guest

    Hi,

    I don't seem to understand why the constructor is never called.
    It compiles fine and executes fine....

    Thanks.

    "calloc_t.h"

    class foo
    {
    int bar[1024];
    public:
    foo::foo(){ cout << "\n Cosntructing foo....";}
    ~foo(){ cout << "\n destructing foo....";}

    void insert(int);
    int get(int) const;

    };

    void foo::insert(int temp)
    {
    this->bar[temp]=temp;
    }

    int foo::get(int temp) const
    {
    // cout << "\n Value = " << this->bar[temp];
    return(this->bar[temp]);
    }


    int main ()
    {
    int i,n,j;
    foo *ptr_foo;
    i = 1024;
    ptr_foo = (foo*) calloc (i,sizeof(foo));
    if (ptr_foo==NULL)
    {
    cout << "\n Calloc failed";
    exit (1);
    }
    for (i=0;i<1024;i++)
    {
    for (j=0;j<1024;j++)
    {
    ptr_foo.insert(j);
    }
    }
    , Mar 17, 2005
    #1
    1. Advertising

  2. Eric Lilja Guest

    <> wrote in message
    > Hi,
    >
    > I don't seem to understand why the constructor is never called.
    > It compiles fine and executes fine....
    >
    > Thanks.
    >
    > "calloc_t.h"
    >
    > class foo
    > {
    > int bar[1024];
    > public:
    > foo::foo(){ cout << "\n Cosntructing foo....";}
    > ~foo(){ cout << "\n destructing foo....";}
    >
    > void insert(int);
    > int get(int) const;
    >
    > };
    >
    > void foo::insert(int temp)
    > {
    > this->bar[temp]=temp;
    > }
    >
    > int foo::get(int temp) const
    > {
    > // cout << "\n Value = " << this->bar[temp];
    > return(this->bar[temp]);
    > }
    >
    >
    > int main ()
    > {
    > int i,n,j;
    > foo *ptr_foo;
    > i = 1024;
    > ptr_foo = (foo*) calloc (i,sizeof(foo));
    > if (ptr_foo==NULL)
    > {
    > cout << "\n Calloc failed";
    > exit (1);
    > }
    > for (i=0;i<1024;i++)
    > {
    > for (j=0;j<1024;j++)
    > {
    > ptr_foo.insert(j);
    > }
    > }
    >


    malloc(), calloc() and friends allocate raw memory and doesn't know about
    constructors (and free() doesn't know about destructors either). So always
    use new and delete for C++ programs. For more details, see Scott Meyers
    books.

    / Eric
    Eric Lilja, Mar 17, 2005
    #2
    1. Advertising

  3. Howard Guest

    <> wrote in message
    news:...
    > Hi,
    >
    > I don't seem to understand why the constructor is never called.
    > It compiles fine and executes fine....
    >
    > Thanks.
    >
    > "calloc_t.h"
    >
    > class foo
    > {
    > int bar[1024];
    > public:
    > foo::foo(){ cout << "\n Cosntructing foo....";}
    > ~foo(){ cout << "\n destructing foo....";}
    >
    > void insert(int);
    > int get(int) const;
    >
    > };
    >
    > void foo::insert(int temp)
    > {
    > this->bar[temp]=temp;
    > }
    >
    > int foo::get(int temp) const
    > {
    > // cout << "\n Value = " << this->bar[temp];
    > return(this->bar[temp]);
    > }
    >
    >
    > int main ()
    > {
    > int i,n,j;
    > foo *ptr_foo;
    > i = 1024;
    > ptr_foo = (foo*) calloc (i,sizeof(foo));
    > if (ptr_foo==NULL)
    > {
    > cout << "\n Calloc failed";
    > exit (1);
    > }
    > for (i=0;i<1024;i++)
    > {
    > for (j=0;j<1024;j++)
    > {
    > ptr_foo.insert(j);
    > }
    > }
    >


    The constructor is never called because nothing in the above code ever
    creates a foo object. You simply allocate enough storage for 1024 of them.
    You should be either using new (and delete[]), or simply declaring an array
    of foo of the constant size ([1024]) that you need. If you insist on using
    calloc (or malloc), then you need to use the "placement new" syntax to
    construct objects in the memory that you've allocated. Search for
    "placement new" on Google or in your favorite C++ book.

    -Howard
    Howard, Mar 17, 2005
    #3
  4. It works, but it is not the preferred way of handling memory in C++. Do
    not use calloc/malloc etc. Use operator 'new' instead. The constructor
    will be called then. Constructors are not called when pointers are
    declared 'coz the object is not created there.

    Nor is it called on calloc() etc which does not know about the type of
    object. It just hands you a chunk of memory. Do the following in your
    main()

    int i = 1024,n,j;
    foo *ptr_foo = new foo();

    --Raghu
    Raghu Uppalli, Mar 17, 2005
    #4
  5. schrieb:
    > Hi,
    >
    > I don't seem to understand why the constructor is never called.
    > It compiles fine and executes fine....
    >
    > Thanks.
    >
    > ptr_foo = (foo*) calloc (i,sizeof(foo));


    because you're not constructing anything but allocating a chunk of
    memory. read this:
    http://www.icce.rug.nl/documents/cplusplus/cplusplus07.html#l109
    Stefan Strasser, Mar 17, 2005
    #5
  6. Guest

    Thanks everybody. I guessed so, since I was just pointing to foo, but
    wasn't really sure.

    Thanks.

    Raghu Uppalli wrote:
    > It works, but it is not the preferred way of handling memory in C++.

    Do
    > not use calloc/malloc etc. Use operator 'new' instead. The

    constructor
    > will be called then. Constructors are not called when pointers are
    > declared 'coz the object is not created there.
    >
    > Nor is it called on calloc() etc which does not know about the type

    of
    > object. It just hands you a chunk of memory. Do the following in your
    > main()
    >
    > int i = 1024,n,j;
    > foo *ptr_foo = new foo();
    >
    > --Raghu
    , Mar 17, 2005
    #6
  7. Howard Guest

    "Raghu Uppalli" <> wrote in message
    news:...
    > It works, but it is not the preferred way of handling memory in C++. Do
    > not use calloc/malloc etc. Use operator 'new' instead. The constructor
    > will be called then. Constructors are not called when pointers are
    > declared 'coz the object is not created there.
    >
    > Nor is it called on calloc() etc which does not know about the type of
    > object. It just hands you a chunk of memory. Do the following in your
    > main()
    >
    > int i = 1024,n,j;
    > foo *ptr_foo = new foo();


    You mean:

    foo * ptr_foo = new foo;

    Right? He was creating 1024 of those, not just one.

    or, better:

    foo a_foo[1024];

    or, better still:

    vector<foo> v_foo(1024);

    -Howard
    Howard, Mar 18, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Soren Kuula
    Replies:
    1
    Views:
    448
    Henry S. Thompson
    Dec 1, 2005
  2. Kevin
    Replies:
    4
    Views:
    423
    Irrwahn Grausewitz
    Oct 17, 2003
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,840
    Smokey Grindel
    Dec 2, 2006
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,222
  5. Jun

    Constructor is never called

    Jun, Nov 9, 2003, in forum: Javascript
    Replies:
    4
    Views:
    152
    Lasse Reichstein Nielsen
    Nov 10, 2003
Loading...

Share This Page