Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Pointers

Author
Message
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 22nd Jun 2008 23:26
I understand they are a powerful tool, but I just can't understand them, and my programs are missing out.

Anyone have a little ditty that might make it all make sense?
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 23rd Jun 2008 08:42


what's the proper way to going about sending an array through ym function?
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 23rd Jun 2008 08:59


Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 23rd Jun 2008 09:09
I don't see any reason to use pointers... What's the point of them? (lol i mad a funneh.)

If you can do any models for FW, reply to the FleetWars thread.

Click here!
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 23rd Jun 2008 09:16


that works. ^^^

wasnt touching my prototype was the issue
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 23rd Jun 2008 09:42
Quote: "I don't see any reason to use pointers... What's the point of them?"


Assuming you don't mean what's the point of them as opposed to references, here's an example(won't compile on its own):



As you can see I've made a struct that stores 1024 float values, this could be heightmap data or anything you like. If I were to pass this to the first function, 1024 floats would be allocated on the stack and then all the floats from the 'whatever' instance of my 'Example' struct would be copied over to 'myExample' in the first function. This is inefficient as copying over 1024 floats cannot be done instantly and it's pointless because I don't need to modify them separately. Whereas in the second function I pass the address to 'whatever', this means that likely only 4bytes is passed depending on OS etc, and no floats are copied anywhere, I can then access the 1024 floats that are stored within my 'Example' struct via 'myExample->kool[42] = 1337.0f;'. This is only a basic example but on a larger scale it can be very beneficial.

Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 23rd Jun 2008 09:51
Oh, I see. Thank you so much.

If you can do any models for FW, reply to the FleetWars thread.

Click here!
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 23rd Jun 2008 09:58
basically, you can change a variable's contents across functions and similar blocks of code without it having to be global.
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 24th Jun 2008 10:14
if this modifies the original array:


what code doesnt?
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 24th Jun 2008 10:17
The names of arrays are actually pointers. So, using that code is using pointers, unbeknown to you.
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 24th Jun 2008 10:20
i was actually quite aware of that.

hence being able to come up with the above code.

perhaps you could answer my question?
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 24th Jun 2008 10:22
As far as I know, arrays can only be passed-by-reference, not passed-by-value.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 24th Jun 2008 10:23
It modifies the original array because 'void changeArrayVals(int pArray[5])' is the same as 'void changeArrayVals(int* pArray)', you'd probably want to use the latter as the size you put in the brackets there doesn't change anything.

Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 24th Jun 2008 10:25
Quote: "void changeArrayVals(int* pArray)"


I second that recommendation. It keeps you from having to alter the expected size of the array. Won't work for multi-dimensionals, though.
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 24th Jun 2008 10:26
Quote: "Won't work for multi-dimensionals, though."


would dark coder agree with that?
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 24th Jun 2008 10:28
That he would. It doesn't know where to start each dimension.
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 24th Jun 2008 10:29
so a double asterisk ** wouldnt work..
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 24th Jun 2008 10:29
Well 'int* something' is merely an address to an int, assuming this int isn't an address(which it shouldn't be) you'd have to use 'int** something' for a 2D array.

elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 24th Jun 2008 10:31
ahHA
elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 24th Jun 2008 10:47
so i've got this function:


and this array:


how would i call my function?
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 24th Jun 2008 10:59
With: change2DArrayVals( my2DArray );

Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 24th Jun 2008 11:27 Edited at: 24th Jun 2008 11:28
I just meant you couldn't do this:

elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 24th Jun 2008 12:04 Edited at: 24th Jun 2008 12:05
Quote: "change2DArrayVals( my2DArray );"



tried that...


elantzb
16
Years of Service
User Offline
Joined: 10th May 2008
Location: Classified
Posted: 24th Jun 2008 12:16 Edited at: 24th Jun 2008 12:17
i hear that the only way to do it would be to change my function:


sadly, this means that my array can only have a second dimension of a set size.
dark coder
21
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 24th Jun 2008 13:01
Oops, I'd never actually used static arrays in this manner so it was just a guess. You'd need to pass 'int* whatever'; and manually calculate the offset, unless you specify it in the function declaration like you're doing. You might be better off dynamically allocating your own array or using a container such as a vector to handle your multi dimension arrays, you could resize it then.

I.e.



or



Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 24th Jun 2008 18:20
I use the second of those methods. I prefer it.
Zuka
16
Years of Service
User Offline
Joined: 21st Apr 2008
Location: They locked me in the insane asylum.
Posted: 27th Jun 2008 21:50
I feel stupid.

Login to post a reply

Server time is: 2024-09-30 01:37:44
Your offset time is: 2024-09-30 01:37:44