访问公共类的内存从C ++使用C [英] Accessing public class memory from C++ using C

查看:167
本文介绍了访问公共类的内存从C ++使用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候大家。

我目前正在写在C,C​​多语言PROGRAME ++和Fortran在UNIX上,不幸的是我遇到了分段错误当我尝试和编译后执行。

I'm currently writing a multi-language programe in C, C++ and fortran on UNIX, unfortunatly I run into "Segmentation Error" when I try and execute after compiling.

我已经缩小问题到C ++和我的程序的Ç部分之间的接口。第一部分包括main.ccp和SA.cpp,并且第二CFE.c.的

I've narrowed down the problem to the interface between the C++ and C sections of my program. The first section consists of main.ccp and SA.cpp, and the second CFE.c.

称为SimAnneal在SA.cpp exsists类,公共载体DensityArray和ElementArray。该方案的顺序如下:

A class called 'SimAnneal' exsists in SA.cpp, with public vectors DensityArray and ElementArray. The order of the program follows:


  1. 创建SimAnneal对象'OBJ1和通话功能ObjFunction()

  1. Create SimAnneal Object 'Obj1' and call function ObjFunction()

这函数初始化向量大小

与指针载体和它们的长度通话CFE(...)。

Call CFE(...) with pointers to both vectors and their length.

CFE.c通过使用指针直接编辑的矢量的数据元素

CFE.c edits the data elements of the vectors directly via use of the pointers

ObjFunction()使用EnergyArray(以及可能的DensityArray)数据。

ObjFunction() uses EnergyArray (and possible DensityArray) data.

相关的脚本是下面所有来源:

The relevant script is below for all sources:

的main.cpp

#include "SA.h" 

int main() 
{   
    SimAnneal Obj1;

    Obj1.ObjFunction();

    return 0;
}

SA.h

class SimAnneal 
{
    void Initialize ();
    ...
  public
    std::vector<float> DensityArray; 
    std::vector<float> EnergyArray;
    double ObjFunction ();
    ...
}

SA.cpp

SA.cpp

#include "CFE.h"

void SimAnneal::Initialize ()
{
    int length = 15;
    EnergyArray.resize(length);
DensityArray.resize(length);
}

double SimAnneal::ObjFunction () 
{
    Initialize ();

    CFE(&DensityArray[0], &EnergyArray[0], DensityArray.size()); 

      // sends pointers of both arrays to CFE.c, which will then 
      // directly modify the array

    double SumStrainEnergy = 0;

    for (int i = 0; i < EnergyArray.size(); i++)
    {
        SumStrainEnergy += EnergyArray[i];  //Effectively sum of array 
                                            //engy[] from CFE.c
    }

    return SumStrainEnergy;
}

CFE.h

#ifdef __cplusplus
extern "C" {
#endif 

void CFE(float density[], float energy[], int NumElem);

#ifdef __cplusplus
 }
#endif

CFE.c

void CFE(float density[], float energy[], int NumElem)
{
    ...

    float * dens;
    dens = density;  //pass pointer of array density[0] in SA.cpp to CFE.c

    for(n=0; n<NumElem; n++)
    { ... modify dens (e.g. DensityArray from SA.cpp) ... }

    float * engy;
    engy = energy; //pass pointer of array energy[0] in SA.cpp to CFE.c

    for(n=0; n<NumElem; n++)
    { ... modify engy (e.g. EnergyArray from SA.cpp) ... }   
}

我是不是造成通过尝试从我的程序中C部分访问向量元素一个非法的内存访问?有什么法子让这个?

Am I causing an illegal memory access by trying to access the vector elements from the C portion of my program? Is there any sure way to allow this?

任何帮助将大大appriciated。

Any help would be much appriciated.

推荐答案

只要你留在载体的范围内,你在做什么,似乎是确定。

Provided you stay within the bounds of the vector, what you are doing would seem to be OK.

您可以把一个std :: vector的完全一样,如果它是通过做你正在做什么C数组 - 以第一个元素的地址。 C ++标准已改为特别允许这种用法。

找不到的C副本++技术勘误2003在present,但显然相关部门裁判是23.2.4,

Can't find a copy of C++ the Technical Corrigendum 2003 at present, but apparently the relevant section ref is 23.2.4,

这篇关于访问公共类的内存从C ++使用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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