Problems with pointers and arrays...
I never was good ad this stuff... How do I pass a three dimentional self defined boolean to a function so that it may alter it? Here''s what I tried :
typedef enum BOOLEAN {true,false} BOOLEAN;
void clear_space(BOOLEAN *space);
int main()
{
BOOLEAN space[640][480][100];
clear_space(&space);
}
void clear_space(BOOLEAN *space) /*guess * isn''t even necessary there*/
{
int x,y,z;
for(x=0;x<640;x++)
for(y=0;y<480;y++)
for(z=0;z<100;z++)
space[x][y][z]=false;
}
Just realized that I''m a real newbie when it comes to programming, arrays and pointers are one of the most important factors in programming...
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
After some fiddling in VC++
You're first problem in VC++... typedef enum BOOLEAN {true,false} BOOLEAN; the words true and false are defined keywords... So if you're using a compiler that has bool... Use it.
Now to the solution of you're problem. After som fiddling with the parameters. The simple solution follows:
void clear_space(bool space[640][480][100]);
int main()
{
bool space[640][480][100];
clear_space(space);
return 0;
}
void clear_space(bool space[640][480][100])
{
int x,y,z;
for(x=0;x<640;x++)
for(y=0;y<480;y++)
for(z=0;z<100;z++)
space[x][y][z]=false;
}
This compiles fine. The drawback It requires a knowledge how big the 3d array is when you are writing the code.
Hairy technical explanation why the old code won't work:
In C and C++ arrays and pointers are the same thing.
int a[] = {1, 3, 10};
int *p;
p = a;
printf("%d, %d, %d", p[0], p[1], p[2]); // should print 1, 3, 10
Even worse according to the C standard multidimensional arrays don't exist in the compiler.
To exactly find out how these things work I would suggest that you read a beginners book on C or C++.
Another suggestion is to use different data structure to store points. Like a struct or a class. They are simpler to handle. If you aren't building a voxel based thingy.
Good Luck too you!
/JanneVee
"Some People even believe that COBOL is a real programming language." Scott Meyers - Effective C++
Edited by - JanneVee on July 12, 2001 7:19:02 AM
You're first problem in VC++... typedef enum BOOLEAN {true,false} BOOLEAN; the words true and false are defined keywords... So if you're using a compiler that has bool... Use it.
Now to the solution of you're problem. After som fiddling with the parameters. The simple solution follows:
void clear_space(bool space[640][480][100]);
int main()
{
bool space[640][480][100];
clear_space(space);
return 0;
}
void clear_space(bool space[640][480][100])
{
int x,y,z;
for(x=0;x<640;x++)
for(y=0;y<480;y++)
for(z=0;z<100;z++)
space[x][y][z]=false;
}
This compiles fine. The drawback It requires a knowledge how big the 3d array is when you are writing the code.
Hairy technical explanation why the old code won't work:
In C and C++ arrays and pointers are the same thing.
int a[] = {1, 3, 10};
int *p;
p = a;
printf("%d, %d, %d", p[0], p[1], p[2]); // should print 1, 3, 10
Even worse according to the C standard multidimensional arrays don't exist in the compiler.
To exactly find out how these things work I would suggest that you read a beginners book on C or C++.
Another suggestion is to use different data structure to store points. Like a struct or a class. They are simpler to handle. If you aren't building a voxel based thingy.
Good Luck too you!
/JanneVee
"Some People even believe that COBOL is a real programming language." Scott Meyers - Effective C++
Edited by - JanneVee on July 12, 2001 7:19:02 AM
/JanneVee"Some People even believe that COBOL is a real programming language." Scott Meyers - Effective C++
quote:
Original post by JanneVee
Even worse according to the C standard multidimensional arrays don''t exist in the compiler.
/JanneVee
"Some People even believe that COBOL is a real programming language." Scott Meyers - Effective C++
Edited by - JanneVee on July 12, 2001 7:19:02 AM
True, let me explain, if I were to do this is dos:
unsigned char far *video_buffer = ( unsigned char *) 0xA0000000;
then use this variable, which is a direct access to the video memory( so it''s like a 2d array in reality ie: 320x200 ), I would access the buffer like so ie:
video_buffer[ ( y * 320 ) + x ] = 10; which would put a green dot at ( x, y ) coordinates.
Let me try another type of example,
say I have this:
char array[10][10];
char *p = array;
I could access the data like so:
p[1][1] or p[11]; it would be the same thing.
"And that''s the bottom line cause I said so!"
Cyberdrek
Headhunter Soft
A division of DLC Multimedia
Resist Windows XP''s Invasive Production Activation Technology!
"gitty up" -- Kramer
[Cyberdrek | ]
Thanks! I never knew callsing function(variable) would actually pass the variable to the function, I tough it just gave the value to the pointer defined in the function and if that pointer was changed it wouldn''t have any effect on the original variable. And if you wanted to effect the original variable you''d have to use & infront of the passed variable, like you have to do in scanf.. Good to know that I''ll be actually changing the original variables.
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
Just a thought... if you have to define the boolean yourself, you might like to try it like this....
typedef enum BOOLEAN {false,true} BOOLEAN;
The way you have it, false will evaluate as true and true will evaluate as false - a bit confusing really.
typedef enum BOOLEAN {false,true} BOOLEAN;
The way you have it, false will evaluate as true and true will evaluate as false - a bit confusing really.
quote:
Original post by Cyberdrek
True, let me explain, if I were to do this is dos:
char array[10][10];
char *p = array;
I could access the data like so:
p[1][1] or p[11]; it would be the same thing.
char *p = array; requires a type cast.
char *p = (char*)array;
or
char *p = reinterpret_cast<char*>(array); // only works in C++
and after the cast you can't access p[1][1] but
a[1][1] == p[11].
/JanneVee
"Some People even believe that COBOL is a real programming language." Scott Meyers - Effective C++
Edited by - JanneVee on July 12, 2001 9:36:41 AM
/JanneVee"Some People even believe that COBOL is a real programming language." Scott Meyers - Effective C++
quote:
Original post by JanneVee
void clear_space(bool space[640][480][100]);
int main()
{
bool space[640][480][100];
clear_space(space);
return 0;
}
void clear_space(bool space[640][480][100])
{
int x,y,z;
for(x=0;x<640;x++)
for(y=0;y<480;y++)
for(z=0;z<100;z++)
space[x][y][z]=false;
}
I tried that and it compiled ok, but when I ran the program it crashed and I got this message with symify :
main + 18,line 26 of code.c (clear_space(space); is on that line)
I also tried the clear function without the for loops to make sure I'm not "overleaking" the space variable group. I also changed BOOLEAN to BOOLI so it can't be taken. What could cause this crash?
Here's the whole code.c just in case... :
#include
#include
#include
#include
#include
#include
typedef enum BOOLI {tosi,vale} BOOLI;
void set_coordinates(BOOLI space[640][480][100],int,int,int);
/*void flatten_space(BOOLI space[640][480][100],int,int,int);
void draw_space(BOOLI space[640][480][100]);*/
void clear_space(BOOLI space[640][480][100]);
int main()
{
short x,y,z;
BOOLI space[640][480][100];
/*allegro_init();
set_color_depth(16);
set_gfx_mode(GFX_AUTODETECT,640,480,0,0);
install_keyboard();*/
clear_space(space);
/*allegro_exit();*/
return 0;
}
void set_coordinates(BOOLI space[640][480][100],int x,int y,int z)
{
space[320+x][240+y][z]=tosi;
}
void clear_space(BOOLI space[640][480][100])
{
int x,y,z;
for(x=0;x<640;x++)
for(y=0;y<480;y++)
for(z=0;z<100;z++)
space[x][y][z]=vale;
}
Edited by - Afterlife on July 12, 2001 3:06:07 PM
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
The reason it crashes is because although all you would be pushing on the stack is the address to space, when your program checks its own integrity it things that you are going to pass the whole 30MB array to the stack. (sizeof( BOOLI[640][480][100] ) gives you the total size of all elements instead of the size of the pointer). Obviously you are not going to have a 30MB stack to work with so the system says ''No way, Jose!''.
You can probably get around this by fiddling with your compiler/linker settings. But it is a really bad practice. And it is about the least efficient way you could possible perform the function you want to perform. For each element in your array underneat your code will be performing 2 multiplications and 2 additions. That adds up to 60,000,000 mulitplies and 60,000,000 additions plus 30,000,000 memory moves each time clear_space is called. Slow as molasses? Yup.
You want to create your data as a single contiguous chunk of memory:
BOOLI* space = (BOOLI*)malloc( 640*480*100*sizeof( BOOLI ) );
then you can clear the entire array with one call:
void clear_space( BOOLI* space )
{
memset( space, 0, 640*480*100*sizeof( BOOLI ) );
}
Which underneath will only perform 1 addition and 1 memory copy for every 4 elements (on most systems anyways), for a total of a little less than 8,000,000 additions and 8,000,000 memory copies, and 3 multiplications for each time clear_space is called.
So ~16,000,003 total operations vs ~150,000,000 total operations.
And no nasty stack overflow errors. lol
Seeya
Krippy
You can probably get around this by fiddling with your compiler/linker settings. But it is a really bad practice. And it is about the least efficient way you could possible perform the function you want to perform. For each element in your array underneat your code will be performing 2 multiplications and 2 additions. That adds up to 60,000,000 mulitplies and 60,000,000 additions plus 30,000,000 memory moves each time clear_space is called. Slow as molasses? Yup.
You want to create your data as a single contiguous chunk of memory:
BOOLI* space = (BOOLI*)malloc( 640*480*100*sizeof( BOOLI ) );
then you can clear the entire array with one call:
void clear_space( BOOLI* space )
{
memset( space, 0, 640*480*100*sizeof( BOOLI ) );
}
Which underneath will only perform 1 addition and 1 memory copy for every 4 elements (on most systems anyways), for a total of a little less than 8,000,000 additions and 8,000,000 memory copies, and 3 multiplications for each time clear_space is called.
So ~16,000,003 total operations vs ~150,000,000 total operations.
And no nasty stack overflow errors. lol
Seeya
Krippy
thanks, sounds reasonable. I just realised even this crashes the program : int space[640][480][100]; space[0][0][0]=0;
but this on the other hand does work : int space[100][40][10]; space[0][0][0]=0;
If I were to use malloc, how would I change the values of certain points? Just like this : space[x][y][z]=tosi;?
but this on the other hand does work : int space[100][40][10]; space[0][0][0]=0;
If I were to use malloc, how would I change the values of certain points? Just like this : space[x][y][z]=tosi;?
------------------------------If there be no heaven,may there atleast be a hell.-------------------------------Afterlife-
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement