I have implemented a container and running some tests on it. While on this I noticed this behaviour:
If I compile this (here cit
is one of my containers):
//...
int i = 0;
while(i != 10)
{
std::cout << *cit << " ";
++cit; ++i;
}
//...
Then I print the data I expect:
me@LAPTOP-Q8SCO8V5:~/proj/proj_dev$ g++ main.cpp
me@LAPTOP-Q8SCO8V5:~/proj/proj_dev$ ./a.out
5 6 2 4 4 1 2 3 2 7
But then, if the condition is changed to this loop:
//...
int i = 0;
do {
std::cout << *cit << " ";
++cit; ++i;
} while(cit != mit.end_row(0));
//...
I get a segfault:
me@LAPTOP-Q8SCO8V5:~/proj/proj_dev$ g++ main.cpp
me@LAPTOP-Q8SCO8V5:~/proj/proj_dev$ ./a.out
Segmentation fault (core dumped)
I know the loop might break (and probably is) because of some mistake I made implementing cit
or mit.end_row
. But in this example, I would expect at least to print that first element correctly before segfaulting. For the first snippet, I believe the container is initialized as I expect.
My question is if this behaviour I see is generated by the compiler, optimizing the code and finding it would eventually segfault. Or if something else I am missing.
If any implementation code for cit
is needed, I will gladly provide. I didn't do so in my original post as I am not sure the question requires it being posted.