0

I am trying to implement my own str concat function in C++, however one of my pointer variables is not being assigned to another pointer value. When I debug my program in CLION, it takes me to the line *p = *s2 and it assigns my *p variable to \0 instead of b. I am not sure where this error is coming from and can I also get help on how to debug this further in cLion?

I've tried changing the second char pointer in the parameter to const, and googling the answer for EXC_BAD_ACCESS (code=2, address=0x10c226edd).

 char * my_strcat(char* s1, char* s2)
{
    if(s1 == NULL || s2 == NULL) {
        return NULL;
    }
    char *p = s1;
    while (*p != '\0')
    {
        p++;
    }

    while (*s2 != '\0')
    {
        *p = *s2;
        p++;
        s2++;
    }
    *p = '\0';

    return s1;
}


  char *arr4 = "Taco";
  char *arr5 = "bean";
  std::cout << my_strcat(arr4,arr5) << std::endl;

Expected results are arr4 and arr5 to be concatenated, nothing is printed as the actual result.

Micheal O'Dwyer
  • 1,237
  • 1
  • 16
  • 26
Taner
  • 75
  • 7
  • 1
    `char *arr4 = "Taco";` should be an error in modern `c++` This needs to be `const char *arr4 = "Taco";` – drescherjm Jul 30 '19 at 17:59
  • 2
    related/dupe: https://stackoverflow.com/questions/11151626/bad-access-to-memory-using-strcat?rq=1 and https://stackoverflow.com/questions/4277927/exc-bad-access-when-trying-to-perform-a-strcat-function-in-c/4277956 – NathanOliver Jul 30 '19 at 17:59
  • 2
    `char *arr4 = "Taco"; char *arr5 = "bean";` -- You cannot concatenate onto a string literal. Second, even if these were not string literals, where is the room for `"bean"` to be concatenated onto `"Taco"`? C++ isn't going to clear space for you -- you have to ensure the space is there. – PaulMcKenzie Jul 30 '19 at 17:59
  • 1
    You'll need a `realloc` for `s1`. – csabinho Jul 30 '19 at 18:07
  • @NathanOliver There's also [this guy](https://stackoverflow.com/questions/15319859/how-to-concatenate-two-strings-in-c) too. –  Jul 30 '19 at 19:30
  • 2
    @OP -- Ironically, the error you're getting is supposed to happen if you were truly trying to mimic what `strcat` does. The `strcat` function does no checking if there is space or not, and if you can bypass the typesafety of C++, `strcat` will not care if what is being passed are string literals. The only thing your code does that is extra is the check for `NULL`, which `strcat` does not do. Your program will just explode on you with `strcat`, just as it is doing now. So in reality, your implementation (the behavior) is closer than you think it is to the standard version of `strcat`. – PaulMcKenzie Jul 30 '19 at 23:07

2 Answers2

1

The problem is that string literals are not modifiable. Nor do C strings automatically grow to fit the characters added to them. Try this instead.

char arr4[100] = "Taco";

Now arr4 really is an array, and it has enough room for you to add 95 more characters to your string.

I think you need to revise the difference between pointers and arrays, it's a very common topic for newbies to get confused about.

john
  • 85,011
  • 4
  • 57
  • 81
0

Ignoring the fact that you are invoking undefined behavior in your code by trying to modify a string literal for a second, you have another problem as well. Even if the char* part did work as expected, you are trying to write to unallocated data.

The reason is that p = s1. That means that p has the same amount of data allocated at that address as s1 does. Why is this important? Well, when you try to do this:

while (*s2 != '\0')
{
   *p = *s2;
   p++;
   s2++;
}

You are going into an unallocated space, since p now points to a place past where s1 has allocated. That's also undefined behavior, and likely to cause a crash.

You really need to fix both errors. for your program to work properly.

My suggestion would to be to use std::string instead:

std::string s1 = "Taco";
std::string s2 = "bean";

std::cout << s1+s2 << std::endl;

Far easier!

By the way, I recommend reading this question, specifically this answer to that question (if you're bent on concatenating 2 character arrays) for more information on what you're trying to do.