Passing std :: string to the library API

We are currently creating an API for a specific library. Part of the interface requires the library to receive and return custom classes, such as a vector and a string.

When you try to simulate using the library in a simple scenario, in debug mode, the system is suppressed when the string is delivered as input.

I believe that there is another representation of the string class in debug or release mode. Then our library expects to get a certain idea, to incorrectly read a data member and crush along the way. So what is the best way to pass STL objects to the API. The target OS is Windows XP compiled with MSVC 8, although the library user will use windows that their compiler could (and probably will) Ideas we have had so far:

  • Change the string to char * - But then developers can confuse the responsibility of freeing memory.
  • Use our own version of String - I don't want to develop another private string implementation.
  • Release the debug version and debug version.
  • Ask people about stack overflows for some option that we missed or don’t understand, or just heard from their experience.
+3
source share
2 answers

You should avoid passing STL objects between different binary modules.

For a string, you must rely on const char*for a read-only parameter and char*, <buffer size>for input parameters ...

For vectors, this can be a little more complicated, especially if you need to change the contents of the vector ...

About your ideas:

  • You are right, but it is generally accepted that you cannot save the passed pointer (you need to make your local copy).
  • In the end, you will have the same problem if you don't have the same binary representation for debugging and releasing a version of your implementation.
  • , / STL ( , STL).
+5

​​ . . , DirectX, . # 3 - /.

+7

Source: https://habr.com/ru/post/1752860/


All Articles