473,323 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Problem with calloc

ds
Hello all,

I have a problem with calloc I have never seen before. I am migrating
some C code to C++ and at some part in the C code there is a calloc
that creates an array of structures. Now in the C++ version, the
structures are classes with virtual access functions. I am working on
linux with g++ and it seems that the _vptr part of the class is null.
Further use of these classes causes a segmentation fault. Of course if
I use new everything is ok. I wonder if this is a limitation of the
calloc implementation or if it is platform specific...

Thanks a lot!!

Feb 12 '07 #1
7 3727
ds wrote:
Hello all,

I have a problem with calloc I have never seen before. I am migrating
some C code to C++ and at some part in the C code there is a calloc
that creates an array of structures. Now in the C++ version, the
structures are classes with virtual access functions. I am working on
linux with g++ and it seems that the _vptr part of the class is null.
Further use of these classes causes a segmentation fault. Of course if
I use new everything is ok. I wonder if this is a limitation of the
calloc implementation or if it is platform specific...
calloc just fills all the allocated bytes with zero. This might be
sufficient for POD types, but for non-POD classes, it isn't. The objects
must be properly constructed, and calloc doesn't do this. That's why there
is operator new.

Feb 12 '07 #2
"ds" <ju***********@yahoo.comwrote in message
news:11*********************@h3g2000cwc.googlegrou ps.com...
Hello all,

I have a problem with calloc I have never seen before. I am migrating
some C code to C++ and at some part in the C code there is a calloc
that creates an array of structures. Now in the C++ version, the
structures are classes with virtual access functions. I am working on
linux with g++ and it seems that the _vptr part of the class is null.
Further use of these classes causes a segmentation fault. Of course if
I use new everything is ok. I wonder if this is a limitation of the
calloc implementation or if it is platform specific...

Thanks a lot!!
Yeah, pain in the neck isn't it? That is one big difference between C's
structures and C++'s classes. A class that is not POD doesn't necessesarily
want everything to be initialized to 0. This is something I ran across when
working with some C code converting it to c++. The C code had something
like:

struct Foo
{
char String[100];
int SomeInt;
};

Foo* Bar;

Bar = malloc( sizeof Foo );
// Now fill the memory with 0's

Well, this was actually taking place in very differnet places (global
variables, etc...). And I innocently tried to add a class to the structure.
The same as:

struct Foo
{
std::string Foo;
int SomeInt;
}

And it took me a while to track down the zero filling of the memory and that
the constructor was never being called.

It's usually not a simple search and replace to fix this code, because some
C code can get a little spagetti like, I spent days tracking down all the
places this program was zero allocating memory.
Feb 12 '07 #3
Jim Langston wrote:
"ds" <ju***********@yahoo.comwrote in message
news:11*********************@h3g2000cwc.googlegrou ps.com...
>Hello all,

I have a problem with calloc I have never seen before. I am migrating
some C code to C++ and at some part in the C code there is a calloc
that creates an array of structures. Now in the C++ version, the
structures are classes with virtual access functions. I am working on
linux with g++ and it seems that the _vptr part of the class is null.
Further use of these classes causes a segmentation fault. Of course
if I use new everything is ok. I wonder if this is a limitation of
the calloc implementation or if it is platform specific...

Thanks a lot!!

Yeah, pain in the neck isn't it? That is one big difference between
C's structures and C++'s classes. A class that is not POD doesn't
necessesarily want everything to be initialized to 0. This is
something I ran across when working with some C code converting it to
c++. The C code had something like:

struct Foo
{
char String[100];
int SomeInt;
};

Foo* Bar;

Bar = malloc( sizeof Foo );
// Now fill the memory with 0's

Well, this was actually taking place in very differnet places (global
variables, etc...). And I innocently tried to add a class to the
structure. The same as:

struct Foo
{
std::string Foo;
int SomeInt;
}

And it took me a while to track down the zero filling of the memory
and that the constructor was never being called.
Just to explain Jim's point: do NOT use 'malloc' or 'calloc' for non-
POD types (especially when they can or do contain virtual functions)
because that's going to interfere with (or simply forgo) proper
construction of your objects.

