【问题标题】:Jump out of function in for loop while debugging in visual studio c在Visual Studio C中调试时跳出for循环中的功能
【发布时间】:2016-11-21 20:47:49
【问题描述】:

我想编辑文件的一些位。我打开文件并确定第一个字节指针。因此,在 ChangeBit 函数中,将一些位替换为 1。 当我想调试 ChangeBit 函数时,在 for 循环的第一次迭代中,调试光标从函数中跳出并且进程不会终止并且代码继续到函数之后的行。
怎么了?

void changeBit(char *ptr, int bitLen, int startPoint)
{
    ptr += startPoint / 8;
    startPoint %= 8;
    int tmpBit = 1;
    for (int i = startPoint; i < bitLen; i++)
    {
        ptr[i / 8] = (ptr[i / 8] | tmpBit << (7 - (i % 8)));
        //after this line in first iteration, debug cursor jump out of function
    }
}

int main(void)
{
// open and mmap() the file
    hFile = CreateFile(input_file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
    bytes_left = GetFileSize(hFile, NULL);
    hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
    stream_pos = (unsigned char*)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);   

    changeBit(stream_pos,5,0); // not working

    // copy all bytes to other memory but still not working
    char *OutFileContent;
    OutFileContent = malloc(bytes_left);
    memcpy(OutFileContent, stream_pos, bytes_left);

    changeBit(OutFileContent,5,0); // not working

    //write new bits stream to out file
}

指针有问题吗?

【问题讨论】:

  • changeBit(OutFileContent,5,12); --> changeBit(OutFileContent,12,5);?
  • 否,changeBit 函数适用于任何正偏移量。并假设输入文件大于 10 K。
  • 你没有抓住重点。您的最后两个参数似乎已交换。因此,for 循环体永远不会执行,因为startPoint 大于bitlen。或者你需要改变for循环条件。
  • 好的,抱歉,我编辑问题。它来自坏例子。但问题还没有解决。

标签: c++ c visual-studio debugging pointers


【解决方案1】:

您可以在调试模式下使用异常设置窗口抛出异常:

https://msdn.microsoft.com/en-us/library/x85tt0dd.aspx

请同时检查以下代码:

hFile = CreateFile(input_file_name, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
bytes_left = GetFileSize(hFile, NULL);
hMap = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
stream_pos = (unsigned char*)MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);   

我将其更改为以下示例,它在我身边运行良好:

HANDLE hFile = NULL;
HANDLE hMap = NULL;
char *stream_pos = NULL;
LPCTSTR input_file_name = _T("C:\\Users\\xxx\\Desktop\\Test\\test.txt");
DWORD bytes_left = 0;
hFile = CreateFile(input_file_name, GENERIC_ALL, 0, NULL, OPEN_EXISTING, 0, NULL);
bytes_left = GetFileSize(hFile, NULL);
hMap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, 0, NULL);
stream_pos = (char*)MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, 0);

该文件使用了只读属性,如果我们更改该属性,它会正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 2018-09-06
    • 1970-01-01
    • 2023-03-31
    • 2011-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多