compilation of malloc using c++

S

somenath

Hi All,

I need to compile c code using c++ compiler .Inside the c code I am
using malloc and free.
1) My doubt is it ok to compile malloc using c++ compiler?
2)Is behavior will be undefined ?

Regards,
Somenath
 
F

Flash Gordon

somenath wrote, On 13/07/07 16:52:
Hi All,

I need to compile c code using c++ compiler .Inside the c code I am
using malloc and free.
1) My doubt is it ok to compile malloc using c++ compiler?
2)Is behavior will be undefined ?

What you can do with a C++ compiler is topical in a C++ group NOT a C
group, so you should ask there. However, unless there is a financial
reason I very much doubt that compiling C code as C++ is the correct
thing to do, since all C compilers I am aware of come with C compilers
as well and there are well defined methods for interfacing C and C++
code. Again, how you interface C and C++ is a C++ issue for a C++ group,
since it is defined by the C++ side. One good place to discuss what you
can do with a C++ compiler is the surprisingly named comp.lang.c++ group

<OT> malloc/frree are available in C++ but there are important
differences between C and C++ that can trip you up.</OT>
 
J

Jens Thoms Toerring

somenath said:
I need to compile c code using c++ compiler .Inside the c code I am
using malloc and free.
1) My doubt is it ok to compile malloc using c++ compiler?

malloc() and free() are functions, so you don't compiler them,
your program just calls them. But since C++ isn't C you will
have to cast the return value of malloc() to the type of the
pointer you assign the return value to - in contrast to C,
where this usually isn't a good idea.

Regards, Jens
 
K

Kenneth Brody

somenath said:
Hi All,

I need to compile c code using c++ compiler .Inside the c code I am
using malloc and free.
1) My doubt is it ok to compile malloc using c++ compiler?
2)Is behavior will be undefined ?

You would need to ask in comp.lang.c++, down the hall and to the left.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
D

Dave Vandervies

Hi All,

I need to compile c code using c++ compiler .

What are you Really Trying To Do? Compiling code in one language with
a compiler for another language is Always A Bad Idea.
Inside the c code I am
using malloc and free.
1) My doubt is it ok to compile malloc using c++ compiler?

Why are you asking comp.lang.c about C++?

The right answer is to Don't Do That. If you want to use C++, calling
malloc is almost always the wrong way to allocate memory. Use new or
new[] instead; that's what it's there for.

2)Is behavior will be undefined ?

I believe malloc's behavior is perfectly well-defined in C++.


dave
 
S

somenath

somenath said:
I need to compile c code using c++ compiler .

What are you Really Trying To Do? Compiling code in one language with
a compiler for another language is Always A Bad Idea.
Inside the c code I am
using malloc and free.
1) My doubt is it ok to compile malloc using c++ compiler?

Why are you asking comp.lang.c about C++?

The right answer is to Don't Do That. If you want to use C++, calling
malloc is almost always the wrong way to allocate memory. Use new or
new[] instead; that's what it's there for.
2)Is behavior will be undefined ?

I believe malloc's behavior is perfectly well-defined in C++.

dave

--
Dave Vandervies (e-mail address removed)
When other kids are scared of monsters, your kid might turn out to grab a
baseball bat and be disappointed if there WASN'T a monster under the bed...
--Juergen Nieveler in the scary devil monastery

Thanks for the reply.Actually i need to call some API written using c+
+ .So i need to compile my code using c++ else it does not compile.But
in side my code it is required to use malloc.So i was having doubt
will it cause any problem ?
 
F

Flash Gordon

somenath wrote, On 13/07/07 17:35:
On Jul 13, 9:23 pm, (e-mail address removed) (Dave Vandervies)
wrote:

Please don't quote peoples signatures, the bit traditionally after a "--
", in fact, you should only quote what you are responding to.
Thanks for the reply.Actually i need to call some API written using c+
+ .So i need to compile my code using c++ else it does not compile.

No, you need to ask in comp.lang.c++ about what you can do so you don't
need to do that, and they may well tell you about the methods that C++
provides for interfacing with C.
>But
in side my code it is required to use malloc.So i was having doubt
will it cause any problem ?

There could be any of a number of things in your code that will cause a
problem. Using malloc as it is often used in C is not legal C++, but you
have already been told the difference, as to the other things, they are
other things that will give yo grief and reasons not to go this route
but to interface C and C++ properly.
 
R

Richard Heathfield

somenath said:
Hi All,

I need to compile c code using c++ compiler .Inside the c code I am
using malloc and free.
1) My doubt is it ok to compile malloc using c++ compiler?
2)Is behavior will be undefined ?

Compile C code using a C compiler, and C++ code using a C++ compiler.
Compiling C code using a C++ compiler is Just Asking For Trouble.

Link the object code (resulting from the C compilation) to your C++ code
using a linker.

Ask comp.lang.c++ for more advice on joining C and C++ code together.
 
D

Default User

somenath wrote:

Thanks for the reply.Actually i need to call some API written using c+
+ .So i need to compile my code using c++ else it does not compile.But
in side my code it is required to use malloc.So i was having doubt
will it cause any problem ?

Ask C++ questions in the C++ newsgroup, comp.lang.c++.




Brian
 
S

somenath

somenath said:



Compile C code using a C compiler, and C++ code using a C++ compiler.
Compiling C code using a C++ compiler is Just Asking For Trouble.

Link the object code (resulting from the C compilation) to your C++ code
using a linker.

Ask comp.lang.c++ for more advice on joining C and C++ code together.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Actually I can not compile c code and c++ code separately . I am using
third party c code and from that code I need to create object of c++
classes. So I need to compile third part c code using c++ compiler .
 
