I have following function:
char * strAlloc(string str) {
char * chArr = new char[str.size()];
for (size_t i = 0; i < str.size(); i++) {
chArr[i] = str[i];
}
return chArr;
}
if i do a break after char * chArr = new char[str.size()]; the debugger says following:
chArr 0x00c38cf8 "ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍýýýý««««««««þîþîþ"
and if i do a break after the for iteration i get this:
chArr 0x00c38cf8 "***************ýýýý««««««««þîþîþ"
so what i’m doing wrong?
edit:
size_t const gSize = 15; typedef char *
TMaze [gSize];
Maze[0] = strAlloc ("***************");
in type TMaze i need the pointer of the char arrays tried also str.size()+1 ,
same behaviour
edit2:
char * strAlloc(string const & str) {
char * chArr = new char[str.size()+1];
strcpy(chArr, str.c_str());
return chArr;
}
did it!
You’re not null terminating. Why not just use
strcpy(chArr, str.c_str());?And you’ll need to add one to your allocation, too:
char* chArr = new char[str.size() + 1];