Why is the constructor never called ?

C

codefixer

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);
}
}
 
E

Eric Lilja

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
 
H

Howard

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
 
R

Raghu Uppalli

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
 
C

codefixer

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

Thanks.
 
H

Howard

Raghu Uppalli said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top