What's the alternative? 'new' and 'new[]'. When in Rome...
It's usually not a simple search and replace to fix this code,
because some C code can get a little spagetti like, I spent days
tracking down all the places this program was zero allocating memory.
"Zero allocating" sounds wrong. You meant 'zero-initialising', right?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 12 '07 #4
On Feb 12, 4:10 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Jim Langston wrote:
"ds" <junkmailav...@yahoo.comwrote in message
news:11*********************@h3g2000cwc.googlegrou ps.com...
Hello all,
I have a problem with calloc I have never seen before. I am migrating
some C code to C++ and at some part in the C code there is a calloc
that creates an array of structures. Now in the C++ version, the
structures are classes with virtual access functions. I am working on
linux with g++ and it seems that the _vptr part of the class is null.
Further use of these classes causes a segmentation fault. Of course
if I use new everything is ok. I wonder if this is a limitation of
the calloc implementation or if it is platform specific...
Thanks a lot!!
Yeah, pain in the neck isn't it? That is one big difference between
C's structures and C++'s classes. A class that is not POD doesn't
necessesarily want everything to be initialized to 0. This is
something I ran across when working with some C code converting it to
c++. The C code had something like:
struct Foo
{
char String[100];
int SomeInt;
};
Foo* Bar;
Bar = malloc( sizeof Foo );
// Now fill the memory with 0's
Well, this was actually taking place in very differnet places (global
variables, etc...). And I innocently tried to add a class to the
structure. The same as:
struct Foo
{
std::string Foo;
int SomeInt;
}
And it took me a while to track down the zero filling of the memory
and that the constructor was never being called.

Just to explain Jim's point: do NOT use 'malloc' or 'calloc' for non-
POD types (especially when they can or do contain virtual functions)
because that's going to interfere with (or simply forgo) proper
construction of your objects.

What's the alternative? 'new' and 'new[]'. When in Rome...
My advice would be to replace malloc and friends with new, std::vector
and std::string. This should be the first step in a conversion, the
second being to write constructors and destructors and ensure RAII is
in use everywhere. There is no reason to use new[] unless you are in a
very special situation.
>
It's usually not a simple search and replace to fix this code,
because some C code can get a little spagetti like, I spent days
tracking down all the places this program was zero allocating memory.

"Zero allocating" sounds wrong. You meant 'zero-initialising', right?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask- Hide quoted text -

- Show quoted text -

Feb 12 '07 #5
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:eq**********@news.datemas.de...
Jim Langston wrote:
>"ds" <ju***********@yahoo.comwrote in message
news:11*********************@h3g2000cwc.googlegro ups.com...
>>Hello all,

I have a problem with calloc I have never seen before. I am migrating
some C code to C++ and at some part in the C code there is a calloc
that creates an array of structures. Now in the C++ version, the
structures are classes with virtual access functions. I am working on
linux with g++ and it seems that the _vptr part of the class is null.
Further use of these classes causes a segmentation fault. Of course
if I use new everything is ok. I wonder if this is a limitation of
the calloc implementation or if it is platform specific...

Thanks a lot!!

Yeah, pain in the neck isn't it? That is one big difference between
C's structures and C++'s classes. A class that is not POD doesn't
necessesarily want everything to be initialized to 0. This is
something I ran across when working with some C code converting it to
c++. The C code had something like:

struct Foo
{
char String[100];
int SomeInt;
};

Foo* Bar;

Bar = malloc( sizeof Foo );
// Now fill the memory with 0's

Well, this was actually taking place in very differnet places (global
variables, etc...). And I innocently tried to add a class to the
structure. The same as:

struct Foo
{
std::string Foo;
int SomeInt;
}

And it took me a while to track down the zero filling of the memory
and that the constructor was never being called.

Just to explain Jim's point: do NOT use 'malloc' or 'calloc' for non-
POD types (especially when they can or do contain virtual functions)
because that's going to interfere with (or simply forgo) proper
construction of your objects.

