将数组传递给C函数 [英] Passing arrays to C funcions

查看:99
本文介绍了将数组传递给C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我必须将一系列双打传递给传统的C函数,该函数使用memcpy复制

一些数据。代码如下所示:


extern" C" {

void legacyCFunctionFill(void * arg);

}

....

int number = 5;

double * my_array =(double *)calloc(number,sizeof(double)) ;

legacyCFunctionFill((void *)my_array);

//做某事有用

免费(my_array);


问题是:如果我用new更改calloc()和free()并删除

[]会出现任何问题,包括可移植性问题吗?起初,它似乎是有效的,在我的程序中测试了它。但是我不确定是否b / b
确定calloc分配的内存是否相同,并且可以使用

与分配给new的内存相同,特别是在windows上,

Linux和Sun.


Tahnks很多!!!


- dimitris

推荐答案

在2007-09-04 17:54,ds写道:
On 2007-09-04 17:54, ds wrote:

大家好,


我必须将一系列双打传递给传统的C函数,该函数使用memcpy复制

一些数据。代码如下所示:


extern" C" {

void legacyCFunctionFill(void * arg);

}

...

int number = 5;

double * my_array =(double *)calloc(number,sizeof(double));

legacyCFunctionFill((void *)my_array);

//做有用的

免费(my_array);


问题是:如果我用new更改calloc()和free()并删除

[]会有任何问题,包括可移植性问题吗?起初,它似乎是有效的,在我的程序中测试了它。但是我不确定是否b / b
确定calloc分配的内存是否相同,并且可以使用

与分配给new的内存相同,特别是在windows上,

Linux和Sun.
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.



只要legacyCFunctionFill()没有尝试使用free()释放内存

一切都很好。内存没有区别

但你不能用malloc分配内存并用

delete(或者反过来)取消分配它。


-

Erik Wikstr?m

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


ds写道:
ds wrote:

大家好,


我必须将一系列双打传递给传统的C函数,该函数使用memcpy复制

一些数据。代码如下所示:


extern" C" {

void legacyCFunctionFill(void * arg);

}

...

int number = 5;

double * my_array =(double *)calloc(number,sizeof(double));

legacyCFunctionFill((void *)my_array);

//做有用的

免费(my_array);


问题是:如果我用new更改calloc()和free()并删除

[]会有任何问题,包括可移植性问题吗?起初,它似乎是有效的,在我的程序中测试了它。但是我不确定是否b / b
确定calloc分配的内存是否相同,并且可以使用

与分配给new的内存相同,特别是在windows上,

Linux和Sun.

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.



假设legacyCFunctionFill()没有超出缓冲区(

*确实*它知道要填充多少空间?),可以使用

new [] / delete []。指针是一个指针。

double * my_array = new double [number];

legacyCFunctionFill(my_array); //没有施放到无效*需要

delete [] my_array; //注意使用删除[]


或者:


std :: vector< doublemyvec(number);

legacyCFunctionFill(& myvec [0]);

//无需删除

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


9月4日下午6:05,红色floyd< no.s ... @ here.dudewrote:
On Sep 4, 6:05 pm, red floyd <no.s...@here.dudewrote:

ds写道:
ds wrote:

嗨所有,
Hi all,


我必须将一系列双打传递给传统的C函数,该函数使用memcpy复制

一些数据。代码如下所示:
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); < br $>
}

...

int number = 5;

double * my_array =(double *)calloc( number,sizeof(double));

legacyCFunctionFill((void *)my_array);

//做某事有用

free(my_array) ;
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);


问题是:如果我用new更改calloc()和free()并删除

[]会在那里是否有任何问题,包括可移植性问题?起初,它似乎是有效的,在我的程序中测试了它。但是我不确定是否b / b
确定calloc分配的内存是否相同,并且可以使用

与分配给new的内存相同,特别是在windows上,

Linux和Sun.
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.



假设legacyCFunctionFill()没有超出缓冲区(

*怎么做*它知道要填充多少空间?) ,使用

new [] / delete []是没问题的。指针是一个指针。


double * my_array = new double [number];

legacyCFunctionFill(my_array); //没有施放到无效*需要

delete [] my_array; //注意使用删除[]


或者:


std :: vector< doublemyvec(number);

legacyCFunctionFill(& myvec [0]);

//无需删除


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,


显然必须将数字传递给遗留函数,

执行memcpy - 在传递的数组上没有分配/释放。

你确定std :: vector分配了一个连续的块吗?这将是一个很好的解决方案,我想做什么,但我担心不是

所有std :: vector实现分配连续内存....


非常感谢!!

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!!


这篇关于将数组传递给C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