2010-12-31 3 views
1

정수 (점수)를 문자로 변환 한 다음 플레이어의 이름 (player1)을 추가하는 코드는 다음과 같습니다. 그 후에 표시됩니다. 그것은 더 큰 프로젝트의 일부입니다정수를 C++의 문자 포인터로 변환하고 다른 문자 포인터에 추가

#include <iostream> 
#include <string.h> 
using namespace std; 

char* convertIntTochar(int number) 
{ 
    char t[3]; 
    t[0] = 0; 
    t[1] = 0; 
    t[2] = '\0'; 

    int i = 0; 
    for(; number != 0; i++) 
    { 
     t[i] = ((number%10) + 48); 
     number/=10; 
    } 

    if(i == 2) 
    { 
     char temp = t[0]; 
     t[0] = t[1]; 
     t[1] = temp; 
    } 
    else 
     t[i] = '\0'; 
    char *ans = t; 
    return ans; 
} 

int main() 
{ 
    char str11[] = "Player1: "; 
    char *str1 = str11; 
    char *str2 = convertIntTochar(11); 
    strcat(str1 , str2); 

    while(*str1) 
    { 
     cout<<*(str1++); 
    } 

    return 0; 
} 

제대로 컴파일하지만 난 그것을 실행하면 다음과 같은 오류 보여줍니다

*** stack smashing detected ***: ./a.out terminated 
======= Backtrace: ========= 
/lib/tls/i686/cmov/libc.so.6(__fortify_fail+0x50)[0x9b3390] 
/lib/tls/i686/cmov/libc.so.6(+0xe233a)[0x9b333a] 
./a.out[0x80487ff] 
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0x8e7bd6] 
./a.out[0x8048621] 
======= Memory map: ======== 
00110000-00134000 r-xp 00000000 08:06 2887608 /lib/tls/i686/cmov/libm-2.11.1.so 
00134000-00135000 r--p 00023000 08:06 2887608 /lib/tls/i686/cmov/libm-2.11.1.so 
00135000-00136000 rw-p 00024000 08:06 2887608 /lib/tls/i686/cmov/libm-2.11.1.so 
004b9000-004d4000 r-xp 00000000 08:06 2887597 /lib/ld-2.11.1.so 
004d4000-004d5000 r--p 0001a000 08:06 2887597 /lib/ld-2.11.1.so 
004d5000-004d6000 rw-p 0001b000 08:06 2887597 /lib/ld-2.11.1.so 
0077d000-00866000 r-xp 00000000 08:06 2756275 /usr/lib/libstdc++.so.6.0.13 
00866000-00867000 ---p 000e9000 08:06 2756275 /usr/lib/libstdc++.so.6.0.13 
00867000-0086b000 r--p 000e9000 08:06 2756275 /usr/lib/libstdc++.so.6.0.13 
0086b000-0086c000 rw-p 000ed000 08:06 2756275 /usr/lib/libstdc++.so.6.0.13 
0086c000-00873000 rw-p 00000000 00:00 0 
008d1000-00a24000 r-xp 00000000 08:06 2887604 /lib/tls/i686/cmov/libc-2.11.1.so 
00a24000-00a25000 ---p 00153000 08:06 2887604 /lib/tls/i686/cmov/libc-2.11.1.so 
00a25000-00a27000 r--p 00153000 08:06 2887604 /lib/tls/i686/cmov/libc-2.11.1.so 
00a27000-00a28000 rw-p 00155000 08:06 2887604 /lib/tls/i686/cmov/libc-2.11.1.so 
00a28000-00a2b000 rw-p 00000000 00:00 0 
00a3b000-00a58000 r-xp 00000000 08:06 2883667 /lib/libgcc_s.so.1 
00a58000-00a59000 r--p 0001c000 08:06 2883667 /lib/libgcc_s.so.1 
00a59000-00a5a000 rw-p 0001d000 08:06 2883667 /lib/libgcc_s.so.1 
00b74000-00b75000 r-xp 00000000 00:00 0   [vdso] 
08048000-08049000 r-xp 00000000 08:06 4719693 /home/dhruv/Desktop/a.out 
08049000-0804a000 r--p 00000000 08:06 4719693 /home/dhruv/Desktop/a.out 
0804a000-0804b000 rw-p 00001000 08:06 4719693 /home/dhruv/Desktop/a.out 
08b67000-08b88000 rw-p 00000000 00:00 0   [heap] 
b77f7000-b77f9000 rw-p 00000000 00:00 0 
b780d000-b7810000 rw-p 00000000 00:00 0 
bfd2a000-bfd3f000 rw-p 00000000 00:00 0   [stack] 
Player1: "�ӿ�XMAborted 

그 이유는 무엇입니까? 그것을 어떻게 교정 할 수 있습니까? convertIntTochar 함수에 null 종료 문자를 이미 넣었습니다.

답변

2
char str11[] = "Player1: "; 

이것은 문제입니다. 문자열 연결을위한 공간이 충분하지 않습니다. 이 시도 :

char str11[100] = "Player1: "; 

더 나은 아직, C와 같은 char* 대신 std::string를 사용합니다. (using namespace std 다음 std::string에서 std:: 부분은 생략 할 수 있습니다이 존재하기 때문에,하지만 난 그냥에서 떠날 것을 선호) 문자열 문제를 해결하는 가장 작은 수의 변화는 다음과 같습니다

#include <iostream> 
#include <string> // instead of <string.h> 
using namespace std; 

std::string convertIntTochar(int number) 
{ 
    ... 
} 

int main() 
{ 
    std::string str1 = "Player1: "; 
    std::string str2 = convertIntTochar(11); 
    str1 += str2; 

    cout << str1; 

    // Or even more effective, just one line of code: 
    cout << "Player1: " << convertIntTochar(11); 

    return 0; 
} 
+0

당신이 표준 : : 문자열에 대해 말해 줄 수? 나는 언어에 다소 익숙하다 – higherDefender

+0

'str11 [100]'? 3 개의 문자 만 추가한다 !!! – Cratylus

+0

저렴한 플레이 이유. 이것들이 어디에서 왔는지 많은 바이트가 있습니다. – Dialecticus

6

많은 문제가 여기에 ...

  1. convertIntTochar는 2 개 자리 숫자 작동합니다. 확인은 수행되지 않습니다.
  2. char t[3]에 정의 된 convertIntTochar는 로컬 변수이므로이 포인터를 convertIntTochar 외부로 사용할 수 없습니다.
  3. strcat(str1 , str2);은 이미 가득 찼던 배열 (str11)에 추가하여 스택을 덮어 씁니다.

std :: strings로 전환하면 더 간단 해집니다.

0

char *에 INT로 변환하기 위해 당신이 할 수 있는지 컴파일러에 itoa을 사용하십시오.
지원되지 않는 경우 구현을 통해 원하는대로 할 수 있습니다.
C- 문자열을 사용하여 수행해야하는 경우

1

std :: strings 및 std :: ostringstreams를 사용하면 훨씬 간단합니다.

#include <sstream> 
#include <iostream> 
std::ostringstream player_score_stream; 
player_score_stream << "Player1: " << score_as_an_integer; 
std::string player_score(player_score_stream.str()); 
std::cout << player_score; 

와 읽기 전용 C 문자열을 원하는 경우 CONST 문자 *를 반환 player_score.c_str()를 사용