473,377 Members | 1,151 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,377 software developers and data experts.

Passing memory from C++ to C

I have a C and a CPP program. The C program calls the CPP program. The CPP program needs to pass a new block of memory back to the C program. If I use "new" in the CPP program what do I use in the C program to free the memory? Should I just use malloc in the CPP program? Thanks, Jim
Nov 17 '05 #1
2 1358
>I have a C and a CPP program. The C program calls the CPP program. The CPP program needs to pass a new block of memory back to the C program. If I use "new" in the CPP program what do I use in the C program to free the memory? Should I just use malloc in the CPP program?

Jim,

Do you have 2 programs, or 2 different source modules?

I assume you've got the latter, in which case, you should be fine
using malloc and free since both modules should link to the same
run-time library.

If you've got a situation where you have an application written in 'C'
and a DLL in C++ (or vice-versa), the situation is essentially the
same, but a little more involved as both the EXE & DLL need to be
built with the DLL version of the run-time - so that they use a common
heap.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
Nov 17 '05 #2
As I understand it, memory allocated with malloc() MUST be released with
free(), and memory allocated with new() (which can do MUCH more than
allocate memory, in fact, can do ANYTHING), MUST be released with delete
(exception: managed (__gc garbage collected) classes don't require delete).

So, you CAN'T release memory allocated using new() in a C++ application in
a vanilla C application the 'pointer' returned is passed to. Worse, the
pointer returned from a new() is to the object, while malloc() returns a
pointer to the memory allocated. I'm not sure, but I believe these are not
the same thing.

You must release memory at the same 'level' it was created in.....(sorry,
but that's the way it is)

"Jim Cutler" <an*******@discussions.microsoft.com> wrote in message
news:06**********************************@microsof t.com...
I have a C and a CPP program. The C program calls the CPP program. The

CPP program needs to pass a new block of memory back to the C program. If I
use "new" in the CPP program what do I use in the C program to free the
memory? Should I just use malloc in the CPP program? Thanks, Jim
Nov 17 '05 #3

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

Similar topics

12
by: harry | last post by:
I have an object that's passed in to a function as a parameter i.e public boolean getProjectTitle(ProjectHeader_DTO obj) {...} If I then call a method on this object inside the function i.e...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
25
by: Victor Bazarov | last post by:
In the project I'm maintaining I've seen two distinct techniques used for returning an object from a function. One is AType function(AType const& arg) { AType retval(arg); // or default...
2
by: Morgan | last post by:
Thanks to all of you because I solved the problem related with my previous post. I simply made confusion with pointers to pointers and then succeeded passing the reference to the first element...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
15
by: mangesh | last post by:
This code is from c++ faq in section 11 : void someCode() { char memory; void* p = memory; Fred* f = new(p) Fred(); f->~Fred(); // Explicitly call the destructor for the placed object }
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
3
by: DaTurk | last post by:
If I call this method, and pass it a byte by ref, and initialize another byte array, set the original equal to it, and then null the reference, why is the original byte array not null as well? I...
7
by: pereges | last post by:
which one do you think is better ? I need to make my program efficient and in some places I have passed the copy of a variable which makes life some what easy while writing huge expressions but...
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...
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.