0

I have a cpp function via an interface float* decode() which returns pointers to raw data of a static vector.

Now in my c code I have a

float *value0;
value0 = (float*) malloc(M* sizeof(float) );

now when I do

value0 = work_decode1(h0, code0, 7, retain0);

I can only see the first value from the vector in value0 when I hover over it in visual studio debug mode. What is wrong here?

1
  • There is nothing wrong.When you return a array from a function you are actually returning a pointer to its first element and not the array itself. Just use the subscripts and you can access the successive elements because array elements are contiguous in memory. Commented Aug 25, 2012 at 7:22

2 Answers 2

1

float * is a pointer, not an array. The debugger doesn't have any idea how many elements it points to so it just shows you the first one.

In the watch window, you can specify the number of elements with this syntax:

value0,20

Where 20 is the number of elements you want to be visible.

Sign up to request clarification or add additional context in comments.

Comments

0

I assume the function decode() comes from a library and returns a pointer to a statically allocated memory buffer. Your first job should be to copy the buffer, since the contents will change the next time the function gets called. You will need to know the length of the buffer, M.

I suggest you copy it to a vector:

std::vector<float> buffer(value0, value0+M);

If I understand your code correctly, you actually have a memory leak. You allocate memory for your buffer, but then you replace the pointer to it with the one returned from the decode function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.