I'm playing with all the cstring and string functions, getting familliar with how they work. Below I have a function call to strcata, and all I'm doing is declaring a string within the function, I'm not even using it with anything other than cout. Everytime I run the code it crashes, can anyone tell me why the following code crashing?

    Practice for cstring parsing

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

// ****** Prototypes *********************************************
// void strcpy();
// void strncpy();
// void strcat(char []);
void strcata();

// ****** MAIN ***************************************************
int main()
    // string example3 = "This is example 3";
    // strcpy();
    // strncpy();
    // strcat(example3.c_str());

    return 0;
// ****** FUNCTION: strcat ***************************************
// char* strcat(char* destination, const char* source);
// Appends the source to the end of the destination
// ***************************************************************
void strcata()
    char buffer[80] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    char example1[]  = "This is example 1";
    char example2[]  = "This is example 2";
    string example3 = "This is example 3";
   cout << example3 << endl;
   // strcat(example1, example3);
    strcat(example1, example2);

    cout << example1 << endl;
    cout << strlen(example1) << endl;
    cout << strlen(example2) << endl;

    strcat(buffer, example1);
    cout << buffer << endl;
    cout << strlen(buffer) << endl;
    cout << strlen(example1) << endl;

Using the definition that any technology sufficiently advanced is indistinguishable from magic, C style strings are far less magical than C++ strings.
With a C++ string, the sky (or rather, your protected allocated process memory space) is the limit. Need a bigger string? That's A-OK. We'll just allocate more memory for it.

A C string, like your buffer, example1 and example2, are arrays of characters that fit in their designated spaces. When they overrun the amount of room in their designated spaces, bad things happen. With example1 and example2 , the size isn't defined in code, so the compiler assumes you're fine with it allocating ONLY as much space is needed to hold those particular strings plus their respective trailing zeros (all C strings end with a 0 byte). Your buffer string is 78 characters plus a zero making for a required size of 79. You can strcat a string that's 1 letter long to it, and that's all.

So what if you want to make it bigger? Well, you have to pre-plan and give it a bigger size e.g. buffer[100] or example1[256].

Or you could rewrite to do dynamic allocation and resizing, but don't worry about that just yet.

EDIT: This is a crappy explanation, and I'm not sure I got the point across. Please ask questions if this is unclear.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.