473,405 Members | 2,272 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,405 software developers and data experts.

Passing arrays to C funcions

ds
Hi all,

I have to pass an array of doubles to a legacy C function that copies
some data using memcpy. The code would look like this:

extern "C"{
void legacyCFunctionFill(void* arg);
}
....
int number=5;
double *my_array=(double*)calloc(number,sizeof(double));
legacyCFunctionFill((void*)my_array);
// Do sth useful
free(my_array);

The question is: if I change calloc() and free() with new and delete
[] will there be any issues, including portability issues? At first it
seems that it works, having tested that in my program. But I am not
sure if the memory allocated by calloc is the same and can be used the
same way as the memory allocated with new, especially on windows,
Linux and Sun.

Tahnks a lot!!!

-- dimitris

Sep 4 '07 #1
14 1745
On 2007-09-04 17:54, ds wrote:
Hi all,

I have to pass an array of doubles to a legacy C function that copies
some data using memcpy. The code would look like this:

extern "C"{
void legacyCFunctionFill(void* arg);
}
...
int number=5;
double *my_array=(double*)calloc(number,sizeof(double));
legacyCFunctionFill((void*)my_array);
// Do sth useful
free(my_array);

The question is: if I change calloc() and free() with new and delete
[] will there be any issues, including portability issues? At first it
seems that it works, having tested that in my program. But I am not
sure if the memory allocated by calloc is the same and can be used the
same way as the memory allocated with new, especially on windows,
Linux and Sun.
As long as legacyCFunctionFill() does not try to deallocate the memory
using free() everything is fine. There is no difference in the memory
but you can not allocate memory with malloc and deallocate it with
delete (or the other way around).

--
Erik Wikström
Sep 4 '07 #2
ds wrote:
Hi all,

I have to pass an array of doubles to a legacy C function that copies
some data using memcpy. The code would look like this:

extern "C"{
void legacyCFunctionFill(void* arg);
}
...
int number=5;
double *my_array=(double*)calloc(number,sizeof(double));
legacyCFunctionFill((void*)my_array);
// Do sth useful
free(my_array);

The question is: if I change calloc() and free() with new and delete
[] will there be any issues, including portability issues? At first it
seems that it works, having tested that in my program. But I am not
sure if the memory allocated by calloc is the same and can be used the
same way as the memory allocated with new, especially on windows,
Linux and Sun.

Assuming that legacyCFunctionFill() doesn't overrun the buffer (how
*does* it know how much space to fill?), it's fine to use
new[]/delete[]. A pointer is a pointer.
double *my_array = new double[number];
legacyCFunctionFill(my_array); // no cast to void* needed
delete[] my_array; // note use of delete[]

Alternatively:

std::vector<doublemyvec(number);
legacyCFunctionFill(&myvec[0]);
// no deletion necessary
Sep 4 '07 #3
ds
On Sep 4, 6:05 pm, red floyd <no.s...@here.dudewrote:
ds wrote:
Hi all,
I have to pass an array of doubles to a legacy C function that copies
some data using memcpy. The code would look like this:
extern "C"{
void legacyCFunctionFill(void* arg);
}
...
int number=5;
double *my_array=(double*)calloc(number,sizeof(double));
legacyCFunctionFill((void*)my_array);
// Do sth useful
free(my_array);
The question is: if I change calloc() and free() with new and delete
[] will there be any issues, including portability issues? At first it
seems that it works, having tested that in my program. But I am not
sure if the memory allocated by calloc is the same and can be used the
same way as the memory allocated with new, especially on windows,
Linux and Sun.

Assuming that legacyCFunctionFill() doesn't overrun the buffer (how
*does* it know how much space to fill?), it's fine to use
new[]/delete[]. A pointer is a pointer.

double *my_array = new double[number];
legacyCFunctionFill(my_array); // no cast to void* needed
delete[] my_array; // note use of delete[]

Alternatively:

std::vector<doublemyvec(number);
legacyCFunctionFill(&myvec[0]);
// no deletion necessary
Hi floyd,

it is clear that number has to be passed to the legacy function, which
performs a memcpy - no allocation/deallocation on the passed array.
Are you sure that std::vector allocates a continuous block? That would
be a nice solution for what I want to do, but I am afraid that not
all std::vector implementations allocate continuous memory....

thanks a lot!!

Sep 4 '07 #4
ds wrote:
On Sep 4, 6:05 pm, red floyd <no.s...@here.dudewrote:
>>
std::vector<doublemyvec(number);
legacyCFunctionFill(&myvec[0]);
// no deletion necessary

Hi floyd,

it is clear that number has to be passed to the legacy function, which
performs a memcpy - no allocation/deallocation on the passed array.
Are you sure that std::vector allocates a continuous block? That would
be a nice solution for what I want to do, but I am afraid that not
all std::vector implementations allocate continuous memory....
Yeah, TC1 aka 14882:2003 guarantees contiguous memory.

