Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested loops and arrays- confused

Status
Not open for further replies.

Cindy290

Programmer
Jan 17, 2005
2
0
0
US
Hi all. I'm new to c++, and am trying to write a stream of interleaved 3 byte (1 byte per color RGB), pixel data to a binary file. The fwrite fails when 3 lines of commented code below are enabled. I don't get an error, the program appears to run correctly, but no file is created. Any thoughts? I thought it might be the nested loop, so I tried 'for' loops instead, but got the same problem. Please don't pick on me too hard if my programming is lousy, I'm really new to all of this.

Cindy





#include "stdafx.h"
#include <stdio.h>
#include "iostream.h"

int main(int argc, char* argv[])
{
int red[256]; //Red Level
int green[256]; //Green Level
int blue[256];// Blue Level
char buffer [768]; //stream to write to file
int a=0; //array positioning device for interleaving
int g=0;

int x=0;
while (x<256)
{
int y=0;
while (y<256)
{
float interval= (255-x)/255;

if(y>0) {g=g+interval;} else {g=x;};

red[y]=255;
green[y]=g;
blue[y]=y;

int lBR=a;int lBG=a+1;int lBB=a+2;

/* The following 3 lines of code causes no file to be created by the fwrite function called below. If I reference constant, numeric values for the array element (instead of lBR etc., then it works and a file is generated by fwrite. */

buffer[lBR]=red[y];
buffer[lBG]=green[y];
buffer[lBB]=blue[y];


a=a+3;
y++;
}

x++;
}


FILE *fp;
fp = fopen("c:\\test.raw", "wb");
if(fp != NULL)
{
fwrite( buffer, 1, 768, fp);
fclose(fp);
}
return (0);
}
 
Try printing the value of a each time around the loop, and notice how much bigger than 768 it gets.

Please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

--
 
Salem, thank you very much for the help. I looked at my numbers and the iterations of the nested loops, and I certainly far off.

Although I simply created too small an array, after some research online (after following up Salem's suggestion), I found a parallel concept to this thread for others to consider when creating arrays regarding stack overflow:



The stack is a region of memory on which local automatic variables are created and function arguments are passed. The implementation allocates a default stack size per process. On modern operating systems, a typical stack has at least 1 megabyte, which is sufficient for most purposes. Under anomalous conditions, the program exceeds its stack limit. This causes a stack overflow. The two most common causes for a stack overflow is an infinite recursion, as in:


int f(){
g();
}
int g() {
f();
}

f() calls g(), which in turn calls f() and so on. Eventually, the stack overflows. Another common cause for stack overflow is an attempt to create a large array on the stack, for example:



int main()
{
int n[10000000]; // array is too large
int j =0; //j's address exceeds the stack's limits, error
}

If your program crashes due to a stack overflow, check for infinite recursion or too large local objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top