0

我正在尝试像这样使用 strstr 函数:

const char * pInicio = strstr(origem, chave);       
if (pInicio != NULL) ...

与预期相反,当我进行“if”测试时,编译器会抛出访问位置冲突异常。每次我尝试从字符串中获取一些不存在的关键字时都会发生这种情况。

调用栈是:

    ntdll.dll!779315de()    
    [Os quadros abaixo podem estar incorretos e/ou ausentes, nenhum símbolo foi carregado para ntdll.dll]   
    ntdll.dll!779315de()    
    ntdll.dll!7792014e()    
>   msvcr100d.dll!_output_l(_iobuf * stream, const char * format, localeinfo_struct * plocinfo, char * argptr)  Linha 2361 + 0xa bytes  C++
    cccccccc()  

输出是:

Exceção de primeira chance em 0x0f8620a7 (msvcr100d.dll) no TestesApp.exe: 0xC0000005: Violação de acesso ao ler o local 0x00000001.
Exceção sem-tratamento em 0x779315de no TestesApp.exe: 0xC0000005: Violação de acesso ao ler o local 0x00000001.

在异常之前,变量值是:

  1. 查韦:0x00196f64“$ GNGSA”类型:字符*
  2. 原点:0x00196840 "$GPRMC,000651.082,V,,,,,,,191210,,,N*4F $GPGGA,000652.091,,,,,0,00,,,M,0.0,M,,0000*5F $ GPGSA,A,1,,,,,,,,,,,,,,,*1E" 类型:const char *
  3. pInicio:0x00000000 无效>类型:const char *

链接器命令行是:

/OUT:"C:\Users\Leandro\documents\visual studio 2010\Projects\TestesApp\Debug\TestesApp.exe" /INCREMENTAL /NOLOGO "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /MANIFEST /ManifestFile:"Debug\TestesApp.exe.intermediate.manifest" /ALLOWISOLATION /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"C:\Users\Leandro\documents\visual studio 2010\Projects\TestesApp\Debug\TestesApp.pdb" /SUBSYSTEM:CONSOLE /PGD:"C:\Users\Leandro\documents\visual studio 2010\Projects\TestesApp\Debug\TestesApp.pgd" /TLBID:1 /DYNAMICBASE /NXCOMPAT /MACHINE:X86 /ERRORREPORT:QUEUE 

c/c++ 命令行也是

/ZI /nologo /W3 /WX- /Od /Oy- /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm /EHsc /RTC1 /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fp"Debug\TestesApp.pch" /Fa"Debug\" /Fo"Debug\" /Fd"Debug\vc100.pdb" /Gd /analyze- /errorReport:queue 

在此之后,我尝试测试指针是否为空。这是我现在的问题,如何解决?

此致!

4

1 回答 1

2

无法复制您的错误。这是我尝试过的:

int main( int argc, char **argv )
{
   char * chave = "$GNGSA";
   const char * origem = "$GPRMC,000651.082,V,,,,,,,191210,,,N*4F $GPGGA,000652.091,,,,,0,00,,,M,0.0,M,,0000*5F $GPGSA,A,1,,,,,,,,,,,,,,,*1E";
   const char * pInicio = strstr(origem, chave);
   if (pInicio != NULL)
   {
      printf("found it\n");
   }
   return 0;
}

这在 Visual Studio 2010 中编译和运行没有错误。顺便说一句,它没有找到匹配项。我愿意接受有关如何调整它以创建错误的说明。

- 编辑 -

将编译器命令行设置为: /ZI /nologo /W3 /WX- /Od /Oy- /D "WIN32" /D "_D​​EBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm /EHsc / RTC1 /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fp"Debug\odt.pch" /Fa"Debug\" /Fo"Debug\" /Fd"Debug\vc100.pdb" /Gd /analyze- /错误报告:队列

链接器命令行:/OUT:"E:\code\odt\Debug\odt.exe" /INCREMENTAL /NOLOGO "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" “advapi32.lib” “shell32.lib” “ole32.lib” “oleaut32.lib” “uuid.lib” “odbc32.lib” “odbccp32.lib” /MANIFEST /ManifestFile:”Debug\odt.exe.intermediate.manifest " /ALLOWISOLATION /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG /PDB:"E:\code\odt\Debug\odt.pdb" /SUBSYSTEM:CONSOLE /PGD:"E:\code\ odt\Debug\odt.pgd" /TLBID:1 /DYNAMICBASE /NXCOMPAT /MACHINE:X86 /ERRORREPORT:QUEUE

即使两个命令行基本相同,我仍然没有收到错误。我不知道该说什么,对不起。

于 2013-03-26T20:27:44.903 回答