Data segment

Hi everyone,
so I have to create a Queue with elements and everything and I seperated part of the code so that I can concentrate on the FIFO(First in first out princip) and check if my code works. So I am deliberately printing out more values then my array actually has so that I can check if the range is correct. Anyways when I compile it with g++ after the 10th value(the last one of the array) instead of getting all random numbers (usually in the millions) I get this weird pattern, for example starts printing out the 'int i' from the loop. So I guess when I print more then the array range it starts printing out the next bytes of the data segment where coincidentally the other variables of my program are stored like 'N' and 'i' etc. So my question is if my understanding above is correct and if yes is there anyway to manipulate the part of the segment where the variables are stored so that they're not close to each other.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cmath>
using namespace std;

const int N = 10;

int main(){
	int obj[N];
	int head = 0;
	int n_obj = 0;
	for(int i = 0; i < 15; i++){
		obj[(head + n_obj)%N] = i;
		if (n_obj < N) {
			n_obj++;
		}
		else {
			head = (head + 1) % N;
		}
		for (int i = 0; i < 20; i++){

		cout << obj[i] << ", ";
		}
	cout << endl;
	}
	
	return 0;
}


P.s. I'll post just the last lines of output.


2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 10, 11, 10, 0, 4201008, 6422320, 6422352, 4198966, 6422284, 3383296,
12, 3, 4, 5, 6, 7, 8, 9, 10, 11, 10, 12, 10, 1, 4201008, 6422320, 6422352, 4198966, 6422284, 3383296,
12, 13, 4, 5, 6, 7, 8, 9, 10, 11, 10, 13, 10, 2, 4201008, 6422320, 6422352, 4198966, 6422284, 3383296,
12, 13, 14, 5, 6, 7, 8, 9, 10, 11, 10, 14, 10, 3, 4201008, 6422320, 6422352, 4198966, 6422284, 3383296,
12, 13, 14, 15, 6, 7, 8, 9, 10, 11, 10, 15, 10, 4, 4201008, 6422320, 6422352, 4198966, 6422284, 3383296,
12, 13, 14, 15, 16, 7, 8, 9, 10, 11, 10, 16, 10, 5, 4201008, 6422320, 6422352, 4198966, 6422284, 3383296,

yes, typically going out of bounds hits nearby variables and corrupts them if you change the values. It absolutely goes to the next byte(s); the data is in a solid block starting at [0], but what is in that next block could be any of many things... another variable, debugging info, function call / stack frames, unusued/random, unused/remnants, and more. Sometimes you can use this to do minor fun hacks by going to such locations, but mostly, its unreliable and useless to play with it. I did a fractal once that colored the segments using a corrupt offset like this, and the recursive calls changed the colors in a predictable way that was quite interesting :)
Last edited on
That does sound interesting, anyway I can have a look at it myself? :D
That code vanished with my box of floppies representing my 386's backed up hard disk. But it would not be hard to re-create... grab a simple fractal and set the RGB value for each iteration to be from invalid stack frame data and fire it up.

I would have to install some graphics to get it working on my end again.. all my C++ code these days is console programs.
Last edited on
Well there's 2 words I didn't think I'd hear again haha. No worries, that's actually a fun little challenge for myself. Thx for the replies, have a nice one :D
Topic archived. No new replies allowed.