Serialize dynamic memory

Hello.
I don't know how to serialize (in binary mode) a struct with dynamic memory by the method ofstream/ifstream, I was searching in the net, but I can't find it.
How could I?
P. S.: Sorry for my bad English.
I'm not quite sure what you are looking for, but if I understand you correctly, you may want to look at this: http://www.cplusplus.com/reference/clibrary/cstdlib/realloc/
If you want to view data one bit at a time, you can just use a data mask, or a logical AND. That will 'select' the bits that you want to view. Then if the result is zero, the 'selected' bit(s) are off. Otherwise, they are on. If my memory serves me right, the operator for bitwise and is &
1
2
3
4
5
int a;
if (a & 128 != 0) //Check status of bit 7
  cout<<1;//Bit 7 is high
else
  cout<<0;//Bit 7 is low 
Last edited on
If you want to persist a structure inside a stream, one normally writes a method for that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
struct MyStruct
{
    int MyInt;
    double MyDouble;

    void Serialize(std::ostream &out) const
    {
        out << MyInt << MyDouble;
    }
    static MyStruct Deserialize(std::istream &in)
    {
        MyStruct s;
        in >> s.MyInt >> s.MyDouble;
        return s;
    }
}

//And operator overloads to complement:
std::ostream& operator<<(std::ostream &out, MyStruct const &s)
{
    s.Serialize(out);
    return out;
}

std::istream& operator>>(std::istream &in, MyStruct &s)
{
    s = MyStruct::Deserialize(in);
    return in;
}


Was that it? Or do you have a different concept in mind?
Yes, it was. But I want to save dynamic memory:
1
2
int* dynamic_int;
dynamic_int = new int[5];


No static memory:
 
int dynamic_int[5];


Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//main.cpp
#include <fstream>
using namespace std;

struct
{
    int* dynamic_int;
    double* dynamic_double;
} mystruct;

int main()
{
    
    mystruct.dynamic_int = new int[5];
    mystruct.dynamic_double = new double[5]
    
    ofstream file("FILE", ios::binary);
    
    // I think that it could be something like this...
    file.write((char*)&mystruct, sizeof(/*???*/));
    
    // Or...
    file.write((char*)&*mystruct.dynamic_int, sizeof(/*???*/);
    file.write((char*)&*mystruct.dynamic_double, sizeof(/*???*/))
    
    return 0;
}
Well, you must know the size of the arrays, so you should accompany the pointers with size_t variables that save the array item count. After you do that, you serialize by writing (<<) the array item count first, then each individual item. When you deserialize, you read (>>) the array item count, then allocate the memory, then read each individual item.
I tried this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
using namespace std;

struct MyStruct
{
    int* MyInt;
    size_t MyIntSize;
};

int main()
{
    MyStruct mystruct;

    //switch
    if (1)
    {
        mystruct.MyInt = new int[(mystruct.MyIntSize = 5)];
        mystruct.MyInt[4] = 15;

        ofstream file("FILE");
        file << mystruct.MyIntSize;

        for (unsigned i = 0; i < mystruct.MyIntSize; i++)
            file << mystruct.MyInt[i];
    }
    else
    {
        ifstream file("FILE");
        file >> mystruct.MyIntSize;

        for (unsigned i = 0; i < mystruct.MyIntSize; i++)
            file >> mystruct.MyInt[i];
    }

    cout << mystruct.MyInt[4];

    return 0;
}


The file says "5000015" so I think that it's fine but when I load it, it fails. What isn't OK?
The stream needs to be a binary stream, otherwise the value is transformed into a textual representation of the number.

ofstream file("FILE", std::ios_base::binary);
If I try with "<<" or ">>", it fails.
If I try with "write" or "read", it fails (segmentation fault).

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
using namespace std;

struct MyStruct
{
    int* MyInt;
    size_t MyIntSize;
};

int main()
{
    MyStruct mystruct;

    //switch
    if (0)
    {
        mystruct.MyInt = new int[(mystruct.MyIntSize = 5)];
        mystruct.MyInt[4] = 15;

        ofstream file("FILE", ios_base::binary);
        file.write((char*)&mystruct.MyIntSize, sizeof(size_t));

        for (size_t i = 0; i < mystruct.MyIntSize; i++)
            file.write((char*)&mystruct.MyInt[i], sizeof(int));
    }
    else
    {
        ifstream file("FILE", ios_base::binary);
        file.read((char*)mystruct.MyIntSize, sizeof(size_t));

        for (size_t i = 0; i < mystruct.MyIntSize; i++)
            file.read((char*)&mystruct.MyInt[i], sizeof(int));
    }

    cout << mystruct.MyInt[4]; //fails when I try to read

    return 0;
}


Sorry if I don't understand, but it does not work.
Last edited on
Simple, you never initialize the MyInt array when reading from the file. Didn't you notice? You must initialize myStruct.MyInt with a size of myStruct.MyIntSize.
Solved. Here is the final code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

#include <iostream>
#include <fstream>
using namespace std;

struct MyStruct
{
    int* MyInt;
    size_t MyIntSize;
};

int main()
{
    MyStruct mystruct;

    //switch
    if (0)
    {
        mystruct.MyInt = new int[(mystruct.MyIntSize = 100000)];
        mystruct.MyInt[mystruct.MyIntSize - 1] = 15;

        ofstream file("FILE", ios_base::binary);
        file.write((char*)&mystruct.MyIntSize, sizeof(size_t));

        for (size_t i = 0; i < mystruct.MyIntSize; i++)
            file.write((char*)&mystruct.MyInt[i], sizeof(int));
    }
    else
    {
        ifstream file("FILE", ios_base::binary);
        file.read((char*)&mystruct.MyIntSize, sizeof(size_t));

        mystruct.MyInt = new int[mystruct.MyIntSize];

        for (size_t i = 0; i < mystruct.MyIntSize; i++)
            file.read((char*)&mystruct.MyInt[i], sizeof(int));
    }

    cout << mystruct.MyInt[mystruct.MyIntSize - 1];

    return 0;
}


Thank you (no, I didn't notice...).
Topic archived. No new replies allowed.