|
-
Sep 19th, 2001, 03:20 PM
#1
Thread Starter
Fanatic Member
What is wrong with this code??? (resolved)
[SIZE=24pt]Woooohooo it works!! (resolved)[/SIZE]
This code will not run no matter what!!! I tried everything... its the function thats causing it to crash??
Here;s the code.
Code:
// Temp2.cpp : Defines the entry point for the console application.
//
#include <iostream.h>
#include <math.h>
#include <stdio.h>
struct nTemp
{
unsigned char Temp[18][256][256];
};
void saveIt(void);
nTemp loadIt(void);
int main(int argc, char* argv[])
{
saveIt();
// thats all.
cout << "Saved!" << endl;
cin.ignore();
return 0;
}
void saveIt(void)
{
long temp = 0;
long effect = 0;
long src = 0;
long dest = 0;
nTemp myTemp;
FILE* f = 0;
cout << "Saving....\n";
for( effect = 0; effect < 18; effect++ )
{
cout << "Effect: \t\t" << effect << endl;
for( dest = 0; dest < 256; dest++ )
{
for( src = 0; src < 256; src++ )
{
switch( effect )
{
case 0:
// Add
temp = (temp = src + dest) > 255 ? 255 : temp;
break;
case 1:
// Subtract
temp = (temp = dest - src) < 0 ? 0 : temp;
break;
case 2:
// Subtract (corrected)
temp = (temp = dest - (255 - src)) < 0 ? 0 : temp;
break;
case 3:
// Better Add Effect (Alpha Map to White)
temp = (((255 - src) * (dest + 1)) >> 16) - src;
break;
case 4:
// Better Subtract Effect (Alpha Map to Black)
temp = ((src * (dest + 256)) >> 16) - src;
break;
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
case 12:
// Translucency (5 to 12)
temp = (src > 0) ? ((effect - 3) * ((dest + 11) - src)) / 11 + src - (effect - 3) : dest;
break;
case 13:
// Weird Effects (13 - 17)
temp = (src > 0) ? ((temp = (dest * atan(dest / src))) > 255 ? 255 : ((temp < 0) ? 0 : temp)) : dest;
break;
case 14:
// Weird Effects (13 - 17)
temp = (src > 0) ? ((temp = (dest * sqrt(dest / src))) > 255 ? 255 : ((temp < 0) ? 0 : temp)) : dest;
break;
case 15:
// Weird Effects (13 - 17)
temp = (src > 0) ? ((temp = (src * sin(dest / src))) > 255 ? 255 : ((temp < 0) ? 0 : temp)) : dest;
break;
case 16:
// Weird Effects (13 - 17)
temp = (src > 0) ? ((temp = (src * cos(dest / src))) > 255 ? 255 : ((temp < 0) ? 0 : temp)) : dest;
break;
case 17:
// Weird Effects (13 - 17)
temp = (src > 0) ? ((temp = (sqrt(dest) * sqrt(src))) > 255 ? 255 : ((temp < 0) ? 0 : temp)) : dest;
break;
//default:
// if no effect selected, do nothing.
// return;
}
myTemp.Temp[effect][dest][src] = temp;
}
}
}
// now write it to a file.
f = fopen("spfx.dat", "w+b");
fwrite(&myTemp, 1, sizeof(myTemp), f);
fclose(f);
}
nTemp loadIt(void)
{
FILE* f = NULL;
nTemp s;
cout << "Loading...\n";
f = fopen("spfx.dat", "r+b");
fread(&s, 1, sizeof(s), f);
fclose(f);
cout << "Loaded!" << endl;
cout << "Temp[3][6][9] = " << s.Temp[3][6][9] << endl;
return s;
}
Sorry, it doesnt wrap...
Last edited by MoMad; Sep 22nd, 2001 at 12:21 AM.
-
Sep 20th, 2001, 09:53 AM
#2
what's the problem?
Doesn't it compile? Does it crash? Does it do something wrong?
What's the error message (if any)? Which effect is causing it to crash?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 20th, 2001, 02:47 PM
#3
Run it by pressing f10, and step through the code one line at a time. When you get to the saveIt function, hit f11 to jump into it, and continue to hit f10 until it crashes. Then let us know where.
Z.
PS: glad to see that C file I/O =).
-
Sep 20th, 2001, 06:33 PM
#4
Thread Starter
Fanatic Member
Ahh, sorry i was in a bit hurry i must;'ve forgotten to mention that it compiles without a problem, but when you run it, as soon as you enter the saveIt() function you get illegal operation error and when u debug that, you get stack overflow.
Why?? I even tried all sorts of things but I think it might have to do with the use of the turnery operator, but hey, thats supposed to be like a big if/else statement right??
-
Sep 21st, 2001, 10:55 PM
#5
Thread Starter
Fanatic Member
Hey, Zei plz help me with this... it keeps crashing for absolutely no reason. I dont see anything wrong...
take the code and compile it and see if it does the same on ur machine.
-
Sep 21st, 2001, 11:21 PM
#6
As I am sure you know that the reason your program is crashing is because of a Stack Overflow... I am not exactly sure how big it is now-a-days but I guess that doesnt really matter at the moment...
Your structure nTemp is 1179648 bytes... I believe that when variables are created they are put on the stack; therefore, with one declaration of nTemp you are using up almost 1MB of the stack... you also have to remember that function addresses are put on the stack... along with any variables passed in to these functions... or out for that matter...
My advice... instead of declaring a variable of nTemp...
declare a pointer to nTemp and use new or malloc() to allocate the structure...
Code:
nTemp * myTemp = new nTemp;
This way you are only using 4 bytes of the stack for the pointer... and the 1179648 bytes is allocated on the heap
I tried this and it seemed to work...
If any of this is wrong... please someone correct me... because I would also like to know.
-
Sep 21st, 2001, 11:36 PM
#7
Thread Starter
Fanatic Member
Thanks, but what about sizeof() what would i use for that? ill try recompiling the code to what you said and see if it works.
-
Sep 21st, 2001, 11:37 PM
#8
Thread Starter
Fanatic Member
btw amac, thanks very much for the reply i really appriciate it
-
Sep 22nd, 2001, 12:19 AM
#9
Thread Starter
Fanatic Member
WOOOOHOOOOOOO!!!!!!!!!!!
THANK YOU SO MUCH FOR THE HELP!!!! IT WORKS NOW!!!!
all i did was to use new instead of declaring a huge piece of memory on the stack. Thanks.
-
Sep 22nd, 2001, 04:46 PM
#10
but what about sizeof() what would i use for that?
I dont think I understand your question.
-
Sep 22nd, 2001, 06:53 PM
#11
Thread Starter
Fanatic Member
Originally posted by amac
I dont think I understand your question.
Nevermind, it works greatly now, thats all that matters right? Lol. It works like a charm, except now i have a question about the fread function.
How do you load only one object (myTemp->temp[i] - should return a * [256][256] array) from the file instead of the whole nTemp 18*256*256?
Confusing... sorry... i changed the object to:
PHP Code:
struct nTemp
{
unsigned char Temp[256][256];
};
nTemp ** myTemp = new *nTemp;
for(int i = 0; i < 18; i++) nTemp[i] = new nTemp;
and now i only want to load one of those nTemp objects from the file, ie. 7. SO how would i load myTemp[7] from the file using fread?
-
Sep 23rd, 2001, 04:25 AM
#12
Not sure if you already solved that too (or only the stack overflow)
I'm sure you wanted to write myTemp[i] in the for loop (not nTemp[i]).
to read any nTemp, let's say 5:
Code:
fseek(f, 4*sizeof(nTemp), SEEK_SET); // if data starts immediatly after file start
fread(myTemp[4], 1, sizeof(nTemp), f); // 5th entry -> [4]
Not tested, but this should work.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Sep 23rd, 2001, 05:37 PM
#13
Thread Starter
Fanatic Member
Great thanks, thats exactly what i wanted.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|