C++ array of fixed size getting garbage values

I was trying to rotate a string circularly by 1 shift. Here's the simple code:

#include <bits/stdc++.h>
using namespace std;

int main() {
  string s = "1010";
  char a[s.length()];
  for (int i = 0; i < s.length() - 1; i++) {
    a[i] = s[i+1];
  a[s.length()-1] = s[0];
  std::cout << a << '\n';
  std::cout << strlen(a) << '\n';

When I run this code, I get the following output:

aditya@aditya-Inspiron-3558:~/miscCodes$ ./a.out 

How's my array's length is changing? And due to garbage values?

Variable Length Arrays (VLA) are not part of Standard C++. Read more in Why aren't variable-length arrays part of the C++ standard?

But let's say, that you use a compiler extension that allows them, then the problem is that you use a C function here:

std::cout << strlen(a) << '\n';

which expects a C string, which means a NULL-terminated string.

That means that you should make your array big enough to hold the NULL termination character, like this:

char a[s.length() + 1];

since string::length() will return 4 for the string "1010". That means that the C string should be this: "1010\0", i.e. the actual string, appended with the NULL terminating character. As a result, you would need an array of size 5 to store that string.

An easy fix would be:

char a[s.length() + 1] = {0};

which will NULL initialize every cell of the array. Then you will overwrite every cell of it with characters, except from the last cell, specially reserved for the NULL terminator.

Another approach would be to only assign the NULL terminator to the last cell of your string, like a[s.length()] = '\0';. Notice how s.length() now is the index to the last element of your array.

Standard C string functions (like strlen()) depend on the NULL termination character to mark the end of the string. In the absence of that important character, they do not know when to stop, thus accessing memory beyond from the point they are meant to visit.

That invokes Undefined Behavior (UB), which, in your computer, is accessing memory with garbage values.

C Programming Tutorial for Beginners, Learn C the Hard Way: Practical Exercises on the Computational Subjects You Keep Avoiding (Like C) C or c is the third letter in the English and ISO basic Latin alphabets.Its name in English is cee (pronounced / ˈ s iː /), plural cees.

First, your code is not valid C++, as you are using Variable Length Arrays. Use an std::vector instead:

std::vector<char> a(s.length() + 1, 0); // Adding +1 to add space for the 0-terminated string

for (int i = 0; i < a.size() - 2; i++) { // Because of the null terminator and the first offset
  a[i] = s[i+1];
a[a.size()-2] = s[0];

Then after output using data:

std::cout << a.data() << '\n';
std::cout << a.size() << '\n'; // Will give you +1 because we give the size of the container and not thew size of the string

"C" Programming Language: Brian Kernighan, This course will give you a full introduction into all of the core concepts in the C programming Duration: 3:46:13 Posted: Aug 15, 2018 Ç or ç is a Latin script letter, used in the Albanian, Azerbaijani, Manx, Tatar, Turkish, Turkmen, Kurdish, Zazaki, and Romance alphabets. Romance languages that use this letter include Catalan, French, Friulian, Ligurian, Occitan, and Portuguese as a variant of the letter C. It is also occasionally used in Crimean Tatar and in Tajik to represent the /d͡ʒ/ sound. It is often retained in the spelling of loanwords from any of these languages in English, Basque, Dutch, Spanish and other

You've already gotten excellent answers so this is just an add-on.

#include <bits/stdc++.h> is not a standard header file and it will make you lazy since it includes all that you probably need and a lot more. Include only the headers you need, especially if you are going to do using namespace std;, which is also a bad idea.

Apart from that, to solve the problem of rotating elements in your string, take a look at std::rotate. It can rotate not only strings, but std::vectors etc. Example doing a left rotate one step:

#include <iostream>
#include <algorithm> // std::rotate

int main() {
    std::string s = "1234";
    std::string cpy = s;

    //          first elem   new first elem   last elem
    std::rotate(cpy.begin(), cpy.begin() + 1, cpy.end());
    std::cout << s.size() << " " << s << "\n";
    std::cout << cpy.size() << " " << cpy << "\n";


4 1234
4 2341

How easy is it to learn C?, "C" is one of the most widely used programming languages of all time. Prof Brian Kernighan Duration: 8:26 Posted: Aug 18, 2015 Stock analysis for Citigroup Inc (C:New York) including stock price, stock chart, company news, key statistics, fundamentals and company profile.

Instead of this char a[s.length()];

Do this char a[s.length() + 1]

How is C programming language still used today?, - and extremely flexible. So flexible, in fact, that as you use the language, you probably go through many different programming styles as you work out how to use the language to produce good, maintainable, reliable code. C-- (pronounced cee minus minus) is a C -like programming language. Its creators, functional programming researchers Simon Peyton Jones and Norman Ramsey, designed it to be generated mainly by compilers for very high-level languages rather than written by human programmers.

Learn C, is best choice when you start programming as it is most basic. This is a list of operators in the C and C++ programming languages.All the operators listed exist in C++; the fourth column "Included in C", states whether an operator is also present in C. Note that C does not support operator overloading.

C Tutorial, learn-c.org is a free interactive C tutorial for people who want to learn C, fast. As well as C and Simula's influences, other languages also influenced this new language, including ALGOL 68, Ada, CLU and ML . Initially, Stroustrup's "C with Classes" added features to the C compiler, Cpre, including classes, derived classes, strong typing, inlining and default arguments.

C Language - Overview, C programming is a general-purpose, procedural, imperative computer programming language developed in 1972 by Dennis M. Ritchie at the Bell Telephone  C# (pronounced see sharp, like the musical note C♯, but written with the number sign) is a general-purpose, multi-paradigm programming language encompassing strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines.

  • This is not valid C++ char a[s.length()];
  • On top of out of bounds access, you have to null-terminate C-strings to use them with std::cout and strlen
  • On top of those 2 comments above, you should also null-terminate your C-style string.
  • char a[s.length() + 1] = {0}; will fix your code.
  • @MatthieuBrucher this is what happens when you have an event of 5 hours in work. Thank you very much for the downvote that alerted me! :)
  • Happens to me all the time ;) Nice answer now :)
  • @gsamaras, can you please explain why my array length showed up to be 6? I didnt get that completely.
  • Yes @anonymous, you cannot tell for sure what it would have been (this is what happens with Undefined Behavior). For example, in your computer, it's 6, in mine it could be another number (even a correct number if I was unlucky (in the sense that I wasn't going to be alerted)), and on TedLyngmo's computer 42 :)
  • Yes, it could read for three, four, or any amount of characters past the array. That is what happens with undefined behavior. edit: ok everyone beat me to it
  • In your for loop, wouldn't it be for(int i = 0; i < s.length()-1;i++) ?
  • Anyways if s = "1010", our vector a contains "010", please check it out.
  • That's the consequence, the assignment for 1 must also be -2.
  • Sorry, didn't get you