[SOLVED] C++ - char Array Holding More Letters It Should
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
When I enter more than fifteen letters, it takes it without complaint. The strlen() function returns the correct number of letters. This works out to at least forty letters. I entered 120 letters and it still worked, but it threw a segmentation fault.
// strings.cpp -- storing strings in an array
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
const int ArSize = 15;
char name1[ArSize]; // empty array
char name2[ArSize] = "C++owboy"; // initialized array
cout << "Howdy! My name is " << name2;
cout << "! What's your name?\n";
cin >> name1;
cout << "Well, " << name1 << ", your name has ";
cout << strlen(name1) << " letters, and is stored\n";
cout << "in an array of " << sizeof(name1) << " bytes.\n";
cout << "Your initial is " << name1[0] << ".\n";
name2[3] = '\0'; // set to null character
cout << "Here are the first three letters of my name: ";
cout << name2 << endl;
return 0;
}
Results from entering 120 characters:
Code:
[dknapp@opensourcegort C++]$ ./a.out
Howdy! My name is C++owboy! What's your name?
kkklkljjkkjkjjjkkjknmkjmkjnmkkjjhjhggfdfhghhtdrdgmhgjyftdrsytfkjnlkjoiuyfkjbnkjlihyiuyfjblknihiuguyfvkjblkhouiguyfkjbljh
Well, kkklkljjkkjkjjjkkjknmkjmkjnmkkjjhjhggfdfhghhtdrdgmhgjyftdrsytfkjnlkjoiuyfkjbnkjlihyiuyfjblknihiuguyfvkjblkhouiguyfkjbljh, your name has 120 letters, and is stored
in an array of 15 bytes.
Your initial is k.
Here are the first three letters of my name: C++
Segmentation fault (core dumped)
I'm on a 64bit machine with Fedora 23. I'm using the C++ implementation that's installed by default.
Last edited by killingthemonkey; 01-03-2016 at 05:19 PM.
In C and C++ it is up to the programmer to assure that the program never writes past the end of the array.
If you DO ask it to write past the end of the array it will do as it has been asked and simply overwrite whatever is there. The fact that it results in a segmentation fault at 120 characters in your specific case is not of any great significance - it is just corrupted memory that causes a crash.
Your code must be written in such a way that it prevents writing past the end of the array.
So, I'm thinking cin.getline() would be better than a bare cin.
Quote:
Originally Posted by astrogeek
In C and C++ it is up to the programmer to assure that the program never writes past the end of the array.
If you DO ask it to write past the end of the array it will do as it has been asked and simply overwrite whatever is there. The fact that it results in a segmentation fault at 120 characters in your specific case is not of any great significance - it is just corrupted memory that causes a crash.
Your code must be written in such a way that it prevents writing past the end of the array.
When you specify "an array," without using any sort of container-class, then what you get is a fixed-size block of storage. It is entirely your responsibility to see to it that your code never accesses any location outside of that block. (C/C++ doesnot check the value of array indexes!)
On the other hand, when you simply use the string type, the implementation of that type provides you with a variable-sized storage area, which grows or contracts as needed and which conceals from you (yay!) all of the associated "messy details." (Furthermore, the type "knows" that it exists to manage ... "strings.")
Whenever possible, therefore: (a) "use C++ ... that's what it's there for." And, (b) use the "smart" classes and data-types which C++ provides, such as strings and various forms of "containers."
When you do this, you silently leverage the "smart work" of "many clever (other!) people," who tackled many otherwise-thorny problems on your behalf, specifically so that you don't have to. The resulting code is extremely efficient, and generally backwards-compatible with "C."
(Obviously, there are certain contexts, such as "writing kernel code," where C++ cannot be used.)
"Do not do a thing already done." "Do not ride the pony bareback when you have equally-convenient access to a nice saddle."
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.