Sep 4 '07 #5
On Sep 4, 9:21 pm, ds <junkmailav...@yahoo.comwrote:
On Sep 4, 6:05 pm, red floyd <no.s...@here.dudewrote:


ds wrote:
Hi all,
I have to pass an array of doubles to a legacy C function that copies
some data using memcpy. The code would look like this:
extern "C"{
void legacyCFunctionFill(void* arg);
}
...
int number=5;
double *my_array=(double*)calloc(number,sizeof(double));
legacyCFunctionFill((void*)my_array);
// Do sth useful
free(my_array);
The question is: if I change calloc() and free() with new and delete
[] will there be any issues, including portability issues? At first it
seems that it works, having tested that in my program. But I am not
sure if the memory allocated by calloc is the same and can be used the
same way as the memory allocated with new, especially on windows,
Linux and Sun.
Assuming that legacyCFunctionFill() doesn't overrun the buffer (how
*does* it know how much space to fill?), it's fine to use
new[]/delete[]. A pointer is a pointer.
double *my_array = new double[number];
legacyCFunctionFill(my_array); // no cast to void* needed
delete[] my_array; // note use of delete[]
Alternatively:
std::vector<doublemyvec(number);
legacyCFunctionFill(&myvec[0]);
// no deletion necessary

Hi floyd,

it is clear that number has to be passed to the legacy function, which
performs a memcpy - no allocation/deallocation on the passed array.
Are you sure that std::vector allocates a continuous block? That would
be a nice solution for what I want to do, but I am afraid that not
all std::vector implementations allocate continuous memory....

thanks a lot!!- Hide quoted text -

- Show quoted text -
continuous memory ??

Sep 4 '07 #6
On 2007-09-04 18:21, ds wrote:
On Sep 4, 6:05 pm, red floyd <no.s...@here.dudewrote:
>ds wrote:
Hi all,
I have to pass an array of doubles to a legacy C function that copies
some data using memcpy. The code would look like this:
extern "C"{
void legacyCFunctionFill(void* arg);
}
...
int number=5;
double *my_array=(double*)calloc(number,sizeof(double));
legacyCFunctionFill((void*)my_array);
// Do sth useful
free(my_array);
The question is: if I change calloc() and free() with new and delete
[] will there be any issues, including portability issues? At first it
seems that it works, having tested that in my program. But I am not
sure if the memory allocated by calloc is the same and can be used the
same way as the memory allocated with new, especially on windows,
Linux and Sun.

Assuming that legacyCFunctionFill() doesn't overrun the buffer (how
*does* it know how much space to fill?), it's fine to use
new[]/delete[]. A pointer is a pointer.

double *my_array = new double[number];
legacyCFunctionFill(my_array); // no cast to void* needed
delete[] my_array; // note use of delete[]

Alternatively:

std::vector<doublemyvec(number);
legacyCFunctionFill(&myvec[0]);
// no deletion necessary

Hi floyd,

it is clear that number has to be passed to the legacy function, which
performs a memcpy - no allocation/deallocation on the passed array.
Are you sure that std::vector allocates a continuous block? That would
be a nice solution for what I want to do, but I am afraid that not
all std::vector implementations allocate continuous memory....
All standards compliant std::vectors allocate continuous blocks of memory.

--
Erik Wikström
Sep 4 '07 #7
ds
On Sep 4, 6:51 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-09-04 18:21, ds wrote:
On Sep 4, 6:05 pm, red floyd <no.s...@here.dudewrote:
ds wrote:
Hi all,
I have to pass an array of doubles to a legacy C function that copies
some data using memcpy. The code would look like this:
extern "C"{
void legacyCFunctionFill(void* arg);
}
...
int number=5;
double *my_array=(double*)calloc(number,sizeof(double));
legacyCFunctionFill((void*)my_array);
// Do sth useful
free(my_array);
The question is: if I change calloc() and free() with new and delete
[] will there be any issues, including portability issues? At first it
seems that it works, having tested that in my program. But I am not
sure if the memory allocated by calloc is the same and can be used the
same way as the memory allocated with new, especially on windows,
Linux and Sun.
Assuming that legacyCFunctionFill() doesn't overrun the buffer (how
*does* it know how much space to fill?), it's fine to use
new[]/delete[]. A pointer is a pointer.
double *my_array = new double[number];
legacyCFunctionFill(my_array); // no cast to void* needed
delete[] my_array; // note use of delete[]
Alternatively:
std::vector<doublemyvec(number);
legacyCFunctionFill(&myvec[0]);
// no deletion necessary
Hi floyd,
it is clear that number has to be passed to the legacy function, which
performs a memcpy - no allocation/deallocation on the passed array.
Are you sure that std::vector allocates a continuous block? That would
be a nice solution for what I want to do, but I am afraid that not
all std::vector implementations allocate continuous memory....

