Gamestudio Links
Zorro Links
Newest Posts
loading historical data 1st time
by AndrewAMD. 04/14/23 12:54
Trade at bar open
by juanex. 04/13/23 19:43
Bug in Highpass2 filter
by rki. 04/13/23 09:54
Adding Limit Orders For IB
by scatters. 04/11/23 16:16
FisherN
by rki. 04/11/23 08:38
AUM Magazine
Latest Screens
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Hecknex World
Who's Online Now
1 registered members (Grant), 999 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
rki, FranzIII, indonesiae, The_Judge, storrealba
18919 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Pointer confusion? #285790
08/20/09 07:51
08/20/09 07:51
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline OP
Serious User
DJBMASTER  Offline OP
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
Hi i'm a little confused about a pointer issue.
Code:
var sound_handle = 0 // variable sound handle
var* pnt_to_sound = &sound_handle; // pointer to handle


Ok, all well and good, the pointer is set to point to the variable.

Now i'm trying to play a sound and return the handle into the var.

Setting it directly...
Code:
sound_handle = snd_play("hi.wav",100,0);


no errors here. Setting it through the pointer...
Code:
*pnt_to_sound = snd_play("hi.wav",100,0);


Produces a crash. I thought it would work like this because dereferencing the pointer gets you the variable.

I dont really work with pointers (i'm a C# guy), so i dont know what's wrong.

Last edited by DJBMASTER; 08/20/09 07:51.
Re: Pointer confusion? [Re: DJBMASTER] #285794
08/20/09 08:02
08/20/09 08:02
Joined: Sep 2003
Posts: 928
Spirit Offline

Moderator
Spirit  Offline

Moderator

Joined: Sep 2003
Posts: 928
This looks ok to me, except theres a ; missing in your sound_handle definition.

The crash means that your ptr_to_sound is still undefined or 0. So check before that its really set to &sound_handle.

Re: Pointer confusion? [Re: Spirit] #285823
08/20/09 11:54
08/20/09 11:54
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline OP
Serious User
DJBMASTER  Offline OP
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
yeh the ; was an error when i was typing the post. I'll see if the pointer is invalid...

Re: Pointer confusion? [Re: DJBMASTER] #299417
11/23/09 22:28
11/23/09 22:28
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline OP
Serious User
DJBMASTER  Offline OP
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
Didn't want to start a new post, so i'll resurrect my old one...
Code:
var sound_handle = 0; // variable sound handle
var* pnt_to_sound = &sound_handle; // pointer to handle

function TestPointer()
{
if(pnt_to_sound == NULL)
{
error("Null Pointer!");
}
else
{
*pnt_to_sound = snd_play("hi.wav",100,0);
}
}

void main()
{
on_space = TestPointer;
}


Pressing 'space' gives me the 'NUll Pointer' message, but i'm not sure why because it is clearly assigned an address.

Re: Pointer confusion? [Re: DJBMASTER] #299420
11/23/09 22:50
11/23/09 22:50
Joined: Feb 2006
Posts: 1,011
Germany
pegamode Offline
Serious User
pegamode  Offline
Serious User

Joined: Feb 2006
Posts: 1,011
Germany
In lite-c (as it is in c / c++) NULL is defined as 0.

So NULL == 0

Regards,
Pegamode.

Re: Pointer confusion? [Re: pegamode] #299421
11/23/09 22:54
11/23/09 22:54
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline OP
Serious User
DJBMASTER  Offline OP
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
Originally Posted By: pegamode
In lite-c (as it is in c / c++) NULL is defined as 0.

So NULL == 0

Regards,
Pegamode.


Same thing happens if 'var sound_handle = 5;'

Pointers are also only null when the pointer is set to 0, not the value it is referencing.

Last edited by DJBMASTER; 11/23/09 22:56.
Re: Pointer confusion? [Re: DJBMASTER] #299422
11/23/09 22:59
11/23/09 22:59
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline
Expert
Joozey  Offline
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Try if( key_space ) { ... } in a while loop, and see if anything changes the situation. It might be to do with pre-initalisation of on_space or whatever. I never trust those functions tongue.


Click and join the 3dgs irc community!
Room: #3dgs
Re: Pointer confusion? [Re: Joozey] #299427
11/23/09 23:10
11/23/09 23:10
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline OP
Serious User
DJBMASTER  Offline OP
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
It doesn't give me a NULL pointer meesage if the pointer is referenced inside a function...
Code:
var sound_handle = 0; // variable sound handle
var* pnt_to_sound; // pointer to handle

function TestPointer()
{
if(pnt_to_sound == NULL)
{
error("Null Pointer!");
}
}

void main()
{
pnt_to_sound =  &sound_handle;
on_space = TestPointer;
}



Can't pointers be referenced in their definition in lite-c?

Re: Pointer confusion? [Re: DJBMASTER] #299429
11/23/09 23:38
11/23/09 23:38
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
having your pointer definition like so
var* pnt_to_sound; // pointer to handle
DESNT guarentee it is at zero if it hasn't been used yet.

Use this version to be sure.
var* pnt_to_sound = NULL; // pointer to handle

AND remember to manually set the pointer back to NULL (or zero) when its been stopped.


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Pointer confusion? [Re: EvilSOB] #299431
11/23/09 23:42
11/23/09 23:42
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline OP
Serious User
DJBMASTER  Offline OP
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
Yes,I know that setting it to NULL is the best way, but I can't get it to work when assigning it a memory address.

Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1