-1

I have a piece of code that writes a string to memory, and I'm trying to modify it to write an array of booleans instead.

The idea is simply to write random boolean data to memory for every iteration.

Here's the method example that writes strings:

void MemoryWriter::write(const string& data) {
    auto buffer = MapViewOfFile(m_shmHandler, FILE_MAP_ALL_ACCESS, 0, 0, m_memorySize);
    CopyMemory(buffer, data.c_str(), data.size());
}

Here's the non-working example that tries to write booleans:

void MemoryWriter::write(const bool * data) {
    auto buffer = MapViewOfFile(m_shmHandler, FILE_MAP_ALL_ACCESS, 0, 0, m_memorySize);
    CopyMemory(buffer, data, data.size());
}

And also, here the code that generates the random booleans. I know this works, because of the logging, and the result of this function is fed into the previous function.

bool* MemoryWriter::createRandomArduinoPinData() const {
    bool data[12];
    for (size_t i = 0; i < m_memorySize; i++) {
        data[i] = getRandomBool();
        std::cout << "C++: RandomArduinoPinData " << data[i] << std::endl;
    }
    return data;
}

Here's the main function:

#include "stdafx.h"
#include "MemoryWriter.h"
#include <iostream>
#include <string>
#include <thread>


int main()
{
    std::wstring memoryName{ L"shm_1" };
    size_t memorySize{ 12 * sizeof(bool) };
    MemoryWriter writer(memoryName, memorySize);

    while (true) {
        bool* data;
        //data = writer.createRandomData();
        data = writer.createRandomArduinoPinData(); // <-- this generates the random bool array and is confirmed to work
        writer.write(data); <-- for some reason, this part doesn't receive the same array
        std::cout << "C++: Written in shm - " << data[0] << data[1] << data[2] << std::endl;
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    return 0;
}

And here is the console output showing the random data being generated, and the different data coming out:

C++: RandomArduinoPinData 1
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 1
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 1
C++: RandomArduinoPinData 1
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 0
C++: Written in shm - 00AFFCE0
C++: RandomArduinoPinData 1
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 1
C++: RandomArduinoPinData 1
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 1
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 1
C++: Written in shm - 00AFFCE0
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 1
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 1
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 1
C++: RandomArduinoPinData 1
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 0
C++: RandomArduinoPinData 1
C++: Written in shm - 00AFFCE0

As you can see, the random data is being generated, but the result is always the same: 00AFFCE0. Despite not printing an array, I'd still expect it to be different everytime since the underlying data should be different

I think this may be an issue with CopyMemory but I'm not sure. Pointers and memory is still something I'm trying to learn, and I'm having a hard time figuring out this issue.

Any ideas?

Gordon13
  • 464
  • 5
  • 21
  • 2
    `createRandomArduinoPinData` returns pointer to a temporary. That won't ever work. Consider changing the function signature to `createRandomData(std::vector& data)` or `std::vector createRandomData()`; – cplusplusrat Aug 20 '18 at 00:18
  • 1
    Also in the adapted version of 'MemoryWriter' you use 'data.size()' when calling 'CopyMemory' but data is a 'bool*' which doesn't have membe functions. You should change the 'createRandomArduinoPinData()' to return as 'std::vector' and not a pointer or a reference to a local variable so it makes a copy. – Petok Lorand Aug 20 '18 at 05:18
  • Thank you guys. You pointed me (no pun intended) in the right direction about the pointers. I realise now that the pointer was empty because it was pointing to an address inside the function which was being wiped as soon as the function completed. So the external code would always see the same empty variable. I now understand pointers a lot better. I see why passing the address to the temporary variable, instead of a pointer, would work, and the current way doesn't. Anyway, I chose the std::vector method because I prefer returning the value than working with references. – Gordon13 Aug 20 '18 at 11:59

2 Answers2

1

The issue was related to the use of a pointers. The createRandomArduinoPinData function was returning a pointer to an internal variable which got wiped at the end of the function, which meant the return value of that function was always a pointer to empty memory address. The code now uses std::vector instead.

Updated createRandomPinData function:

std::vector<bool> MemoryWriter::createRandomArduinoPinData() const {
    std::vector<bool> data(12);
    for (size_t i = 0; i < m_memorySize; i++) {
        data[i] = getRandomBool();
        std::cout << "C++: RandomArduinoPinData " << data[i] << std::endl;
    }
    return data;
}

Updated write function:

void MemoryWriter::write(const std::vector<bool>& data) {
    auto buffer = MapViewOfFile(m_shmHandler, FILE_MAP_ALL_ACCESS, 0, 0, m_memorySize);
    CopyMemory(buffer, &data, data.size());
}
Gordon13
  • 464
  • 5
  • 21
0

Also createRandomArduinoPinData function cannot return an array this way (you can use std::vector or std::list), however your problem is because of not seeding random function.

random function returns the same value

Ali Asadpoor
  • 327
  • 1
  • 13
  • Why is it wrong to return an array like this? I saw it described in this post: [Return array in a function](https://stackoverflow.com/questions/3473438/return-array-in-a-function). Also, if you look at the console output in my post, you'll see the data is correctly randomised, so I don't think its an issue with the random function. – Gordon13 Aug 20 '18 at 11:30
  • 1
    Thanks for pointing me in the right direction about the array. I understand why the example I gave in the link works and mine doesn't. Basically my array in created internally to the function, whereas in the other example, the array is passed into it, so the variable lives outside the function which is why it works. I changed it to use std::vector instead and it works. – Gordon13 Aug 20 '18 at 12:04