0

Due to certain constraints I am forced to use a C float array for one of my libraries. The array is malloc'ed following some processing. How would I allocate memory within the serialize() method?

If you move the malloc from init() to the constructor, the code works.

#include <iostream>
#include <fstream>
#include <cstdlib>
#pragma warning(disable: 4244)
#include <boost/serialization/serialization.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>

struct Monkey
{
    int num = 256*70000;
    float* arr;

    Monkey()
        {}
        void init() {
                arr = (float*)malloc(num*sizeof(float));//new float[num];
        }  
    ~Monkey() { free(arr); }
};

namespace boost
{   
    namespace serialization
    {   
        template<class Archive>
        void serialize(Archive & ar, Monkey& m, const unsigned int version)
        {   
            ar & m.num;
            ar & make_array<float>(m.arr, m.num);
        }
    }
}
int main(int argc, char* argv[])
{
    const char* name = "monkey.txt";
    {   
        Monkey m;
        m.init();

        std::ofstream outStream(name, std::ios::out | std::ios::binary | std::ios::trunc);
        boost::archive::binary_oarchive oar(outStream);
        oar << (m);
    }

    Monkey m;
    std::ifstream inStream(name, std::ios::in | std::ios::binary);
    boost::archive::binary_iarchive iar(inStream);
    iar >> (m);

    //std::copy(m.arr, m.arr + m.num, std::ostream_iterator<float>(std::cout, ";"));
    std::cout << m.arr[10] << std::endl;
}

To be honest, I don't really get what's going on under the hood with boost serialization. It's just a black box to me. I'm too much of a beginner to read its source code.

John
  • 3,037
  • 8
  • 36
  • 68
  • You can't just delete your question after you received an answer. That's really poor style. We're not servants for you, personally. You post questions that may help future users (including you). – sehe Sep 09 '15 at 22:16
  • @sehe I know, but I was stupid. You're answer from the other question answers what I was looking for exactly. Is that still poor style? Or would my rephrasing help? – John Sep 09 '15 at 22:19
  • Nah. You either find the dupe before you post, or maaaayybe you delete before someone answers (this is iffy, because few things are so frustrating as answering a question that \*POOF\* vanishes). In this case, just leave it here. I marked it as the dupe after I verified that, indeed, it is. – sehe Sep 09 '15 at 22:20
  • I remember reading the same question a few days ago too. I was like: "What is all this extra stuff about nullptr and is_loading?" After finally sort of understanding the boost serialization library and seeing the answer again, I realize that was a part of what I needed. – John Sep 09 '15 at 22:22
  • 2
    Good for you! You learned :) It's not bad to learn with mistakes. It was just bad form to throw away effort that someone just made on your behalf. – sehe Sep 09 '15 at 22:23

1 Answers1

2

According to this answer, you can do it like this:

template<class Archive>
void serialize(Archive & ar, Monkey& m, const unsigned int version)
{
    ar & m.num;
    if (Archive::is_loading::value)
    {
        assert(m.arr == nullptr);
        m.arr = new float[m.num];
    }
    ar & make_array<float>(m.arr, m.num);
}
Community
  • 1
  • 1
Adam
  • 16,808
  • 7
  • 52
  • 98