0

I am working with boost and an a two dimensional array of floats expressed like a double pointer (float**). By the moment I am allocating memory two load the value (when deserializing), I don't know and I did not find any information on how to do it.

            int i;
            array    = (float**) malloc(N * sizeof(float*));
            assert(array == NULL);
            for (i=0; i<N; i++) {
                    array[i] = (float*)  malloc(N * sizeof(float));
                    assert(array[i] == NULL);
            }

I tried a few things like serializing one by one the arrays inside but is not working. Also you know some documentation about this? I found a few examples of serialization but they are very simple.

Thank you.

EDIT

I changed the mallocs by constructors of float type. The case is that now is not possible even to compile.

        if (Archive::is_loading::value)
    {

            assert(array == NULL);
            array = new float*[N];

            int i;
            for (i=0; i<N; i++) {

                    assert(array[i] == NULL);
                    array[i] = new float[M];
            }
    }

    ar & array;

This is giving me the following error:

error: request for member ‘serialize’ in ‘t’, which is of non-class type ‘float*’
       t.serialize(ar, file_version);

It's not possible to serialize float* or I am missing something?

Marc43
  • 491
  • 2
  • 6
  • 15
  • Yet another reason why you should use proper C++ containers rather than raw C-style data structures. – Paul R Nov 19 '18 at 09:25
  • Well, the case is that this application has been brought to me and I have to do this part of the serialization. I can spend my time changing to the C++ containers (which would be nice), but also I would like to know how to do it with these raw C data structures :) – Marc43 Nov 19 '18 at 09:26
  • "*I tried a few things*" - please include that code in the question, and what exactly didn't work. – rustyx Nov 19 '18 at 09:37
  • You shouldn't be using a pointer of a pointer to represent your matrix. That's plain wrong. Not only you'll face problems like the one you're asking about, but also your matrix performance will be awful because you'll likely have tons of cache misses when doing any calculations. Make your matrix a one dimensional array in a proper class, and create accessors for the elements that calculate the position in the array. Or better, use a [valid library to do that for you](http://arma.sourceforge.net/). – The Quantum Physicist Nov 19 '18 at 09:38
  • You don't have a C++ program there. Casting the result of `malloc` doesn't construct any objects, so you have undefined behaviour – Caleth Nov 19 '18 at 10:20
  • Well, just for having an answer, I don't expect any performance right now; Maybe as I can serialize it I will change the data structures. I am allocating memory and then I put there data, if I can't serialize it makes no sense. – Marc43 Nov 20 '18 at 08:48

0 Answers0