Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why is the constructor never called ?

Reply
Thread Tools

Why is the constructor never called ?

 
 
codefixer@gmail.com
Guest
Posts: n/a
 
      03-17-2005
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[i].insert(j);
}
}

 
Reply With Quote
 
 
 
 
Eric Lilja
Guest
Posts: n/a
 
      03-17-2005

<(E-Mail Removed)> 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[i].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


 
Reply With Quote
 
 
 
 
Howard
Guest
Posts: n/a
 
      03-17-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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[i].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



 
Reply With Quote
 
Raghu Uppalli
Guest
Posts: n/a
 
      03-17-2005
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

 
Reply With Quote
 
Stefan Strasser
Guest
Posts: n/a
 
      03-17-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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/cpl...us07.html#l109
 
Reply With Quote
 
codefixer@gmail.com
Guest
Posts: n/a
 
      03-17-2005
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


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      03-18-2005

"Raghu Uppalli" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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[i];

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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Why is a button Click event also called when a textbox TextChanged event is called??? S_K ASP .Net 6 11-08-2007 07:44 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
three times copy ctor called, one ctor called, why? Apricot C++ 4 04-16-2004 07:55 AM
Constructor is never called Jun Javascript 4 11-10-2003 12:35 PM



Advertisments