将共享内存地址值打印到命令行

时间:2021-10-05 23:08:30

I'm attempting to use boost's shared memory library to do some inter-process communication (VS 2015). I found an example online which is very helpful. For sanity's sake I just want to perform a simple check that the value I wrote to the shared memory address is what I wanted. To do this I want to print the value of the shared memory using cout. This is the code I have currently:

我正在尝试使用boost的共享内存库进行一些进程间通信(VS 2015)。我在网上找到了一个很有帮助的例子。出于理智的考虑,我只想执行一个简单的检查,即我写入共享内存地址的值就是我想要的值。为此,我希望使用cout打印共享内存的值。这是我目前的代码:

#include <boost\interprocess\shared_memory_object.hpp>
#include <boost\interprocess\mapped_region.hpp>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <cstring>
#include <cstdlib>
#include <string>

int main()
{
    using namespace boost::interprocess;

    struct shm_remove
    {
        shm_remove() { shared_memory_object::remove("MySharedMemory"); }
        ~shm_remove() { shared_memory_object::remove("MySharedMemory"); }
    } remover;

    //Create a shared memory object
    shared_memory_object shm(create_only, "MySharedMemory", read_write);

    //Set size to 1
    shm.truncate(1);

    //Map the whole shared memory in this process
    mapped_region region(shm, read_write);

    //Write all the memory to 1
    std::memset(region.get_address(), 1, region.get_size());

    //Check that memory was initialized to 1
    char *mem = static_cast<char*>(region.get_address());

    for (std::size_t i = 0; i < region.get_size(); ++i)
    {
        std::cout << "Memory value: " << *mem << "\n";
        if (*mem++ != 1)
        {
            return 1;   //Error checking memory
        }
    }
    std::cout << "press any key to quit";
    _getch();
}

The code works fine, no errors are thrown when it checks that the mapped memory has been set to 1. However when I try to print what I think should be the value at the address, I get a smiley face...

代码运行良好,当它检查映射内存是否被设置为1时不会抛出错误。然而,当我尝试打印我认为应该是地址的价值时,我得到了一张笑脸……

将共享内存地址值打印到命令行

Can anyone point me in the right direction? I have some suspicions (no terminating \0?) but I really don't understand the inner workings here. Any help is appreciated!

谁能给我指出正确的方向吗?我有一些怀疑(没有终止\0?)但是我真的不了解这里的内部工作。任何帮助都是赞赏!

2 个解决方案

#1


3  

Cast mem to int explicitly, then std::cout is going to output the value as a number, not as a letter corresponding to the ASCII code stored inmem (which may be unprintable or look funny, the latter being your case).

显式地将mem转换为int,然后std::cout将输出值作为数字,而不是作为与存储inmem的ASCII码对应的字母(可能无法打印或看起来很有趣,后者就是您的情况)。

See cout not printing unsigned char for a more thorough answer on the problem.

有关这个问题的更全面的答案,请参见cout不打印无符号字符。

#2


2  

The console shows you the ASCII representation of bytes in memory. Any character under 13 is usually unprintable. Try setting the memory to something like 67.

控制台向您显示内存中字节的ASCII表示形式。13下的任何字符通常是不可打印的。试着将内存设置为67。

#1


3  

Cast mem to int explicitly, then std::cout is going to output the value as a number, not as a letter corresponding to the ASCII code stored inmem (which may be unprintable or look funny, the latter being your case).

显式地将mem转换为int,然后std::cout将输出值作为数字,而不是作为与存储inmem的ASCII码对应的字母(可能无法打印或看起来很有趣,后者就是您的情况)。

See cout not printing unsigned char for a more thorough answer on the problem.

有关这个问题的更全面的答案,请参见cout不打印无符号字符。

#2


2  

The console shows you the ASCII representation of bytes in memory. Any character under 13 is usually unprintable. Try setting the memory to something like 67.

控制台向您显示内存中字节的ASCII表示形式。13下的任何字符通常是不可打印的。试着将内存设置为67。