I’m using linux.
I have a function called like:
PlayBackgroundIntroMusic((char *)"IntroMusic");
The functions is:
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);
}
But i having a bad access to memory on line:
strcat (musicFile,audioExtension);
audioExtension is declarated:
#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
So, i expected to have:
IntroMusic.caf on iOS IntroMusic.ogg on Android
What’s is happening?
NOTE: I have tried to:
char * musicFileWithExtension = strcat (musicFile,audioExtension);
But it didn’t work anyway.
musicFile is not a constant. I don’t want to declarate a tempchar[80] to avoid overflow if the name of the file is too long like Example cc reference
Thanks in advance.
String literals, such as
"IntroMusic"are of typeconst char[N]which is implicitly convertible toconst char *. Through a mistake in language design, it is also convertible tochar*, but that conversion is rightfully deprecated in C++, hence the warning. You need to use an array (dynamically or statically allocated), not a string literal.Or better yet, use
std::string.