-2

In my cpp code I'm iterating over four geotiff images and want to extract their geotiepoints (coordinates) in a pair with the image number as first and the geotiepoints as second. Next, I want to push them into a global vector of type pair for later use. That's my current code:

//Global Variable 
std::vector <std::pair<short, double*>> GeoTPWithNr;

//for each file in directory
void GetGeoTiePoints(TIFF* tif)
{
    unsigned short count;
    double* Geotiepoints;
    std::pair<short, double*>TiepointsWithNumber;
    TiepointsWithNumber.first = filenumber;

    TIFFGetField(tif, TIFFTAG_GEOTIEPOINTS, &count, &Geotiepoints)
    TiepointsWithNumber.second = Geotiepoints;
    GeoTPWithNr.push_back(TiepointsWithNumber); 

    std::cout << GeoTPWithNr[0].second[3] << std::endl;
    std::cout << GeoTPWithNr[0].second[4] << std::endl;
}

To test the code I want to print the tiepoints of the vector at position 0. What I expect are four times the tiepoint values. When I ran the code first time it worked and I got the following result: enter image description here
However, every time I run my code I get different result, eventhough I didn't change anything in my code.
enter image description here

When I print the values of my pair, before I push it into the vector, I always get the correct results. Thus, it seems there is a problem when trying to push the pair into the vector or retrieving the values from the vector.

Where is the problem? I guess it has something to do with how the results are written into the vector. Does anyone have an idea on how to solve this issue?

Thanks and best wishes

DGIS
  • 111
  • 7
  • 3
    You can't store pointers to a local variable after the function containing that variable has finished. It's [undefined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior). – Silvio Mayolo Jun 26 '22 at 16:13
  • After you call `TIFFGetField(tif, TIFFTAG_GEOTIEPOINTS, &count, &Geotiepoints)` you have `count` doubles in `Geotiepoints` array but later you print `GeoTPWithNr[0].second[3]` i.e. directly the 4-th double from the array. Could be that `count` is less than 4 for your first `GeoTPWithNr[0]` entry and you are accessing past the end of `Geotiepoints` array into uninitialized memory? – wqw Jun 26 '22 at 16:18
  • The values of TIFFTAG_GEOTIEPOINTS are always 6 doubles, with the coordinates always being the 4th and 5th value. – DGIS Jun 26 '22 at 16:25
  • I removed all the local variables and declared them as global variables. Unfortunately, the issue remains. – DGIS Jun 26 '22 at 16:35
  • 1
    If they are global then you are still overwriting the values every loop. You need to copy them or read into a new array every time – Goswin von Brederlow Jun 26 '22 at 16:59
  • Yes, I want to extract the Geotiepoints and filenumber for every file into a pair and push it into a vector. The idea is to overwrite the pair everytime and have a vector with all Geotiepoints in the end. I'm not allowed to overwrite my pair? – DGIS Jun 26 '22 at 17:05

1 Answers1

0

If I understood the problem correctly, my code doesn't write the actual values into the declared pair, but only to pointer to the values. When the compiler finishes the function it removes the values, thus at the next iteration of the loop the pointer somewhere else.

since I couldn't find a way to write an actuall array into a pair I replaced the pair by:

std::vector <std::array <double, 7>> GeoTPWithNr;

At position 7 I'm writing the value I wanted to write at the pair.first. Now that I write values into my global variable instead of a pointer, my output is stable and correct.

DGIS
  • 111
  • 7