Program runs up to the last function then gives a terminate message

My last function is not working. This function appends a literal string to a C string. It checks if there is enough space in the C string to append the literal string to it. If there is not enough space, the C string length must be expanded to twice the size of the (literal string length + C string length). Then it can append the literal string to the C string. After I run the program and enter a text string, the first output statement is displayed but after that I keep getting an error of "terminate called after throwing an instance of std::bad_alloc" and the program crashes. All the other functions work before this last append function. Is there a way to fix the last function to work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    int main()
    {
    char* s1 = assign();			
    char* s2 = assign(" C++ ");	


    
    char* s3 = add(s1, s2);
    cout << "length of \"" << s3 << "\" is " << strlen(s3) << endl;

    
     append(s3, "programming language");   // This function doesn't work
     cout << "length of \"" << s3 << "\" is " << strlen(s3) << endl;
   
    return 0;
    }


    char* assign()
    {
    const int SIZE = 100;
    char temp[SIZE];
    int length;
    int twicelen;

    cout << "Enter a text string which will be used to append literal strings to it: ";
    cin.getline(temp, SIZE);
    length = strlen(temp);
    twicelen = length * 2;


    char* newCstring = new char[twicelen];
    strcpy(newCstring, temp);

    return newCstring;
    }



    char* assign(string str)
    {
    int len = strlen(str.c_str());
    int newlen = len * 2;
    char* newStr = new char[newlen];

    strcpy(newStr, str.c_str());;

    return newStr;
    }


    char* add(char* s1, char* s2)
    {
    strcat(s1, s2);
    return s1;
    }


    void append(char* s3, string literalStr)  // Every function before this works and this is where the program displays the terminate message and crashes.
    {


    if (sizeof(s3) < (strlen(s3) + strlen(literalStr.c_str()) + 1))
    {
        int expandLength = (strlen(s3) + strlen(literalStr.c_str())) * 2;
        char* s3 = new char[expandLength];
        strcat(s3, literalStr.c_str());

    }
    else
        strcat(s3, literalStr.c_str());

    }
    
char* s3
s3 is a pointer. You should pass another parameter to check if the string exceeds the memory you allocated to that pointer.

1
2
3
4
5
void append(char* s3, unsigned int s3Length,  unsigned int memorySize, std::string literalStr){
    if((s3Length + literalStr.length()) > memorySize){
        // TODO: Exceeding memory. Allocate a new memory block and copy/append content. Free old memory.
    }
}


Also your "char* add(...)" function isn't checking if you're exceeding memory or not.
Topic archived. No new replies allowed.