-
April 12th, 2007, 07:53 PM
#1
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?
-
April 12th, 2007, 08:08 PM
#2
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
-
April 12th, 2007, 08:19 PM
#3
Re: Why are these 2 code blocks not the same?
I don't know how to use the debugger.
-
April 12th, 2007, 08:46 PM
#4
Re: Why are these 2 code blocks not the same?
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
-
April 12th, 2007, 09:19 PM
#5
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.
-
April 12th, 2007, 09:55 PM
#6
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.
-
April 12th, 2007, 10:09 PM
#7
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.
-
April 12th, 2007, 10:41 PM
#8
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
-
April 12th, 2007, 10:58 PM
#9
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)
-
April 15th, 2007, 08:22 PM
#10
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.
-
April 15th, 2007, 10:35 PM
#11
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.
-
April 16th, 2007, 12:25 AM
#12
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
-
April 16th, 2007, 07:23 AM
#13
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.
-
April 16th, 2007, 08:03 AM
#14
Re: Why are these 2 code blocks not the same?
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
-
April 16th, 2007, 08:40 AM
#15
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;
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|