M

Malcolm McLean

somenath said:
Hi All,

I need to compile c code using c++ compiler .Inside the c code I am
using malloc and free.
1) My doubt is it ok to compile malloc using c++ compiler?
2)Is behavior will be undefined ?
You can use a C++ compiler asa C compiler with a few glitches. Noticeably
the return from malloc() must be cast.

This is generally a bad idea, because you will find all sorts of problem if
you try to mix the C++ class features with malloc(). The C++ people will
tell you all the pitfalls. However it is quite common to see C-like C++ in
production code. That probably tells you why most large software projects go
over budget.
 
D

Default User

somenath wrote:


Actually I can not compile c code and c++ code separately . I am using
third party c code and from that code I need to create object of c++
classes. So I need to compile third part c code using c++ compiler .


Go to comp.lang.c++.



Brian
 
R

Richard Heathfield

somenath said:

Actually I can not compile c code and c++ code separately .

Then learn. It isn't difficult.
I am using
third party c code and from that code I need to create object of c++
classes. So I need to compile third part c code using c++ compiler .

Um, you can't. If you use a C++ compiler, you're using C++ rules, not C
rules, so the code will be treated as if it were written in C++, not C.
Same as if you used a Fortran compiler, the compiler would assume the
source to be written in Fortran.

This can cause no end of confusion. Don't Do It.
 
S

somenath

somenath said:



Then learn. It isn't difficult.


Um, you can't. If you use a C++ compiler, you're using C++ rules, not C
rules, so the code will be treated as if it were written in C++, not C.
Same as if you used a Fortran compiler, the compiler would assume the
source to be written in Fortran.

This can cause no end of confusion. Don't Do It.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Thanks for the answer.I would like to know is there any way of doing
it ? I mean creating c++ object in c code and calling member
functions ?
 
R

Richard Heathfield

somenath said:

Thanks for the answer.I would like to know is there any way of doing
it ? I mean creating c++ object in c code and calling member
functions ?

C doesn't supply a way to do this. I don't /think/ C++ does either, but
you may want to check in comp.lang.c++ for a definitive answer on that
subject.

The normal way of using C and C++ together is to drive the program from
C++, and use C libraries (compiled using a C compiler, naturally) to
perform the well-defined tasks for which they were designed. C++ does
provide a simple and much-used way to do this.

The principal problem with calling C++ methods from C is C++'s penchant
for name-mangling, which makes life very awkward indeed for your C
code. That's why people tend to do it the other way around.
 
S

somenath

somenath said:



C doesn't supply a way to do this. I don't /think/ C++ does either, but
you may want to check in comp.lang.c++ for a definitive answer on that
subject.

The normal way of using C and C++ together is to drive the program from
C++, and use C libraries (compiled using a C compiler, naturally) to
perform the well-defined tasks for which they were designed. C++ does
provide a simple and much-used way to do this.

The principal problem with calling C++ methods from C is C++'s penchant
for name-mangling, which makes life very awkward indeed for your C
code. That's why people tend to do it the other way around.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Thanks for the reply .I thought of doing the other way also. But here
problem is i can not make third party code as library because it runs
as a process from this process we need to call some function written
in c++.For example we need to modify tcp/ip stack for our
requirement .The stack is written in c. And the function we need to
call fom TCP/IP stack code is written in c++ .
 
F

Flash Gordon

somenath wrote, On 14/07/07 11:27:
Here Richard suggested you ask in comp.lang.c++, this was not the first
post in the thread giving you this advice, which is the best advice
anyone on this group can give.

Please don't quote peoples signatures, the bit traditionally after the
"-- ". In fact, only quote the material you are replying to.

requirement .The stack is written in c. And the function we need to
call fom TCP/IP stack code is written in c++ .

How many times to people have to ask you to go over to comp.lang.c++
where you can get correct advice? Are you continuing to post your
requirements here because you want to avoid getting the best advice?
 
B

Ben Bacarisse

<snip>

You are still quoting sigs.
Thanks for the answer.I would like to know is there any way of doing
it ? I mean creating c++ object in c code and calling member
functions ?

If I have this right, you have some C which for whatever reason you
are feeding through a C++ compiler (hence treating it as C++) and you
want to know how to make member calls? I'd suggest, since your code is
being treated as C++, that you just write C++ member calls.

As soon as all those bytes go near a C++ compiler it becomes C++, so
why not make use of that fact? Your problem, then, may well be the
*rest* of the code -- the great pile of C you have from the other
party that you are trying to sneak past a C++ compiler, but if the
only remaining problem is making member calls, just make them -- it is
C++ now after all.

If the other code is problematic (and that is very likely because C !=
C++) then consider making a "glue" library (sometimes called a shim):

C++ code with x.f() you need to call => compile with C++
Small set of functions
 
B

Ben Bacarisse

Ben Bacarisse said:
If the other code is problematic (and that is very likely because C !=
C++) then consider making a "glue" library (sometimes called a shim):

C++ code with x.f() you need to call => compile with C++
Small set of functions

OK bad form to reply to myself but this got sent in error. I was in
the process of removing this section when I hit the send combination!
No glue library will help the OP much.

But I suspect that the "compiling C with C++" is due exactly to what I
suggested as the solution: the OP has to write member calls and in
doing so. Hence my "solution" to how to make member calls is
pointless -- it is probably the reason for the post in the first
place! I usually wait a few minutes before posting, I think this one
would have been kill in its entirety after more thought.
 
S

srimks11

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,479
Members
48,144
Latest member
EllisVelas

Latest Threads

Top