25 May 2019, 23:20 | #1 |
Registered User
Join Date: Oct 2017
Location: Germany
Posts: 193
|
VBCC and pointer dereferencing
I'm trying to finalise the code for my 68000 CPU Relocator with FLASH kickstart support (https://github.com/PR77/68000_Relocator_FLASH_Kickstart) however I am stuck on some pointer arithmetic. Here is a simplified version of the code;
Code:
fileHandle = Open("test.txt", MODE_OLDFILE); if (0L != fileHandle) { APTR memoryHandle = AllocMem(0x80000, 0); if (memoryHandle) { ULONG totalBytesReadFromFile = 0; UWORD *wordToWrite; totalBytesReadFromFile = Read(fileHandle, memoryHandle, 0x80000); wordToWrite = (UWORD *)memoryHandle …. } } wordToWrite[0] dereferences the LSB of the first ULONG. wordToWrite[1] dereferences the LSB of the second ULONG, and so on. I put some details in the attached picture. The test.txt file is simply ASCII "1234567890". The output in the image is generated by the following Code:
printf("Current index: %d, wordInMem: %x, wordToWrite: %x\n", currentWordWritten, (UWORD)memoryHandle[currentWordWritten], (UWORD)wordToWrite[currentWordWritten]); Last edited by PR77; 25 May 2019 at 23:26. Reason: Added printf(...) for additional info. And fixed the heart condition. :) |
26 May 2019, 00:53 | #2 |
Registered User
Join Date: Jun 2016
Location: europe
Posts: 1,039
|
memoryHandle is a longword pointer, wordToWrite is a word pointer. They both point to the same location (to a "1" in that ASCII string).
When you dereference them via array indexing, you get: memoryHandle[0] = "1234" (0x31323334) wordToWrite[0] = "12" (0x3132) Then, you cast both values (not pointers, values!) to a word, thus cutting off the high-word of "1234", resulting in "34". Now, ((UWORD*) memoryHandle)[0] would give you "12". That's a pointer cast, not a value cast. |
29 May 2019, 12:26 | #3 | |
Registered User
Join Date: Oct 2017
Location: Germany
Posts: 193
|
Quote:
Thanks, it now works! |
|
29 May 2019, 17:32 | #4 |
Registered User
Join Date: Apr 2018
Location: Stockholm / Sweden
Posts: 129
|
Hi PR77. This is slightly off-topic, but there's a channel, The Lab, on Retro Man Cave's discord (https://discordapp.com/channels/4313...45410385592321) that you might find interesting, where you could ask these kind of question and get answers quickly.
|
29 May 2019, 18:20 | #5 | |
Total Chaos forever!
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 49
Posts: 2,186
|
Quote:
|
|
29 May 2019, 18:59 | #6 |
Registered User
Join Date: Apr 2018
Location: Stockholm / Sweden
Posts: 129
|
Ah, I see. Here's the invite link: https://discord.gg/7qYtGcz
|
29 May 2019, 19:09 | #7 |
Total Chaos forever!
Join Date: Aug 2007
Location: Waterville, MN, USA
Age: 49
Posts: 2,186
|
Thanks.
|
30 May 2019, 20:29 | #8 |
Registered User
Join Date: Oct 2017
Location: Germany
Posts: 193
|
Hey thanks... Just registered...
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
VBCC - What's going on here? | deimos | Coders. C/C++ | 69 | 28 July 2018 16:14 |
vbcc 0.9d | phx | News | 43 | 13 July 2015 19:41 |
Mouse Pointer | Washac | support.WinUAE | 2 | 19 February 2012 20:57 |
WB 1.3 pointer in Workbench 2.1 | Bloodwych | support.Apps | 7 | 21 August 2010 15:21 |
Mouse pointer | Washac | support.Other | 4 | 12 February 2010 01:38 |
|
|