473,499 Members | 1,808 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is the constructor never called ?

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

Jul 23 '05 #1
6 1469

<co*******@gmail.com> 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
Jul 23 '05 #2

<co*******@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr 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

Jul 23 '05 #3
co*******@gmail.com 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
Jul 23 '05 #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

Jul 23 '05 #5
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


Jul 23 '05 #6

"Raghu Uppalli" <ra*********@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr 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

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
13191
by: Robin Forster | last post by:
I have two classes: aule_gl_window (parent class) and aule_button (sub class) I want to call the super class (parent) constructor code from the sub class constructor.
3
4527
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
4
1594
by: Kench | last post by:
Sorry if this becomes a repost. I posted this to comp.lang.c++.moderated 1 hour ago still it does not show up there so posting this here. Hi, Consider class A & B both of which implement a copy...
23
5135
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
24
3706
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
9
2353
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
74
15865
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
12
7169
by: Rahul | last post by:
Hi Everyone, I have the following code and i'm able to invoke the destructor explicitly but not the constructor. and i get a compile time error when i invoke the constructor, why is this so? ...
9
23719
by: Morten Lemvigh | last post by:
Is it possible to pass a pointer to a constructor or a class definition as argument to a function? Maybe in a way similar to passing function pointers...? The function should construct a number...
14
1921
by: Sweeya | last post by:
/*Program to do manipulations on a string*/ #include <iostream> using namespace std; class String { int len; char *p;
0
7131
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3099
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.