Char 数组末尾的额外字符 (char *Result = new char) 8OI

Evr*_*urk 0 c++ directx visual-c++ directx-9

我有一个 C++ 函数,它将 LPSTR 类型变量拆分为一个字符数组 (char*) 示例:

this->XMeshTexturePath = FindTexturePath(XMeshTexturePath,d3dxMaterials[i].pTextureFilename);
   //the value of XMeshTexturePath is: Models\\Textures\\
   //the value of d3dxMaterials[i].pTextureFilename is: BlaBlaBla\\BlaBla\\Cyrex.x
   //The Result(XMeshTexturePath) should be like this:"Models\\Textures\\Cyrex.x"
Run Code Online (Sandbox Code Playgroud)

这是我正在尝试编写的功能:

int FindTextLength(char* Text){
    int length
Run Code Online (Sandbox Code Playgroud)

h=0; for(int i=0;i

char* FindTexturePath( char* TexturePath ,LPSTR FileNameToCombine){
    int FileLength=0;
    int PathAndFileLength=0;
    char *FileName = new char;
    char *TexPathAndName = new char;

    strcpy(TexPathAndName, FileNameToCombine);
    PathAndFileLength = FindTextLength(TexPathAndName);

    for(int i=0; i<PathAndFileLength; i++){
        if( TexPathAndName[i] != NULL){
            if(TexPathAndName[i] != '\\'){
                FileName[FileLength] = TexPathAndName[i];
                FileLength++;
            }
            else 
                FileLength = 0 ;
        }else break;
    }

    int PathLength = FindTextLength(TexturePath);
    char *Result = new char;
//==============>> // I also tryed this:char *Result = new char[PathLength+FileLength];
//==============>> //                   char *Result = new char();

    for(int i=0; i<PathLength; i++){
        if( TexturePath[0] != NULL){
            Result[i] = TexturePath[i];
        }
        else break;
    }

    for(int i=0; i<FileLength; i++){
        if( FileName[0] != NULL){
            Result[PathLength + i] = FileName[i];
        }
        else break;
    }

    return **Result**; // The Problem is here It should be like this:
                       // "Models\\Textures\\Cyrex.x"
                       // But I'm taking one of these as result:
                       //    "Models\\Textures\\Cyrex.x{"
                       //    "Models\\Textures\\Cyrex.xu"
                       //    "Models\\Textures\\Cyrex.xY"
                       //    The last character is random... 8O(
}
Run Code Online (Sandbox Code Playgroud)

实际上它并没有那么糟糕。问题是,当我声明一个字符数组(char *Result = new char;)时,它不知道长度是多少,我在最终结果(结果)的末尾添加了一个额外的字符,我真的被困在这里如果您有任何想法或建议,请告诉我。感谢您的任何建议和回复。

解决方案在函数的末尾添加了这个:

            Result[i] = TexturePath[i];
        }
        else break;
    }

    for(int i=0; i<FileLength; i++){
        if( FileName[0] != NULL){
            Result[PathLength + i] = FileName[i];
        }
        else break;
    }
    Result[PathLength+FileLength] = '\0' ;  // This part is resloving the problem.
                                            // **Thanks for helps**.
    return Result;
}
Run Code Online (Sandbox Code Playgroud)

Mah*_*esh 5

char *Result = new char[PathLength+FileLength];
Run Code Online (Sandbox Code Playgroud)

Result指向的数据应以终止字符结尾\0。或者在返回由Result. 所以,

Result[PathLength+FileLength-1] = '\0' ;
Run Code Online (Sandbox Code Playgroud)

确保永远不会溢出缓冲区,或者更好的选择是使用std::string.

  • 不需要PathLenght+FileLenght+1吗?对于空终止符? (2认同)