简体   繁体   中英

Converting integer to a character pointer in c++ and then appending it to another chacter pointer

I have the following code for converting the integer(a score) into the character and then appending it with the player's name (player1). It gets displayed after that. It is a part of a bigger project :

    #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;
}

It compiles correctly but when I run it , it shows the following error :

*** 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

What is the reason for this? How can it be rectified. I have already put the null termination character in the convertIntTochar function.

Many problems here...

  1. convertIntTochar works for 2 digit numbers only. No checks are done.
  2. char t[3] defined in convertIntTochar is a local variable, you can't return a pointer to it and use this pointer outside convertIntTochar.
  3. strcat(str1 , str2); appends to an array that is already full (str11) so you overwrite the stack.

Just switch to std::strings, it will be simpler.

   char str11[] = "Player1: ";

This is the problem. There is not enough room for string concatenation. Try this:

   char str11[100] = "Player1: ";

Better yet, use std::string instead of C-like char* . Smallest possible changes that fix string problems are these (because there exist using namespace std then std:: part in std::string can be left out, but I just prefer to leave it in):

#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;
}

Use std::strings and std::ostringstreams, it's much simpler.

#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;

and if you want a read-only C string, use player_score.c_str(), that returns a const char *

To convert int to char * see if you can use itoa with your compiler.
If it is not supported you can find its implemenation to do what you want.
That is if you have to do it using C-strings

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM