HELP! Weird pointer declaraion behaviour needs explaning please

Posted By: EvilSOB

HELP! Weird pointer declaraion behaviour needs explaning please - 10/25/11 16:54

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 ...
Code:
short *DATA=sys_malloc(stuff), *ROW;               //(EXAMPLE#1)


behave different to this one? And WHAT its it doing?
Code:
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
Code:
ROW = &DATA[0];


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....
Code:
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...
Posted By: Aku_Aku

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/25/11 17:36

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.
Posted By: Lukas

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/25/11 17:56

To set ROW to the DATA pointer, try using this instead:
Code:
ROW = DATA;



&DATA[0] seems to be supposed to take the first element, then a pointer to it. Or in the opposite order, which might become a problem...
Posted By: EvilSOB

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/25/11 18:44

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.

Posted By: MrGuest

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/25/11 23:04

Hey, I can't recreate the problem you have but just make sure you know all pointers are 4bytes not just 2 even if it is a short data type?
Posted By: EvilSOB

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/26/11 01:07

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...
Click to reveal..
Code:
#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);
}


Posted By: MrGuest

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/26/11 01:36

lol, makes no sense to me.
Code:
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
Posted By: EvilSOB

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/26/11 06:44

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.
Posted By: Bunsen

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/30/11 11:18

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:

Code:
short *DATA=sys_malloc(stuff), ROW;               // ROW is a pointer



instead of this, where "*ROW" becomes a pointer to a pointer:

Code:
short *DATA=sys_malloc(stuff), *ROW;               // Row is a pointer to a pointer



This behavior is not the the case in regular C/C++.
Posted By: MrGuest

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/30/11 11:34

then why does this work
Code:
#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?
Posted By: Superku

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/30/11 12:56

Quote:
In Lite-C the pointer star "*" belongs to the data type.

That's not correct, every lite-C data type is usually written as

BMAP* my_bmap = ...
or
TEXT* my_text = { ... }

but all it means is that you have a pointer *my_text that points to the TEXT object on the right side of the "=".
Posted By: Bunsen

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/30/11 14:08

You are missing the (local) inbetween assignment declaration. Try this:

Code:
void main()
{
	VECTOR v0;
	vec_set(v0, vector(1,2,3));
	
	VECTOR *v1 = &v0, *v2; // v2 is not a pointer to a vector as v1
		
	v2 = v1; // wrong
	
	while(1){
		DEBUG_VAR(v2.x, 20);
		DEBUG_VAR(v2.y, 40);
		DEBUG_VAR(v2.z, 60);
		wait(1);
	}
}



VECTOR is also not a good example because it's a typedef struct.

This runs correct because var2 is a short pointer and not a short data:
Code:
function main()
{
	short *var1 = sys_malloc(10*sizeof(short)), var2;
	
	int i;
	for (i=0; i<10; i++)
		var1[i] = i;
	
	var2 = var1; // correct
	
	printf("%d", var1[6]); 
	printf("%d", var2[6]); // Expected result "6"
}



This is wrong, because var2 becomes a pointer to a short pointer:
Code:
function main()
{
	short *var1 = sys_malloc(10*sizeof(short)), *var2;
	
	int i;
	for (i=0; i<10; i++)
		var1[i] = i;
	
	var2 = var1; // wrong
	
	printf("%d", var1[6]);
	printf("%d", var2[6]); // Not expected result "6"
}



This is correct, because var2 is handled as a pointer to a pointer:
Code:
function main()
{
	short *var1 = sys_malloc(10*sizeof(short)), *var2;
	
	int i;
	for (i=0; i<10; i++)
		var1[i] = i;
	
	var2 = &var1; // correct
	
	printf("%d", var1[6]);
	printf("%d", *((*var2) + 6)); // Expected result "6"
}


Posted By: EvilSOB

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/30/11 22:41

Please forgive me but my morning coffee has not reached my brain yet...

I see things here I agree with, some I dis-agree with, and some that i need to check.

BUT... The most important thing to be uncovered in this thread, I feel, was what
I discovered during my post that contains my complete test script.

And that discovery was this...
Code:
short *DATA=sys_malloc(sizeof(short)*10), *ROW;    //this line FAILS

short *DATA=sys_malloc( 4 * 10), *ROW;             //this line WORKS as I would expect


Posted By: Bunsen

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/31/11 09:32

@EvilSOB:

you are right! There must be a strange error with the sizeof() Macro.

This version runs correctly:
Code:
function main()
{
	int n = sizeof(short);
	short *var1 = sys_malloc(10 * n), *var2;
	// or using constants: short *var1 = sys_malloc(10 * 2), *var2;
	
	int i;
	for (i=0; i<10; i++)
		var1[i] = i;
	
	var2 = var1; // correct
	
	printf("%d", var1[6]);
	printf("%d", var2[6]); // Expected result "6"
}



where this version turns var2 into a pointer to a short pointer:

Code:
short *var1 = sys_malloc(10 * sizeof(short)), *var2;


Posted By: Rei_Ayanami

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/31/11 09:39

There once was a similar problem...

"sizeof() is a macro and not a function. It can be used for setting a variable or passing a parameter to a function, but can not be used in expressions. Thus, long_size = sizeof(long); is ok, but long_size_times_ten = sizeof(long)*10; is not. This restriction does not apply to A7.7 or above."

Maybe that is happening again?
Posted By: EvilSOB

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/31/11 10:35

FINALLY !! Someone agrees with me! Woohoo!

Now I feell 'validated' enough to pass ths over to the "Bug Hunt" forum.

Thanks guys...

I will post results from JCL here when I get them...

HERE is the post in Bug Hunt....


Posted By: Bunsen

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/31/11 12:13

@EvilSOB:

you had better wait for some other detections (you never know, if JCL has got his dayly ration of "Frischfleisch").

This works too:
Code:
short *v1=(short*)sys_malloc(sizeof(short)*5);
short *v2;



So it would be possible, that the sizeof() Macro is working, but inbetween assignments (at least in combination
with sizeof() not.
Posted By: EvilSOB

Re: HELP! Weird pointer declaraion behaviour needs explaning please - 10/31/11 15:43

Being added to the bug list shortly...

Originally Posted By: JCL

Yes, indeed the compiler assigns a wrong type to the second pointer, and it's indeed apparently related to sizeof() in the same line. Seems that no one has encountered this strange bug before.

It's too late for 8.30, but this will be fixed in 8.40. Until then, please don't define a pointer in the same line behind a sizeof() call.


© 2024 lite-C Forums