Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用strcat无法访问内存_C++_Std_String Concatenation_Cocos2d X - Fatal编程技术网

C++ 使用strcat无法访问内存

C++ 使用strcat无法访问内存,c++,std,string-concatenation,cocos2d-x,C++,Std,String Concatenation,Cocos2d X,我正在使用linux 我有一个函数,叫做like: PlayBackgroundIntroMusic((char *)"IntroMusic"); PlayBackgroundIntroMusic((char*)“IntroMusic”); 其功能是: void SoundManager:: PlayBackgroundIntroMusic( char * musicFile) { // Concatenate extension for each platform

我正在使用linux

我有一个函数,叫做like:

PlayBackgroundIntroMusic((char *)"IntroMusic"); PlayBackgroundIntroMusic((char*)“IntroMusic”); 其功能是:

void SoundManager:: PlayBackgroundIntroMusic( char * musicFile) { // Concatenate extension for each platform strcat (musicFile,audioExtension); CCLOG("musicFile: %c" musicFile); SimpleAudioEngine::sharedEngine()->playBackgroundMusic(std::string(CCFileUtils::fullPathFromRelativePath(musicFile)).c_str(), false); } 经理:: PlayBackgroundIntroMusic(char*musicFile) { //连接每个平台的扩展 strcat(音乐文件、音频扩展); CCLOG(“音乐文件:%c”音乐文件); SimpleAudioEngine::sharedEngine()->playBackgroundMusic(std::string(CCFileUtils::fullPathFromRelativePath(musicFile)).c_str(),false); } 但我在网上对内存的访问不好:

strcat (musicFile,audioExtension); strcat(音乐文件、音频扩展); audioExtension声明为:

#include using std::string; #include using std::cout; using std::cerr; using std::endl; /** * Declare sound extension for each platform * Android = ogg * iOS = caf * WIN32 = mp3 */ #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) static const char * audioExtension = ".wav"; #elif (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) static const char * audioExtension = ".caf"; #elif (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) static const char * audioExtension = ".ogg"; #endif #包括 使用std::string; #包括 使用std::cout;使用std::cerr;使用std::endl; /** *为每个平台声明声音扩展 *Android=ogg *iOS=caf *WIN32=mp3 */ #如果(CC_目标_平台==CC_平台_WIN32) 静态常量字符*audioExtension=“.wav”; #elif(CC_目标_平台==CC_平台_IOS) 静态常量char*audioExtension=“.caf”; #elif(CC_目标_平台==CC_平台_ANDROID) 静态常量char*audioExtension=“.ogg”; #恩迪夫 因此,我希望:

IntroMusic.caf on iOS IntroMusic.ogg on Android iOS上的IntroMusic.caf Android上的IntroMusic.ogg 发生了什么事

注:我已尝试:

char * musicFileWithExtension = strcat (musicFile,audioExtension); char*musicFileWithExtension=strcat(musicFile,audioExtension); 但它无论如何都不起作用

musicFile不是一个常数。如果文件名太长,我不想声明tempchar[80]以避免溢出


提前感谢。

字符串文本,例如
“IntroMusic”
属于
常量字符[N]
类型,可以隐式转换为
常量字符*
。通过语言设计中的错误,它也可以转换为 char */COD>,但是这种转换在C++中是正确的,因此警告。您需要使用数组(动态或静态分配),而不是字符串文字


或者更好地使用std::string查看strcat文档。它正在将目标字符串添加到源字符串。在您的例子中,源字符串是“musicFile”,所以它不应该是常量,应该有足够的长度

如果函数是这样调用的:

PlayBackgroundIntroMusic((char *)"IntroMusic");
然后musicFile==“IntroMusic”是常量,不能被覆盖。

首先,“IntroMusic”是常量

从常量值中删除常量并对其进行修改是未定义的行为。任何事情都有可能发生,你很幸运能马上坠机

此外,为“IntroMusic”分配的内存正好是字符的10个字节加上一个分隔符
\0
,因此总共11个字节。时期现在,除了试图强制修改
const
值之外,您甚至还写入未分配的内存(至少,不是您为写入而分配的):您只需尝试在“intromise”之后将与平台相关的文件扩展名写入内存

您有责任为您的操作提供足够大的缓冲区


简单解决方案(既然你给问题添加了标签
c++
,而不是
c
:使用
std::string

你的编译器试图告诉你一些事情。然后为了关闭它,你添加了一个cast。也许你应该听它。它在没有cast的情况下工作吗?练习:找出原因。去掉(char*)抛出警告,发送,转换,所以我做演员。发生了什么?是不是很坏地使用字符串?你试图连接一些东西而不存储它。AFAIR普通字符串文字有“N conchar数组”类型,从C++ STD。