-2

I declared char linkk[23];

I printed it using

for(int i=0;i<23;i++)
    cout<<linkk[i];

I didn't notice anything wrong but when I opened the link a !v popped up after the 23 elements.

char arr[]="start C:\\Users\\user\\Downloads\\chrome-win32\\chrome-win32\\chrome.exe ";
strcat(arr, linkk);
string openL=arr;
system(openL.c_str());

So before joining the arrays I removed the the two elements from linkk.

linkk[24]='\0';
linkk[23]='\0';

It worked but I don't understand where the !v came from.

PS: I am a noob.

  • 1
    1) What do you mean by `!v`? 2) Since `linkk` is declared as `char linkk[23];`, `linkk[24]='\0'; linkk[23]='\0';` is UB (undefined behavior), due to out-of-range array access. – Algirdas Preidžius Apr 03 '18 at 17:17
  • Thats the extra elements at `linkk[23]` and `linkk[24]` – salman_sali Apr 03 '18 at 17:19
  • 1
    Not all errors in c++ are required to crash. – François Andrieux Apr 03 '18 at 17:21
  • 2
    you reserved space for 23 chars,which you can reference with indexes from 0 to 22. with `linkk[23]` and `linkk[24]` you are accessing some nomanland memory: that's undefined behaviour, don't try to understand the result, it's undefined. – Gian Paolo Apr 03 '18 at 17:22
  • 2
    You cannot use strcat() on that `arr`, it is not big enough. The buffer overflow will arbitrarily corrupt memory and the result is entirely unpredictable. The linkk access is out of bounds as well, same problem. Favor std::string instead of char[]. – Hans Passant Apr 03 '18 at 17:25
  • 1
    C++ compilers are not required to warn you when you break the language rules. They just get to assume that you won't do that and if you do anyway they are allowed to do whatever they please. Your code has undefined behaviour when accessing beyond the array - it is broken - you can't assume *anything* about its behaviour in that case. – Jesper Juhl Apr 03 '18 at 17:26
  • Either use `std::string` instead of `char[]`, or change the tag from `c++` to `c`. – John Apr 03 '18 at 18:13

4 Answers4

3

There are several issues here. However, to answer your specific question, you need to show how you initialized linkk, which I do not see in your question.

A big problem is using strcat to concatenate linkk onto arr. The way arr is declared, it will only be large enough to hold the literal string you initialized it with. Thus, the strcat is going to overflow that buffer.

Legacy C functions that do not take a length specifier are almost always dangerous and should never be used in modern programming. These include strcat, strcpy, and others. Because you are using C++, and convert everything to a std::string in the end anyway, I recommend working with std::string all along and avoiding raw char arrays:

std::string linkk;
// Initialize linkk however you want. Again, this wasn't specified in your question.

std::string program("start C:\\Users\\user\\Downloads\\chrome-
win32\\chrome-win32\\chrome.exe ");
std::string openL = program + linkk;
system(openL.c_str());
1

Most likely, linkk wasn't properly null terminated. You didn't notice this when you printed it out since you printed it one char at a time.

The other problem with this code is that while using strcat you are overflowing the destination buffer. I recommend using std::string for all of these variables.

Segfault
  • 7,696
  • 2
  • 32
  • 53
1

To quote from the documentation:

char *strcat( char *dest, const char *src );
The behavior is undefined if the destination array is not large enough for the contents of both src and dest and the terminating null character.

Your destination array is not large enough to hold both strings. This means strcat has undefined behavior (most likely writing into memory that is in use by something else, but just about anything could happen).

The spurious !v is one of the possible consequences - but again, anything could happen. Either specify a large enough arr or go the C++ route of using std::string.

Max Langhof
  • 22,875
  • 5
  • 39
  • 70
0

In C, you have to make sure your destination array is large enough to hold both its current string plus the additional string you are concatenating. Since you're obviously using C++, declare your strings the C++ way:

const std::string yourFirstString ="something something chrome.exe ";
const std::string link = "<link here>";

Then, just add the two strings like so:

const std::string strOpenLink = yourFirstString + link;

You shouldn't mix C and C++ strings because it's both confusing and more work, and therefore more prone to errors.


As for the random string you saw, judging from your code, the linkkk array was not initialized so you got random values which were present in that memory location before the array was allocated on the stack. You can either use memset to zero it out, or you can initialize it right away.