All standards compliant std::vectors allocate continuous blocks of memory.

--
Erik Wikström
I agree. But are all stl implementations complying to the standard?
Especially the MSVC implementation deviates in some cases...

Sep 4 '07 #8
ds wrote:
On Sep 4, 6:51 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>All standards compliant std::vectors allocate continuous blocks of memory.
I agree. But are all stl implementations complying to the standard?
Especially the MSVC implementation deviates in some cases...
MSVC uses Dinkumware, so it should be pretty good. If you're talking
MSVC 7.1 or 8.0, it should be pretty compliant. If you're talking
MSVC6, then you need to upgrade.

Also, please don't quote sigs.
Sep 4 '07 #9
On 2007-09-04 18:44, karthikbalaguru wrote:
On Sep 4, 9:21 pm, ds <junkmailav...@yahoo.comwrote:
>On Sep 4, 6:05 pm, red floyd <no.s...@here.dudewrote:


ds wrote:
Hi all,
I have to pass an array of doubles to a legacy C function that copies
some data using memcpy. The code would look like this:
extern "C"{
void legacyCFunctionFill(void* arg);
}
...
int number=5;
double *my_array=(double*)calloc(number,sizeof(double));
legacyCFunctionFill((void*)my_array);
// Do sth useful
free(my_array);
The question is: if I change calloc() and free() with new and delete
[] will there be any issues, including portability issues? At first it
seems that it works, having tested that in my program. But I am not
sure if the memory allocated by calloc is the same and can be used the
same way as the memory allocated with new, especially on windows,
Linux and Sun.
Assuming that legacyCFunctionFill() doesn't overrun the buffer (how
*does* it know how much space to fill?), it's fine to use
new[]/delete[]. A pointer is a pointer.
double *my_array = new double[number];
legacyCFunctionFill(my_array); // no cast to void* needed
delete[] my_array; // note use of delete[]
Alternatively:
std::vector<doublemyvec(number);
legacyCFunctionFill(&myvec[0]);
// no deletion necessary

Hi floyd,

it is clear that number has to be passed to the legacy function, which
performs a memcpy - no allocation/deallocation on the passed array.
Are you sure that std::vector allocates a continuous block? That would
be a nice solution for what I want to do, but I am afraid that not
all std::vector implementations allocate continuous memory....

thanks a lot!!- Hide quoted text -

- Show quoted text -

continuous memory ??
Meaning that all the elements in the vector will be placed one after
another without any gaps (except padding), in other words that the
elements will be allocated just like in an array.

--
Erik Wikström
Sep 4 '07 #10
On Sep 4, 9:21 am, ds <junkmailav...@yahoo.comwrote:
On Sep 4, 6:05 pm, red floyd <no.s...@here.dudewrote:


ds wrote:
Hi all,
I have to pass an array of doubles to a legacy C function that copies
some data using memcpy. The code would look like this:
extern "C"{
void legacyCFunctionFill(void* arg);
}
...
int number=5;
double *my_array=(double*)calloc(number,sizeof(double));
legacyCFunctionFill((void*)my_array);
// Do sth useful
free(my_array);
The question is: if I change calloc() and free() with new and delete
[] will there be any issues, including portability issues? At first it
seems that it works, having tested that in my program. But I am not
sure if the memory allocated by calloc is the same and can be used the
same way as the memory allocated with new, especially on windows,
Linux and Sun.
Assuming that legacyCFunctionFill() doesn't overrun the buffer (how
*does* it know how much space to fill?), it's fine to use
new[]/delete[]. A pointer is a pointer.
double *my_array = new double[number];
legacyCFunctionFill(my_array); // no cast to void* needed
delete[] my_array; // note use of delete[]
Alternatively:
std::vector<doublemyvec(number);
legacyCFunctionFill(&myvec[0]);
// no deletion necessary

Hi floyd,

it is clear that number has to be passed to the legacy function, which
performs a memcpy - no allocation/deallocation on the passed array.
Are you sure that std::vector allocates a continuous block? That would
be a nice solution for what I want to do, but I am afraid that not
all std::vector implementations allocate continuous memory....

thanks a lot!!- Hide quoted text -

- Show quoted text -
Isn't the correct term "contiguous", not "continuous" ?

Sep 4 '07 #11
Erik Wikstrom wrote:
On 2007-09-04 18:44, karthikbalaguru wrote:
continuous memory ??

Meaning that all the elements in the vector will be placed one after
another without any gaps (except padding), in other words that the
elements will be allocated just like in an array.
I think "contiguous" is somewhat more common, but either should work
fine.