What's the alternative? 'new' and 'new[]'. When in Rome...
>It's usually not a simple search and replace to fix this code,
because some C code can get a little spagetti like, I spent days
tracking down all the places this program was zero allocating memory.

"Zero allocating" sounds wrong. You meant 'zero-initialising', right?
Yeah, I guess I just made "zero allocating" up without even realizing it,
meaning allocating memory then zero filling it.
Feb 12 '07 #6
ds
Hello all

and thanks for the answers!!! I really got unstuck, I was wondering
why calloc wouldn't allocate memory the same way for an object just as
new (since I already could observe the difference). Note, that without
virtual methods, calloc works. However it is true that the object is
not constructed appropriately as is the case with new. However, if I
explicitly construct the elements using

::new ((void*)ptr) TYPE;

where ptr is the pointer to the object and TYPE the type of the
object, the object is properly constructed. I of course understand
that this is not the correct way. However, since I plan to perform the
migration iteratively, I would like for the moment to maintain the "C"
styke memory management, check that the algorithm works, and change it
alltogether to any prper C++ memory management scheme that we will
decide on.

Thanks for all the answers and help!!!

Feb 12 '07 #7
ds wrote:
Hello all

and thanks for the answers!!! I really got unstuck, I was wondering
why calloc wouldn't allocate memory the same way for an object just as
new (since I already could observe the difference). Note, that without
virtual methods, calloc works. However it is true that the object is
not constructed appropriately as is the case with new. However, if I
explicitly construct the elements using
>>new ((void*)ptr) TYPE;
No need to cast to void*.
where ptr is the pointer to the object and TYPE the type of the
object, the object is properly constructed. I of course understand
that this is not the correct way.
Actually, it's fine. It's called "placement new", and it exists
specifically for doing what you did, constructing in the memory you
have acquired somehow, beforehand.

Just don't 'delete' that pointer. Use the explicit destructor call
and then deallocate your memory as you normally would:

ptr->~TYPE();
free(ptr);
However, since I plan to perform the
migration iteratively, I would like for the moment to maintain the "C"
styke memory management, check that the algorithm works, and change it
alltogether to any prper C++ memory management scheme that we will
decide on.
Up to you.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 12 '07 #8

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

Similar topics

29
by: David Hill | last post by:
Is there a difference between: /* code 1 */ struct sample test; test = malloc(sizeof(struct sample)); memset(&test, 0, sizeof(test)); /* code 2 */ struct sample test; test = calloc(1,...
6
by: kdogksu | last post by:
I am trying to dynamically allocate memory to be used as an array. The array is pointed to by the 'vert' member of the structure 'grid'. I wish for this array to be a 2 dimensional array of the...
37
by: Harsimran | last post by:
Can any one explain what are far pointers and what is the difference between malloc and calloc .Which is better ?
7
by: Emerson | last post by:
Hi all: my problem is in the comments of the code . would u please give me some clue what 's the problem? what u in advance. /*******************************/ #include "stdio.h" #include...
2
by: chingfulan | last post by:
I have the following code and I can not figure out why Stg2In returns a null pointer? "Stg2In = (float *)calloc(9*DataLen, sizeof(float)); " while "Stg2Out = (float *)calloc(9*DataLen,...
7
by: mef526 | last post by:
I have had this problem for months now and it has been nagging me. I have a large project that has several C++ DLL's, one of them uses malloc / calloc / free for the other DLL's. I process very...
14
by: Harry | last post by:
Good Day, Here is a structure that i am using in my code typedef struct storable_picture { PictureStructure structure; int poc;
29
by: marvinla | last post by:
Hello! I'm a beginner in C, and I'm having trouble with a pointer-to-pointer reallocation. This piece of code works well, but Valkyrie warns some parts (pointed below), and is breaking my real...
10
by: Igal | last post by:
hay, i'm doing this program. having problem wiht realloc in the function that reads data structures into array (pointer - bp2), this happens after reading the second record. when call to realloc....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.