compilation of malloc using c++

S

somenath

Hi All,

I need to compile c code using c++ compiler .As i need to create
object of classes inside the c functions I am using malloc and
free.
1) My doubt is it ok to compile "c"code using c++ compiler?
2) Is behavior will be undefined ?



Regards,
Somenath
 
E

EasyVoip

Hi All,

I need to compile c code using c++ compiler .As i need to create
object of classes inside the c functions I am using malloc and
free.
1) My doubt is it ok to compile "c"code using c++ compiler?
2) Is behavior will be undefined ?

Regards,
Somenath

In MSVS 2005 there is an option in project setting to compile it as C
code.

This is could be one option...
 
J

Jim Langston

somenath said:
Hi All,

I need to compile c code using c++ compiler .As i need to create
object of classes inside the c functions I am using malloc and
free.
1) My doubt is it ok to compile "c"code using c++ compiler?
2) Is behavior will be undefined ?

Is it okay to compile c code in a c++ compiler? Yes, although you may need
to make a few modifications for small changes.

"I need to create object of classes inside the c functions". Please
explain?
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi All,

I need to compile c code using c++ compiler .As i need to create
object of classes inside the c functions I am using malloc and
free.

Note that since C does not have classes, as soon as you use them you are
in effect programming in C++, and should probably treat the code as
such. Note also that there are important differences between allocating
objects using new and malloc. Given a class Test doing

Test* t = (Test*)malloc(sizeof(Test) * 10);

and

Test* t = new Test[10];

the first will only allocate the memory needed while the second will
also call the constructors for each object in the array (which is
important since you can't use an object unless you called it's
constructor has been run).
1) My doubt is it ok to compile "c"code using c++ compiler?

Most C code will probably fall in the set that is common to C and C++,
but there might be some things that have to be changed.
2) Is behavior will be undefined ?

If you try to use an object you allocated memory for using malloc, yes.
 

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

Forum statistics

Threads
474,291
Messages
2,571,453
Members
48,137
Latest member
IndiraMcCo

Latest Threads

Top