Stack around variable 'Stars' corrupted.

I'm new to handling this type of error but from what I understand it seems to usually be a problem with the scope of a variable. For example writing beyond the length of an array. But I'm having difficulty finding where exactly the problem lies in this code.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#define NUM_BG_STARS 20

void InitBGStars(BGStar Stars[], int NumStars)
{
	BGStar* pCurrentStar = Stars;
	for(int StarIndex = 0; StarIndex < NumStars; ++StarIndex)
	{
		pCurrentStar->Position[0] = ((float)rand() / (float)RAND_MAX);
		pCurrentStar->Position[1] = ((float)rand() / (float)RAND_MAX);

		pCurrentStar->Size = 0.001f + (0.001f * ((float)rand() / (float)RAND_MAX));

		++pCurrentStar;
	}
}

void UpdateBGStars(BGStar Stars[], int NumStars, float DeltaTime)
{
	BGStar* pCurrentStar = Stars;
	for(int StarIndex = 0; StarIndex <= NumStars; ++StarIndex)
	{
		UpdateBGStar(pCurrentStar, DeltaTime);

		++pCurrentStar;
	}
}

void DrawBGStars(BGStar Stars[], int NumStars)
{
	// Draw the stars
	glBegin(GL_QUADS);

	BGStar* pCurrentStar = Stars;
	for(int StarIndex = 0; StarIndex < NumStars; ++StarIndex)
	{
		glColor3f(1.0f, 1.0f, 1.0f);
		glVertex3f(pCurrentStar->Position[0] - pCurrentStar->Size, pCurrentStar->Position[1] - pCurrentStar->Size, 0.0f);
		glColor3f(1.0f, 1.0f, 1.0f);
		glVertex3f(pCurrentStar->Position[0] + pCurrentStar->Size, pCurrentStar->Position[1] - pCurrentStar->Size, 0.0f);
		glColor3f(1.0f, 1.0f, 1.0f);
		glVertex3f(pCurrentStar->Position[0] + pCurrentStar->Size, pCurrentStar->Position[1] + pCurrentStar->Size, 0.0f);
		glColor3f(1.0f, 1.0f, 1.0f);
		glVertex3f(pCurrentStar->Position[0] - pCurrentStar->Size, pCurrentStar->Position[1] + pCurrentStar->Size, 0.0f);

		++pCurrentStar;
	}

	// Finish drawing
	glEnd();
}
These are the function definitions before the entry point.


1
2
3
// Setup the background stars
	BGStar Stars[NUM_BG_STARS];
	InitBGStars(Stars, NUM_BG_STARS);
This creates and initialises the stars before the main loop.


1
2
		// Update the BG stars
		UpdateBGStars(Stars, NUM_BG_STARS, 1.0f / 60.0f);
1
2
		// Draw the background stars
		DrawBGStars(Stars, NUM_BG_STARS);
These are all the references to stars in the main loop.


1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef BGSTAR_H
#define BGSTAR_H

// Information about a background star
struct BGStar
{
	float Position[2];
	float Size;
};

#define UpdateBGStar(Star, DeltaTime) {Star->Position[0] -= Star->Size * 100.0f * DeltaTime; if(Star->Position[0] < 0) Star->Position[0] = 1;}

#endif 
The definition of BGStar from a header file.


These make up all the references to stars in the entire program.
My initial thought was that under the UpdateBGStars method <= is used instead of < in the loop. But when I changed this I still got the error so I changed it back and I've been looking elsewhere.

Any help you can offer is greatly appreciated and I should note this is for a test where I am expected to seek the answers on the internet for myself. I don't think asking for help here is cheating but I'd appreciate hints if at all possible.
This seems like a good candidate (In UpdateBGStars):
for(int StarIndex = 0; StarIndex <= NumStars; ++StarIndex)
I'm guessing that should've been '<', not '<='.
As I said in my original post, that was my first thought but when I tried that it didn't make any difference. Is it possible that changing that with other changes as well it could make the difference?
Build it with debugging symbols and run it under valgrind or similar, and it will tell you exactly which line causes the problem.
Sorry but I don't know what debugging symbols or valgrind are.
As I said in my original post, that was my first thought but when I tried that it didn't make any difference. Is it possible that changing that with other changes as well it could make the difference?

Woops, missed that. Anyway: yes. Just because it isn't the only problem doesn't mean it's correct. I'm very confused that you decided to change it back, because I don't really see how it could ever be correct (or logical) to go one iteration further there.

Are you sure the array passed as Stars is properly initialized?
Sorry but I don't know what debugging symbols or valgrind are.


Don't be sorry, be curious :) If you want to knock together code more articulate than "Hello world" you'll need to know how to fix the mistakes you make.

It is possibly to do something called "debugging". This is the art and science of examining buggy code and determining what the bugs are.

When a kind of programme called a "debugger" is used, it can interrupt the running programme at any point. The user can then interrogate the paused process to find out values of variables; the idea is that by seeing the values of variables, one can determine what has gone wrong. Because a compiled object does not contain names of variables, we can insert "debugging information" which will then allow the debugger to identify variables by name.

"Valgrind" is a programme commonly used under *nix that watches another programme and when it does something with memory it shouldn't (along with other things it can do), interrupts the programme at that point and tells you about it; again, if it has the debugging symbols, it will print out for you the exact line of code causing the problem. If you had run your code with debugging symbols under valgrind, this would already be solved.

I bet you're using windows, though, so you'd have to use a valgrind alternative.
Last edited on
InitBGStars is the function that initialises the stars and it seems to be correct. At least as far as I can tell. I only changed it back because of the format of the test. Basically I have been given this program to fix. So if the problem isn't with that bit of code I feel inclined to leave it alone.
InitBGStars is the function that initialises the stars and it seems to be correct.


How does it go about allocating the array?
@Moschops I understand the concept of debugging I think. I use VS2008 and use break points to step through the code. Unfortunately, I guess I don't know enough about C++ to know when something is doing something it shouldn't, for example, why this problem is occuring.
@Moschops The function definition is near the top of the code on the first post. I've stepped through it and it seems to go fine. Unless that was a hint that it isn't and I need to pay more attention!
I tried changing the <= to < again just to make sure I wasn't going completely mad. And as it turns out, I was... For some reason it didn't work before and it does now. Thanks all for your help.
Topic archived. No new replies allowed.