Accessing public class memory from C++ using C

后端 未结 5 561
旧巷少年郎
旧巷少年郎 2021-01-24 22:27

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

5条回答
  •  粉色の甜心
    2021-01-24 22:45

    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 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 cannot be treated as a C array - it a special case since the elements in vector are not stored as bool.

提交回复
热议问题