|
HELP! Weird pointer declaraion behaviour needs explaning please
#385868
10/25/11 16:54
10/25/11 16:54
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Ow! My brain hurts from this one, so hopefully someone can explain. Im afraid the test code Im using is messy as hell so hopefully this 'sample' code will do well enough... Why is this (local) declaration of pointers ...
short *DATA=sys_malloc(stuff), *ROW; //(EXAMPLE#1)
behave different to this one? And WHAT its it doing?
short *DATA=sys_malloc(stuff); short *ROW; //(EXAMPLE#2)
What is happening with the examples is as follows... (we can ignore the actual content of DATA, as it is not important ATM)So I then set the secondary pointer ROW to And read some data into it from a file. All is going well. Now, when I access ROW[0] I get the correct data as expected, and then when using EXAMPLE#2 declarations, When I access ROW[1] and beyond, I actually get the data I expect. !! BUT !! If Im using EXAMPLE#1 declarations, When I access ROW[1] I get the data from (what should be) ROW[2]!!. After some HEAVY diagnostics, I have discovered that using EXAMPLE#1 declaration somehow messes up the declaration of the ROW pointer, so that when it is told to read through an 'array' of its own type(shorts), it is acually incrementing its memory location by FOUR instead of TWO. Like this diagram will hopefully show....
BYTES 01234567890123456789...
EXAMPLE#1 00..11..22..33..44.... BAD!
EXAMPLE#2 00112233445566778899... Good
The odd thing is, it is still interpreting the actual byte data CORRECTLY into its SHORT values... Any Ideas Anyone??? Im pretty sure its a mistake I'M making, but I am looking more for an explaination of WHAT is happening... Thankx in advance...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: HELP! Weird pointer declaraion behaviour needs explaning please
[Re: EvilSOB]
#385896
10/25/11 17:36
10/25/11 17:36
|
Joined: Sep 2009
Posts: 993 Budapest
Aku_Aku
User
|
User
Joined: Sep 2009
Posts: 993
Budapest
|
A version number (and edition) could make better approaching of the problem... Edit: Or A8 8.30.? should use? I saw that right now. I am sorry.
Last edited by Aku_Aku; 10/25/11 17:37.
|
|
|
Re: HELP! Weird pointer declaraion behaviour needs explaning please
[Re: Lukas]
#385926
10/25/11 18:44
10/25/11 18:44
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Aku: Im saying Im using 8.30.? because one machine I use is 8.30.2, and the other is 8.30.3, and I can never be sure which I'll be using when posting.
Lukas: Thats not really the point, as its position within DATA is immaterial ATM. The question could be summed up as why when I ask for ROW[1] it gives me data that is FOUR bytes away (in memory) from the data that is at ROW[0]... Being of type 'short' it should only be TWO bytes away.
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: HELP! Weird pointer declaraion behaviour needs explaning please
[Re: MrGuest]
#385953
10/26/11 01:07
10/26/11 01:07
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
MrGuest: Im not (intentionally) using any pointer-to-pointers, only pointer-to-data. For everyones perusal, here is a script where my problem is easily re-producable. Just edit the script in the area marked "AREA TO WATCH". Choose ONE of lines to uncomment based on the end-of-line comments and run it. I suggest running the "both work" line first so you can see how it is SUPPOSED to work. The last line in the 'watch' area is a new discovery, that makes me pretty damn sure the problem is an ENGINE bug, not mine. I just want some second opinions from you guys before I take it to the BugHunt forum. The Test Script...
#include <acknex.h>
#include <default.c>
//
//
//---------------------------------------------------------------------------------------
//
//
void main()
{
wait(1); level_load(NULL); wait(1); diag("\n\n\n");
draw_textmode("Courier",0,25,100); warn_level = 3; on_esc = NULL;
VECTOR col; vec_fill(col, 255); var idx;
//------------------------------------------------------------------------------------
//
//
short *row_1 = (short*)sys_malloc(sizeof(short)*20);
short *row_2 = (short*)sys_malloc(sizeof(short)*20);
//
//////////////////////////////////////////////////////////////////////////////////////
// THIS IS THE AREA TO WATCH //// only un-comment one line at a time ///////////////
//
short *point1, *point2, *dummy=sys_malloc(sizeof(short)); //both work
// short *point1, *dummy=sys_malloc(sizeof(short)), *point2; //only row_2 fails
// short *dummy=sys_malloc(sizeof(short)), *point1, *point2; //both fail
// now watch this NEW discovery!!
// short *dummy=sys_malloc(250), *point1, *point2; //both WORK if size_of function not used
//
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//
point1 = &row_1[5]; point2 = &row_2[5];
//
while(!key_esc)
{
memset(row_1, 0, sizeof(short)*20); memset(row_2, 0, sizeof(short)*20);
//
//------------------------------------------------------------------------------------
if(key_1) { for(idx=0; idx<9; idx++) row_1[idx+5] = idx+1; }
if(key_2) { for(idx=0; idx<9; idx++) row_2[idx+5] = idx+1; }
//
if(key_5) { for(idx=0; idx<9; idx++) point1[idx] = idx+1; }
if(key_6) { for(idx=0; idx<9; idx++) point2[idx] = idx+1; }
//------------------------------------------------------------------------------------
//
draw_text("Array 0.1.2.3.4.5.6.7.8.9.0.1.2.3.4.5.6.7.8.9.", 100, 100, col);
draw_text("Row_1", 100, 130, col); draw_text("Row_2", 100, 160, col);
for(idx=0; idx<20; idx++)
{ draw_text(str_for_int(NULL, row_1[idx]), 184+(idx*24), 130, col);
draw_text(str_for_int(NULL, row_2[idx]), 184+(idx*24), 160, col); }
//
draw_text("Hold the '1' key to fill row_1 directly via 'row_1[x]'", 100, 300, col);
draw_text("Hold the '2' key to fill row_2 directly via 'row_2[x]'", 100, 340, col);
draw_text("Hold the '5' key to fill row_1 via pointer 'point1[x]'", 100, 380, col);
draw_text("Hold the '6' key to fill row_2 via pointer 'point2[x]'", 100, 420, col);
//
wait(1);
}
//
//
//
//------------------------------------------------------------------------------------
if(key_shift) exec("notepad.exe", "acklog.txt");
sys_exit(0);
}
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: HELP! Weird pointer declaraion behaviour needs explaning please
[Re: EvilSOB]
#385956
10/26/11 01:36
10/26/11 01:36
|
Joined: Jul 2008
Posts: 1,178 England
MrGuest
Serious User
|
Serious User
Joined: Jul 2008
Posts: 1,178
England
|
lol, makes no sense to me.
int n = sizeof(short);
short *point1 = sys_malloc(n), *point2 = sys_malloc(n), *dummy = sys_malloc(n); //works
short *point1 = sys_malloc(sizeof(short)), *point2 = sys_malloc(n), *dummy = sys_malloc(n); //row2 fails
short *point1 = sys_malloc(n), *point2 = sys_malloc(sizeof(short)), *dummy = sys_malloc(n); //row2 fails
short *point1 = sys_malloc(n), *point2 = sys_malloc(n), *dummy = sys_malloc(sizeof(short)); //works
another few from research
|
|
|
Re: HELP! Weird pointer declaraion behaviour needs explaning please
[Re: MrGuest]
#385962
10/26/11 06:44
10/26/11 06:44
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
It appears to me that the 'size_of' function/macro is breaking/corrupting any other pointers (and maybe variables) that come after it in the same declaration statement.
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: HELP! Weird pointer declaraion behaviour needs explaning please
[Re: EvilSOB]
#386187
10/30/11 11:18
10/30/11 11:18
|
Joined: Apr 2009
Posts: 33 Germany
Bunsen
Newbie
|
Newbie
Joined: Apr 2009
Posts: 33
Germany
|
In Lite-C the pointer star "*" belongs to the data type. So if ROW should be a pointer to a short data you must declare:
short *DATA=sys_malloc(stuff), ROW; // ROW is a pointer
instead of this, where "*ROW" becomes a pointer to a pointer:
short *DATA=sys_malloc(stuff), *ROW; // Row is a pointer to a pointer
This behavior is not the the case in regular C/C++.
|
|
|
Re: HELP! Weird pointer declaraion behaviour needs explaning please
[Re: Bunsen]
#386188
10/30/11 11:34
10/30/11 11:34
|
Joined: Jul 2008
Posts: 1,178 England
MrGuest
Serious User
|
Serious User
Joined: Jul 2008
Posts: 1,178
England
|
then why does this work
#include <acknex.h>
#include <default.c>
void main(){
VECTOR *v1, v2;
//vec_set(v1, nullvector); //errors as expected
vec_set(v2, nullvector);
while(1){
DEBUG_VAR(v2.x, 20);
DEBUG_VAR(v2.y, 40);
DEBUG_VAR(v2.z, 60);
wait(1);
}
}
if v2 is still a pointer?
|
|
|
|