English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Language > Coders. C/C++

 
 
Thread Tools
Old 26 May 2019, 00:20   #1
PR77
Registered User

 
Join Date: Oct 2017
Location: Germany
Posts: 164
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" should be a pointer to a UWORD however it seems to default to a ULONG boundary. So;

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]);
Any ideas? CPU is 68000.
Attached Thumbnails
Click image for larger version

Name:	Pointer_Dereference.PNG
Views:	47
Size:	33.4 KB
ID:	63246  

Last edited by PR77; 26 May 2019 at 00:26. Reason: Added printf(...) for additional info. And fixed the heart condition. :)
PR77 is offline  
Old 26 May 2019, 01:53   #2
a/b
Registered User

 
Join Date: Jun 2016
Location: europe
Posts: 124
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.
a/b is offline  
Old 29 May 2019, 13:26   #3
PR77
Registered User

 
Join Date: Oct 2017
Location: Germany
Posts: 164
Quote:
Originally Posted by a/b View Post
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.

Thanks, it now works!
PR77 is offline  
Old 29 May 2019, 18:32   #4
Niklas
Registered User

 
Join Date: Apr 2018
Location: Stockholm / Sweden
Posts: 50
Quote:
Originally Posted by PR77 View Post
Thanks, it now works!
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.
Niklas is offline  
Old 29 May 2019, 19:20   #5
Samurai_Crow
Total Chaos forever!

Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Ft. Collins, CO USA
Age: 45
Posts: 1,307
Send a message via Yahoo to Samurai_Crow
Quote:
Originally Posted by Niklas View Post
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.
You need to be invited to use a Discord channel.
Samurai_Crow is offline  
Old 29 May 2019, 19:59   #6
Niklas
Registered User

 
Join Date: Apr 2018
Location: Stockholm / Sweden
Posts: 50
Quote:
Originally Posted by Samurai_Crow View Post
You need to be invited to use a Discord channel.
Ah, I see. Here's the invite link: https://discord.gg/7qYtGcz
Niklas is offline  
Old 29 May 2019, 20:09   #7
Samurai_Crow
Total Chaos forever!

Samurai_Crow's Avatar
 
Join Date: Aug 2007
Location: Ft. Collins, CO USA
Age: 45
Posts: 1,307
Send a message via Yahoo to Samurai_Crow
Thanks.
Samurai_Crow is offline  
Old 30 May 2019, 21:29   #8
PR77
Registered User

 
Join Date: Oct 2017
Location: Germany
Posts: 164
Hey thanks... Just registered...
PR77 is offline  
 


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 17:14
vbcc 0.9d phx News 43 13 July 2015 20:41
Mouse Pointer Washac support.WinUAE 2 19 February 2012 21:57
WB 1.3 pointer in Workbench 2.1 Bloodwych support.Apps 7 21 August 2010 16:21
Mouse pointer Washac support.Other 4 12 February 2010 02:38

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 14:21.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Page generated in 0.06912 seconds with 14 queries