-2

I was trying to initialize an array via statement:

char a[]="abc";
cout<<a<<" "<<sizeof(a);

Output:

abc 4

Now, when I did this:

 strcpy(a,"abcdefgh");
 cout<<a<<" "<<sizeof(a);

Output:

abcdefgh 4

and then I got a error that m.exe has stopped working. (m is the name of my file)

I want to ask if each character takes 1 byte and size of array a is 4 byte then how could a string of size 9 byte got initialized to a and why did I get that error?

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
  • 1
    "then how could a string of size 9 byte got intialisedd to a" – seriously? It couldn't… your program even crashed… what kind of additional evidence you need that it's wrong? – The Paramagnetic Croissant Jul 28 '15 at 05:43
  • 1
    size of array is four chars, you are tying to copy a 10 char array of to it. Each c string had a null character at the end. '\0' – Ben Jul 28 '15 at 05:44
  • then why did i got that output? – Tarun Verma Jul 28 '15 at 05:45
  • 1
    @TarunVerma Undefined behaviour – Nishant Jul 28 '15 at 05:48
  • 2
    @TarunVerma you wonder how strcpy worked, right? Well, it tried to copy your new 9 bytes string using the pointer to the 4 byte sized array, because strcpy does not know actual size of your array, and tries to copy to a place which is not "yours". So, the behaviour is undefined. Even, you might not get any error, for your small program. This does not mean that strpcpy worked correctly. It just wrote to a memory that is not "yours". – Mert Mertce Jul 28 '15 at 05:50

4 Answers4

3

Your array is 4 bytes, your string is 9 bytes, you coped the string to the array and you got an error. What did you expect to happen?

C++ has a concept called 'undefined behaviour'. In many cases if your program breaks the rules of C++ then you get undefined behaviour, it means exactly what it says, anything can happen. That includes your program appearing to work, there's no guarantee that the program above would generate any error at all.

If you were expecting your program to give you some kind of helpful error message explaining what you did wrong, or if you were expecting your program to not compile because of the error then I'm afraid you are looking at the wrong programming language.

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

Your array is of 4 bytes, and you are trying to copy something to it which is of 9 bytes.

strcpy(a,"abcdefgh");

why won't it crash!!!!

how could a string of size 9 byte got intialisedd to a

This is "Undefined Behaviour" in c++.

Nishant
  • 1,635
  • 1
  • 11
  • 24
0

Array a is of 4 bytes, and 9 bytes are being copying to it resulting in Undefined Behavior.

Please note that all strings must end with \0 character. Also, no such character is available in your string and thus compiler goes on reading the string out of your array bounds.

Consider the following line:

strcpy(a,"abcdefgh");

This line copies the string into your array while overwriting the contents of the subsequent locations in memory.

However the sizeof() just returns the size of actual memory occupied by the array a which is 4 bytes("abc"+\0 character).

TryinHard
  • 4,078
  • 3
  • 28
  • 54
0

Functions like strcpy() come from C and they are very "low-level". They involve a lot of manual accounting for how-much-memory-is-allocated-where. It's quite easy to make mistakes with them. And you made one!

As others have said: if your point of confusion was "why didn't the compiler stop me from doing this", the answer is that you were using a method of handling strings with very little checking. The good news about C++ is that it offers that low-level ability when you need it, but also comes with nice classes in the standard library to help.

So for instance, there is std::string:

#include <string>
#include <iostream>

using namespace std;

int main() {
    string a = "abc";
    cout << a << a.length();
    a = "abcdefgh";
    cout << a << a.length();

    // Note: it's okay to leave off return in C++, for main() only!
}

That should give you:

abc 3
abcdefgh 8

Notice that even the issue of the null terminator has been handled for you, so you don't have to worry about it. You can append strings together using "+" and do many things you'd expect from other languages, yet with a lot more control over how and when copies are made (when you need it).

All this said: there are better ways to learn very early C++ concepts than asking on StackOverflow. That's likely to make people mad that you haven't made the effort to work through a good book or tutorial! So consider taking that initiative:

The Definitive C++ Book Guide and List

And if your interest is in learning C and sticking to really low-level stuff, I'd suggest staying in C. If you are writing new code and it's a mix of strlen() along with cout <<, that's probably not the best idea.

Community
  • 1
  • 1