Brian
Sep 4 '07 #12
"blangela" <Bo***********@telus.netwrote in message
news:11**********************@o80g2000hse.googlegr oups.com...
On Sep 4, 9:21 am, ds <junkmailav...@yahoo.comwrote:
>On Sep 4, 6:05 pm, red floyd <no.s...@here.dudewrote:


ds wrote:
Hi all,
I have to pass an array of doubles to a legacy C function that copies
some data using memcpy. The code would look like this:
extern "C"{
void legacyCFunctionFill(void* arg);
}
...
int number=5;
double *my_array=(double*)calloc(number,sizeof(double));
legacyCFunctionFill((void*)my_array);
// Do sth useful
free(my_array);
The question is: if I change calloc() and free() with new and delete
[] will there be any issues, including portability issues? At first
it
seems that it works, having tested that in my program. But I am not
sure if the memory allocated by calloc is the same and can be used
the
same way as the memory allocated with new, especially on windows,
Linux and Sun.
Assuming that legacyCFunctionFill() doesn't overrun the buffer (how
*does* it know how much space to fill?), it's fine to use
new[]/delete[]. A pointer is a pointer.
double *my_array = new double[number];
legacyCFunctionFill(my_array); // no cast to void* needed
delete[] my_array; // note use of delete[]
Alternatively:
std::vector<doublemyvec(number);
legacyCFunctionFill(&myvec[0]);
// no deletion necessary

Hi floyd,

it is clear that number has to be passed to the legacy function, which
performs a memcpy - no allocation/deallocation on the passed array.
Are you sure that std::vector allocates a continuous block? That would
be a nice solution for what I want to do, but I am afraid that not
all std::vector implementations allocate continuous memory....
Isn't the correct term "contiguous", not "continuous" ?
Continuous memory is contiguous, and contiguous memory is continuous.
Contiguous is the offical term, but it means continuous anyway.

From dictionary.com definition #1 (which is what we use) for contiguous:
1. touching; in contact.

From dictoinary.com definition #2 (which woudl apply) for continuous:
2. being in immediate connection or spatial relationship: a continuous
series of blasts; a continuous row of warehouses.

They are fairly synonymous in this context.
Sep 5 '07 #13
ds
red floyd wrote:
ds wrote:
Erik Wikström wrote:
All standards compliant std::vectors allocate continuous blocks of memory.
I agree. But are all stl implementations complying to the standard?
Especially the MSVC implementation deviates in some cases...

MSVC uses Dinkumware, so it should be pretty good. If you're talking
MSVC 7.1 or 8.0, it should be pretty compliant. If you're talking
MSVC6, then you need to upgrade.

Also, please don't quote sigs.
Sorry...

I am talking MSVC 8.0 pro. For example check out this one:

http://www.cplusplus.com/reference/i...uf/setbuf.html

this is not implemented in <sstream- at least in my stl...

Sep 5 '07 #14
On 2007-09-05 10:35, ds wrote:
red floyd wrote:
>ds wrote:
Erik Wikström wrote:
All standards compliant std::vectors allocate continuous blocks of memory.
I agree. But are all stl implementations complying to the standard?
Especially the MSVC implementation deviates in some cases...

MSVC uses Dinkumware, so it should be pretty good. If you're talking
MSVC 7.1 or 8.0, it should be pretty compliant. If you're talking
MSVC6, then you need to upgrade.

Also, please don't quote sigs.

Sorry...

I am talking MSVC 8.0 pro. For example check out this one:

http://www.cplusplus.com/reference/i...uf/setbuf.html

this is not implemented in <sstream- at least in my stl...
What do you mean is not implemented?

#include <sstream>
int main()
{
std::stringstream ss;
char* buf = new char[512];
ss.rdbuf()->setbuf(buf, 512);
}

Fails to compile on my MSVC 8.0 Pro because setbuf() is protected, that
seems like it is implemented to me.

--
Erik Wikström
Sep 5 '07 #15

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

Similar topics

12
by: Kevin Lyons | last post by:
Hello, I am trying to get my select options (courses) passed correctly from the following URL: http://www.dslextreme.com/users/kevinlyons/selectBoxes.html I am having difficulty getting the...
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...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
2
by: dave.harper | last post by:
I'm relatively new to C++, but have a question regarding functions and arrays. I'm passing a relatively large array to a function several thousand times during the course of a loop, and it seems...
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...
3
by: Mark | last post by:
Hi From what I understand, you can pass arrays from classic ASP to .NET using interop, but you have to change the type of the.NET parameter to object. This seems to be because classic ASP passes...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
2
by: sonaliagr | last post by:
I am trying to update a msg array in function by passing the address but it is showing an error. and also, i want the value of msg array to be accessible to the full code that is inside the main...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
1
by: Fizzics | last post by:
This is my first post here at Bytes. I have been trolling it, mostly with the help of Google searches, for some time now. I have done about all of the searching and reading that I really know how to...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.