Home > Software design >  How to use pointer array for segmented data
How to use pointer array for segmented data

Time:11-07

So I am doing some graphics rendering and have gotten to a point where the data being used is too high and run/loading time takes way too long. Completely my fault as I am copying a massive chucks (2 gig) of data all over the place. Naturally I need to transition to pointers and here is the problem I face.

We have main data "vector data" and I need to access random areas (xyz points) in it.

vector<float> data{1, 2, 3, ... , 101, 102, 103, ...};
float* point1 = &data[0]            //points to beginning of array (1,2,3,...)
float* point2 = &data[100]          //points to middle of array  (101, 102, 103,...)

Now I need to make an output array that uses both pointers, but I'm not sure how to do this. In essence I want the following.

float* outputList = point1;
outputList 3 = point2;

Such that output List = {1,2,3,101,102,103}; This wont work because I am trying to reassign the actual pointer address in the second line. The second major issue is that output list would go on after 103, and keep going till the end of the data vector. I know there are a few issues with this, but hopefully I got the idea across. Thank you for any advice.

CodePudding user response:

Well, pointers point to physical address cells. When you assign outputList = point1; that means outputList will now point to the same address cell as point1. Adding a value to a pointer will move it certain amount of physical cells. You cannot state that outputList 3 = point2, because outputList already points to physical cell point1, and moving it 3 cells will point to physical cell 3.

If you would like to have another array with values from both point1 and point2 you would have to allocate new memory and assign the right values to it. You can do that by creating another vector.

However, if you only want to use the values from point1 and point2 you could just iterate over interesting parts of original array and temporarily use the original values (without creating a new array).

CodePudding user response:

This should be a comment, but I don't have enough reputation.

Take a look at std::span.

Maybe you could use a std::vector<std:span<...>>?

std::span is basically a pointer and a size.

Note that if you do something like this you lose cache locality if the data is too far apart.

  • Related