CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 30

Thread: Why are these 2 code blocks not the same?

  1. #1
    Join Date
    Apr 2004
    Posts
    204

    Why are these 2 code blocks not the same?

    These 2 code blocks should function identically, I would think.

    I marked in bold the only thing I changed (2 places).

    This code functions properly:
    Code:
    #define MAX_SECTORS_PER_READ    27
    #define RAW_SECTOR_SIZE     2352
    
    ULONG  sectorsPerRead;
    ULONG  bytesPerRead;
    
    sectorsPerRead = MAX_SECTORS_PER_READ;
    bytesPerRead = sectorsPerRead * RAW_SECTOR_SIZE;
    
    fSuccess = DeviceIoControl(pextr->hCDROM,
                               IOCTL_CDROM_RAW_READ,   
                               &info, 
                               sizeof(RAW_READ_INFO),  
                               smpl,
                               sectorsPerRead * RAW_SECTOR_SIZE,      
                               &bytesRead,             
                               0);                    
    
    if(bytesRead != sectorsPerRead * RAW_SECTOR_SIZE) __leave;
    This one fails:
    Code:
    #define SECTORS_PER_READ    27
    #define RAW_SECTOR_SIZE     2352
    
    ULONG  sectorsPerRead;
    ULONG  bytesPerRead;
    
    sectorsPerRead = MAX_SECTORS_PER_READ;
    bytesPerRead = sectorsPerRead * RAW_SECTOR_SIZE;
                      
    fSuccess = DeviceIoControl(pextr->hCDROM,         
                               IOCTL_CDROM_RAW_READ,  
                               &info,                  
                               sizeof(RAW_READ_INFO), 
                               smpl,                   
                               bytesPerRead,           
                               &bytesRead,             
                               0);                   
    
    if(bytesRead != bytesPerRead) __leave;
    These makes no sense to me, as I see them as identical.

    Am I missing something here?

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: Why are these 2 code blocks not the same?

    Use your debugger, is the value of "bytesread" the same in both cases?
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Apr 2004
    Posts
    204

    Re: Why are these 2 code blocks not the same?

    I don't know how to use the debugger.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why are these 2 code blocks not the same?

    Quote Originally Posted by mmscg
    I don't know how to use the debugger.
    Well you need to learn to use it for cases such as yours. No one will be able to duplicate this problem, since the two blocks seem identical.

    But to let you know, you won't get far at all in this field unless you know how to debug your own programs using the debugger. It is a requirement, so you need to learn it.

    What if your program were 10,000 lines long? How would you go about solving the problem? I don't think posting such a program on CodeGuru will get you a lot of help.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    May 2003
    Posts
    424

    Re: Why are these 2 code blocks not the same?

    You could convert bytesPerRead and the other to a string and then show what their values are in a messagebox.

  6. #6
    Join Date
    Oct 2006
    Location
    Singapore
    Posts
    346

    Red face Re: Why are these 2 code blocks not the same?

    Look closely... The first two lines differ. I'm not sure if this is a typo but if not then you might have defined MAX_SECTORS_PER_READ somewhere else with a different value.
    Believe in your Dreams, Work for what you Believe in.
    My thoughts? Angelo's Stuff
    Some fun things I've done: RayWatch, QuickFeed, ACSVParser

    @ngelo

  7. #7
    Join Date
    Apr 2004
    Posts
    204

    Re: Why are these 2 code blocks not the same?

    Paul McKenzie
    No instructions came with VC++ that I purchased, nothing about debugger that I've ever seen on the net, ditto in any C++ books I bought.

    If you'd care to explain, I'll listen.


    aewarnick
    That's what I am doing right now...


    angelorohit
    Should both be MAX_SECTORS_PER_READ (OK in my program, typo here at forum)
    Good catch though!!
    Last edited by mmscg; April 12th, 2007 at 10:32 PM.

  8. #8
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    Re: Why are these 2 code blocks not the same?

    Well you haven't searched around much on the net then since in less than 1 minute I've found loads of resources on debugging.

    Try this one for starters

    Visual Studio Debugging

    As Paul said you won't get far if you can't use a debugger.

    Regards
    Alan

  9. #9
    Join Date
    Apr 2004
    Posts
    204

    Re: Why are these 2 code blocks not the same?

    OK thanks! (I guess I wasn't searching properly... C++ tutorial, win32 tutorial, etc. were typical searches I did)

  10. #10
    Join Date
    Apr 2004
    Posts
    204

    Re: Why are these 2 code blocks not the same?

    OK, after learing how to set breakpoints while using the debugger, I have determined why
    the two code blocks do not behave the same.

    Use your debugger, is the value of "bytesread" the same in both cases?
    Yes, they are the same in both cases.

    However, the value of sectorsPerRead CHANGES during the execution of a loop,
    even though I do not reset it's value.

    This should be impossible, should it not?

    Here is the loop:
    Code:
    do
    {
        while(startingSector + sectorsPerRead <= endingSector)
        {   
    
            info.DiskOffset.QuadPart = (ULONGLONG)(startingSector*(ULONGLONG)2048);
            info.SectorCount         = sectorsPerRead;
            info.TrackMode           = CDDA;
     
            // read data (samples) from CD
            fSuccess = DeviceIoControl(pextr->hCDROM,          
                                       IOCTL_CDROM_RAW_READ,     
                                       &info,                  
                                       sizeof(RAW_READ_INFO),  
                                       smpl,                   
                                       sectorsPerRead * RAW_SECTOR_SIZE,            
                                       &bytesRead,             
                                       0);                     
                    
            if(bytesRead != sectorsPerRead * RAW_SECTOR_SIZE) __leave;
            BytesToWrite = bytesRead;
                    
            // write data (samples) to hard disk
            fSuccess = WriteFile(hWav, smpl, BytesToWrite, &BytesWritten, 0);
                    
            startingSector += sectorsPerRead;
    
        } 
    } while((sectorsPerRead >>= 1) != 0);
    Last edited by mmscg; April 15th, 2007 at 08:45 PM.

  11. #11
    Join Date
    Oct 2006
    Location
    Singapore
    Posts
    346

    Re: Why are these 2 code blocks not the same?

    However, the value of sectorsPerRead CHANGES during the execution of a loop,
    even though I do not reset it's value.
    Which loop? The inner while loop or the outer do-while? If you consider the outer loop, then it is evident that the change to sectorsPerRead occurs in the while condition of the do-while loop.
    Believe in your Dreams, Work for what you Believe in.
    My thoughts? Angelo's Stuff
    Some fun things I've done: RayWatch, QuickFeed, ACSVParser

    @ngelo

  12. #12
    Join Date
    Jan 2003
    Posts
    615

    Re: Why are these 2 code blocks not the same?

    Is this correct (right shift and assignment operator)?
    Code:
    while((sectorsPerRead >>= 1) != 0);
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  13. #13
    Join Date
    Apr 2004
    Posts
    204

    Re: Why are these 2 code blocks not the same?

    Which loop? The inner while loop or the outer do-while?
    It changes during the execution of the inner loop.


    while((sectorsPerRead >>= 1) != 0);
    Actually, this was to be my next question, as I don't understand what this line is actually doing in the loop.

  14. #14
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Why are these 2 code blocks not the same?

    Quote Originally Posted by mmscg
    However, the value of sectorsPerRead CHANGES during the execution of a loop,
    even though I do not reset it's value.

    This should be impossible, should it not?
    It is highly possible if your parameters to DeviceIoControl are not correct.

    From MSDN:
    BOOL DeviceIoControl(
    HANDLE hDevice,
    DWORD dwIoControlCode,
    LPVOID lpInBuffer,
    DWORD nInBufferSize,
    LPVOID lpOutBuffer,
    DWORD nOutBufferSize,
    LPDWORD lpBytesReturned,
    LPOVERLAPPED lpOverlapped
    );
    Please go through all of your parameters that you're passing to DeviceIoControl. So far, I see discrepancies in what you've declared, and what DeviceIoControl expects. You have ULONG, but DeviceIoControl says DWORD. You have other discrepancies in the types you've sent, and the types DeviceIoControl expects.

    Since DeviceIoControl is going to write data on return to your buffers that you've passed as parameters, they had better be big enough for the information. If not, DeviceIoControl will inadvertantly cause a memory overwrite, and more than likely, this is what happened. That memory that was overwritten was probably your "sectorsPerRead" variable.

    Did the variable change when DeviceIoControl was called? In other words, was sectorsPerRead OK before the call to DeviceIoControl, and then changed after the call to DeviceIoControl? If this is what happened, then it looks like what I've stated happened did happen.

    Regards,

    Paul McKenzie

  15. #15
    Join Date
    Oct 2006
    Location
    Singapore
    Posts
    346

    Arrow Re: Why are these 2 code blocks not the same?

    Yes, what Paul McKenzie says is highly probable.
    Quote:
    while((sectorsPerRead >>= 1) != 0);
    Actually, this was to be my next question, as I don't understand what this line is actually doing in the loop.
    This is the line that is changing the value of sectorsPerRead in the outer loop. Essentially, it shifts the bits of sectorsPerRead by one towards the right. This is a fast and efficient way of dividing by 2. Its the same as saying :
    Code:
    sectorsPerRead /= 2;
    Believe in your Dreams, Work for what you Believe in.
    My thoughts? Angelo's Stuff
    Some fun things I've done: RayWatch, QuickFeed, ACSVParser

    @ngelo

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)