Passing arrays to C funcions

D

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
 
?

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

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).
 
R

red floyd

ds said:
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<double> myvec(number);
legacyCFunctionFill(&myvec[0]);
// no deletion necessary
 
D

ds

ds said:
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<double> myvec(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!!
 
R

red floyd

ds said:
std::vector<double> myvec(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.
 
K

karthikbalaguru

ds said:
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<double> myvec(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 ??
 
?

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

ds said:
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<double> myvec(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.
 
D

ds

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<double> myvec(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.

I agree. But are all stl implementations complying to the standard?
Especially the MSVC implementation deviates in some cases...
 
R

red floyd

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.
 
?

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

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<double> myvec(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.
 
B

blangela

ds said:
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<double> myvec(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" ?
 
D

Default User

Erik said:
On 2007-09-04 18:44, karthikbalaguru wrote:

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
 
J

Jim Langston

blangela said:
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<double> myvec(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.
 
?

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

Sorry...

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

http://www.cplusplus.com/reference/iostream/stringbuf/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.
 
D

ds

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

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.

It's virtual and not pure. You actually end up here:

virtual _Myt *__CLR_OR_THIS_CALL setbuf(_Elem *, streamsize)
{ // offer buffer to external agent (do nothing)
return (this);
}
Now check the documentation:
http://www.cplusplus.com/reference/iostream/streambuf/setbuf.html and
the link above of what it should do.

Cheers,

dimitris
 
?

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

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.

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

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.

It's virtual and not pure. You actually end up here:

virtual _Myt *__CLR_OR_THIS_CALL setbuf(_Elem *, streamsize)
{ // offer buffer to external agent (do nothing)
return (this);
}
Now check the documentation:
http://www.cplusplus.com/reference/iostream/streambuf/setbuf.html and
the link above of what it should do.

The effect of stetbuf on a stringbuf is implementation-defined as long
as setbuf(0,0); does nothing according to the standard, so that seems
all OK to me.
 

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

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top