jeudi 21 avril 2022

c++17 memory access of memory owned by c++11 library

I have a program compiled with -std=c++17 and it links against a library that uses -std=c++11. There is a class with a buffer (char *) inside the c++11 that I want to use. The c++11 library made the buffer public for whatever reason. When I look at this buffer from my class in the c++17 program, the buffer is pointing 4 bytes too far. So I am missing 4 bytes. If I step into the class from the c++11 library and I look at the buffer it looks fine. I want to know if this is a known thing between c++17 as i don't normally mix and match c++ versions.

Here is what I see in gdb when stepping through the code below.

(gdb) p &bufClass.buf
$11 = (unsigned char (*)[4194304]) 0x7fffffbf9efc
(then ill step into the print())
(gdb) p &buf
$12 = (unsigned char (*)[4194304]) 0x7fffffbf9ef8

Here is a rough idea of what it looks like:

#include "theirlib.h"
class MyClass
{
public:
void inspectbuff()
{
std::cout.write(bufClass.buf, 8); //"OYOU\0\0\0\0"
bufClass.print(std::cout); //this is a function implemeted in that lib and buf-> "HELLOYOU"
}

private:
theirlib::theirclass bufClass;
};

Aucun commentaire:

Enregistrer un commentaire