Returning pointers from Juce's Array


#1

Hello all.

Using the Juce Array, I have an Array of an Array of doubles.

If I call .getFirst() on the first Array of doubles I get the correct result, the first value of the array:

floatTester = midiTrackNotesArray[0].getFirst();

However, I need the pointer to return a pointer to the Array of Arrays from a function, so I use begin to return a pointer to the first Array:

tempArrayPointer = midiTrackNotesArray[0].begin();

Surely, that pointer points to the value which getFirst() returns correctly. However, when I dereference the pointer:

floatTester = *tempArrayPointer;

I get a nonsense return: e.g. -1.45682e+144.

I have tried plenty of variations but to no avail. Apologies if this is a C++ question, as I'm learning both C++ and library together it's not always clear which side of the fence the questions falls, but as I was using the Juce Array object it seemed to make sense.

Thank you.

 


#2

Most likely you're using the pointer after the original Array has been deleted.


#3

Hello Jules, thank you for your quick reply.

The code follows straight on from each other, so I can't imagine it would go out of scope:

        midiTrackNotesArray.insert(i, tempNoteArray);        
        tempArrayPointer = midiTrackNotesArray[0].begin();        
        floatTester = *tempArrayPointer;
 

 


#4

It is going out of scope - the operator[] returns a temporary copy of the object you ask for. You probably want to use getReference(0) instead.


#5

That works perfectly, so now I have the direct reference, I can create a pointer to that and return that from the function.

Thank you very much, many things (well, every step at the moment!) comes slow and I work most of them out, sometimes things just don't mentally unstick!