C question

From: Cini, Richard <RCini_at_congressfinancial.com>
Date: Thu Aug 1 12:48:02 2002

Erik:

        No errors or warnings are emitted by the compiler, so the way I'm
determining that the various functions work or not work is by looking at a
"core dump" of the emulator memory (performed by another routine). In all
but the first example, the memory is empty (they remain the initialized
value of 0). The first way, the emulator memory indeed shows the contents of
the loaded ROM.

        I've "watched" the contents of the roms array and have also
outputted the values to the debug screen. The values are as expected.

Rich


-----Original Message-----
From: Eric J. Korpela [mailto:korpela_at_ssl.berkeley.edu]
Sent: Thursday, August 01, 2002 12:34 PM
To: cctech_at_classiccmp.org
Subject: Re: C question


> // Read ROM directly to memory. This works.
> ppmem = &ucMem[roms[i].iROMStart + MEM_ROM] ;
> fread(ppmem, sizeof(byte), roms[i].iROMLen, pFH) ;
>
> //this doesn't work...
> //fread(&ucMem[roms[i].iROMStart + MEM_ROM], sizeof(byte),
roms[i].iROMLen,
> pFH) ;
>
> //...nor does this
> //fread((char *)ucMem[roms[i].iROMStart + MEM_ROM], sizeof(byte),
> roms[i].iROMLen, pFH) ;
>
> So here's where I'm missing it. Under all three fread scenarios, the
> compiler doesn't throw a warning...they all compile cleanly. But only the
> first works.

Lack of warnings or code don't have anything to do with whether the code
is correct...

The last one is the easiest to figure out....

Call fread with the following parameters:
  1. byte value at address (ucMem+roms[i].iROMStart+MEM_ROM) converted into
      a pointer to type char.
  2. The value 1 (assuming byte is typedefed to "unsigned char")
  3. The value stored in roms[i].iROMLen
  4. The value stored in pFH.

It's fairly easy to see why the first parameter is wrong... No warning is
issued because the type of the first parameter is correct. Without the
typecast, a warning should be issued.

The previous two look equivalent to me. I don't really see a significant
difference. Are you sure "i" and "roms" are set to what you think it should
be.

Personally, I would have written the first parameter as
"ucMem+roms[i].iROMStart+MEM_ROM" as I think it more clearly represents
what you mean.

What is the error reported? What are the return values from fread? (You
should be checking that the return value equals roms[i].iROMLen.)

Eric
Received on Thu Aug 01 2002 - 12:48:02 BST

This archive was generated by hypermail 2.3.0 : Fri Oct 10 2014 - 23:34:35 BST