1 registered members (AndrewAMD),
684
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: HELP! Weird pointer declaraion behaviour needs explaning please
[Re: MrGuest]
#386192
10/30/11 12:56
10/30/11 12:56
|
Joined: Sep 2003
Posts: 6,861 Kiel (Germany)
Superku
Senior Expert
|
Senior Expert
Joined: Sep 2003
Posts: 6,861
Kiel (Germany)
|
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 "=".
"Falls das Resultat nicht einfach nur dermassen gut aussieht, sollten Sie nochmal von vorn anfangen..." - Manual Check out my new game: Pogostuck: Rage With Your Friends
|
|
|
Re: HELP! Weird pointer declaraion behaviour needs explaning please
[Re: Superku]
#386198
10/30/11 14:08
10/30/11 14:08
|
Joined: Apr 2009
Posts: 33 Germany
Bunsen
Newbie
|
Newbie
Joined: Apr 2009
Posts: 33
Germany
|
You are missing the (local) inbetween assignment declaration. Try this:
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:
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:
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:
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"
}
|
|
|
Re: HELP! Weird pointer declaraion behaviour needs explaning please
[Re: Bunsen]
#386221
10/30/11 22:41
10/30/11 22:41
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
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...
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
"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]
#386232
10/31/11 09:32
10/31/11 09:32
|
Joined: Apr 2009
Posts: 33 Germany
Bunsen
Newbie
|
Newbie
Joined: Apr 2009
Posts: 33
Germany
|
@EvilSOB: you are right! There must be a strange error with the sizeof() Macro. This version runs correctly:
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:
short *var1 = sys_malloc(10 * sizeof(short)), *var2;
|
|
|
Re: HELP! Weird pointer declaraion behaviour needs explaning please
[Re: Rei_Ayanami]
#386234
10/31/11 10:35
10/31/11 10:35
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
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....
Last edited by EvilSOB; 10/31/11 10:48. Reason: Link to bug hunt added
"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]
#386240
10/31/11 12:13
10/31/11 12:13
|
Joined: Apr 2009
Posts: 33 Germany
Bunsen
Newbie
|
Newbie
Joined: Apr 2009
Posts: 33
Germany
|
@EvilSOB: you had better wait for some other detections (you never know, if JCL has got his dayly ration of "Frischfleisch"). This works too:
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.
|
|
|
Re: HELP! Weird pointer declaraion behaviour needs explaning please
[Re: Bunsen]
#386254
10/31/11 15:43
10/31/11 15:43
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Being added to the bug list shortly... 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.
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
|