简体   繁体   English

C/C++ 中带有“cout”的函数返回的 char 指针和 char 指针的奇怪行为

[英]Strange behavior with char pointer and char pointer returned by fonction in C/C++ with “cout”

I have a strange behavior with a char pointer initialized by the value of a return function and with the cout.我有一个奇怪的行为,char 指针由返回 function 的值和 cout 初始化。

All my code is for an Arduino application, this is why I use char pointer, char array and string.h.我所有的代码都是针对 Arduino 应用程序的,这就是我使用 char 指针、char 数组和 string.h 的原因。 I created a class named FrameManager , with a function getDataFromFrame to extract data from a string (in fact a char array).我创建了一个名为FrameManager的 class ,并使用 function getDataFromFrame从字符串(实际上是一个字符数组)中提取数据。 See above:看上面:

`char * FrameManager::getDataFromFrame ( const char frame[], char key[] )
{
  char *pValue = nullptr;
  int frameLength = strlen ( frame );
  int previousStartIndex = 0;
  for ( int i=0; i<frameLength; i++ ) {
    char c = frame[i];
    if ( c == ',' ) {
        int buffSize = i-previousStartIndex+1;
        char subbuff[buffSize];
        memset ( subbuff, 0, buffSize ); //clear buffer
        memcpy ( subbuff, &frame[previousStartIndex], i-previousStartIndex );
        subbuff[buffSize]='\0';
        previousStartIndex = i+1;

        int buffLength = strlen ( subbuff );
        const char *ptr = strchr ( subbuff, ':' );
        if ( ptr ) {
            int index = ptr-subbuff;
            char buffKey[index+1];
            memset ( buffKey, 0, index+1 );
            memcpy ( buffKey, &subbuff[0], index );
            buffKey[index+1]='\0';
            char buffValue[buffLength-index];
            memset ( buffValue, 0, buffLength-index );
            memcpy ( buffValue, &subbuff[index+1], buffLength-index );
            buffValue[buffLength-index]='\0';

            if ( strcmp ( key,buffKey ) == 0 ) {
                pValue = &buffValue[0];
                break;
            }
        }
    } else if ( i+1 == frameLength ) {
        int buffSize = i-previousStartIndex+1;
        char subbuff[buffSize];
        memcpy ( subbuff, &frame[previousStartIndex], frameLength-1 );
        subbuff[buffSize]='\0';
        int buffLength = strlen ( subbuff );
        const char *ptr = strchr ( subbuff, ':' );
        if ( ptr ) {
            int index = ptr-subbuff;
            char buffKey[index+1];
            memset ( buffKey, 0, index+1 );
            memcpy ( buffKey, &subbuff[0], index );
            buffKey[index+1]='\0';
            char buffValue[buffLength-index];
            memset ( buffValue, 0, buffLength-index );
            memcpy ( buffValue, &subbuff[index+1], buffLength-index );
            buffValue[buffLength-index]='\0';

            if ( strcmp ( key,buffKey ) == 0 ) {
                pValue = &buffValue[0];
                break;
            }
        }
    }
}

return pValue;

}` }`

In the main(), I created juste a little code to test the returned value:在 main() 中,我创建了一些代码来测试返回值:

int main(int argc, char **argv) {

  const char frame[] = "DEVICE:ARM,FUNC:MOVE_F,PARAM:12,SERVO_S:1";
  FrameManager frameManager;
  char key[] = "DEVICE";
  char *value; 
  value = frameManager.getDataFromFrame(frame, &key[0]);
  cout << "Retrieved value: " << value << endl;
  cout << "Retrieved value: " << frameManager.getDataFromFrame(frame, &key[0]) << endl;

  printf("%s",value);

  return 0;
}

and here the result:结果如下:

  • Retrieved value: y%R检索到的值:y%R
  • Retrieved value: ARM检索值:ARM
  • ARM ARM

The first "cout" doesn't display the expected value.第一个“cout”不显示预期值。 The second "cout" display the expected value and the printf too.第二个“cout”也显示预期值和 printf。

I don't understand what is the problem with the first "cout".我不明白第一个“cout”有什么问题。

Thanks谢谢

Jocelyn乔斯林

pValue points into local arrays, which get out of scope. pValue指向从 scope 出来的本地 arrays。 That's undefined behavior.那是未定义的行为。 It might work, but your program might also crash, return wrong values (that's what you experience), corrupt your data or do any other arbitrary action.它可能有效,但您的程序也可能崩溃、返回错误值(这就是您所经历的)、损坏您的数据或执行任何其他任意操作。

Given that you're already using C++, consider using std::string as a result instead or point into the original frame (if possible).鉴于您已经在使用 C++,请考虑使用std::string作为结果,或者指向原始frame (如果可能)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM