views:

294

answers:

5

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.

A: 

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.

Pablo Santa Cruz
...or provide safe accessors to it.
plinth
He takes the adress of the first element - this is the same as the name of a C array.
anon
I is the same as the name of a C array indeed. But I think he should not rely on how memory is laid out inside Vector class. It could change. Safe bet will be convert it to a regular C array.
Pablo Santa Cruz
See my answer for why this is wrong
anon
Wow... Didn't know that. Thanks a lot.
Pablo Santa Cruz
+10  A: 

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,

anon
Wow. Didn't know that. Voting up! Can you provide a link to the section of the Standard that states this? Thanks!
Pablo Santa Cruz
It's in the Technical Corrigenda - I don't have a copy on this machine but will try to locate one.
anon
The draft standard (not official) can be found at ftp://ftp.research.att.com/dist/c++std/WP/CD2/ . See 23.1.1 paragraph 9, 23.1 paragraph 7, 23.2.4 paragraph 1, and 24.1.5 paragraph 1.
Adam Rosenfield
I should point out that that is the C++98 draft standard, not C++03. See also Michael Burr's answer.
Adam Rosenfield
A: 

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.

Adam Rosenfield
+1  A: 

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:

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.

Michael Burr
I can't seem to find it in the C++98 draft standard, but see my comment on Neil's post for relevant sections.
Adam Rosenfield
The explicit statement isn't in the 98 standard. Without the explicit statement, a perverse vector implementation could have the elements non-contiguous or reversed in memory and still be compliant.
Michael Burr
A: 

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?

partouf
The declaration of CFE() with array parameters is equivalent to what you propose (and casting still doesn't have to come into play - array indexing is defined in terms of pointer arithmetic). However, I still think this is a good idea because it declares CFE() to what is actually happening anyway.
Michael Burr