Accessing public class memory from C++ using C

2019-03-01 10:23发布

问题:

Greetings Everyone.

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.

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.

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

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

  2. That function initializes the vector sizes

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

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

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

#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) ... }   
}

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?

Any help would be much appriciated.

回答1:

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

You can treat a std::vector exactly as if it were a C array by doing what you are doing - taking the address of the first element. The C++ Standard has been changed to specifically allow this kind of usage.

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



回答2:

The code you posted appears to be OK - you'll need to give more detail if you want the problem debugged. Actually, if you run the program in a debugger, it should be able to tell you exactly which line of code is causing the exception (you may have to look in a call stack), or simply step through the program until it crashes.

As for the confusion about whether vector can be treated as a C array, it definitely can by getting the address of the first element (ie., &vect[0]) - if the vector contains elements.

The C++03 standard says this about vector<> in 23.2.4:

The elements of a vector are stored contiguously, meaning that if v is a vector<T, Allocator> where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size()

Note that this was not explicitly stated in the C++98 standard (but was still the intent).

See also Herb Sutter's article:

  • http://herbsutter.wordpress.com/2008/04/07/cringe-not-vectors-are-guaranteed-to-be-contiguous/

Note that vector<bool> cannot be treated as a C array - it a special case since the elements in vector<bool> are not stored as bool.



回答3:

You can't do that. Vector class is not the same as a C-like array.

You must convert it to a regular C array before passing it to CFE function.

Edit: Apparently my answer is wrong. Check Neil's post.



回答4:

The code that you've posted is correct. Provided that every access to an array element inside of CFE() is within bounds, you shouldn't be getting a segmentation fault. Try running your program under valgrind and see if it reports anything unusual.



回答5:

What's the content of CFE() ?

Why not define CFE() as; void CFE(float *density, float *energy, int NumElem);

So you don't have to fool around with casts and just do; density[i] = ... inside your loops?



标签: c++ c memory class