Exission Exission
Exission Exission

Reputation: 11

c++ pointer allocating more space than specified when using Visual Studio

When when I run this code in Visual Studio I get a bunch of garbage characters at the end but when I run it in an online compiler I get the exact characters I want?("olleH"). Please be gentle, im a noob.

#include<iostream>
#include<string>

int main() {
    std::string stringOne{ "Hello" };
    int stringLength = stringOne.length();
    char* stringTwo = new char[stringLength];
    for (int i = 0; i < stringLength; ++i) {
        *(stringTwo + i) = stringOne[stringLength - i - 1];
    }

    std::string stringThree = stringTwo;
    std::cout << stringThree << std::endl;
}

Upvotes: 0

Views: 61

Answers (2)

paxdiablo
paxdiablo

Reputation: 881653

Here's what you get with the char* stringTwo = new char[stringLength]; statement when stringLength is five (as it is here):

 _____stringTwo_____
/                   \
+---+---+---+---+---+------------------+
| ? | ? | ? | ? | ? | <arbitrary data> |
+---+---+---+---+---+------------------+

Then, your loop will populate it as follows:

 _____stringTwo_____
/                   \
+---+---+---+---+---+------------------+
| o | l | l | e | H | <arbitrary data> |
+---+---+---+---+---+------------------+

Then you treat is as a C-style string, despite the fact that there is no null terminator on the string. That means string functions will read beyond the end of the data and process whatever exists there. What you should be doing is constructing a real C style string, terminator and all:

char *stringTwo = new char[stringLength+1];
for (int i = 0; i < stringLength; ++i)
    *(stringTwo + i) = stringOne[stringLength - i - 1]; // or: stringTwo[i]
*(stringTwo + stringLength) = '\0';                     //     stringTwo[stringLength]

That will give you the more correct:

 _______stringTwo________
/                        \
+---+---+---+---+---+----+-----------------+
| o | l | l | e | H | \0 |<arbitrary data> |
+---+---+---+---+---+----+-----------------+

Of course, the use of C style strings in C++ is pretty much the textbook definition of a bad idea, suitable only in very rare circumstances if any.

Unless you want to be known as a C+ developer (that strange breed of C developer that never quite made the transition to C++ fully), you should embrace the more modern stuff found within C++, including std::string and the stuff from <algorithms> (if your need is to get something done rather than learn lower-level programming in an educational context):

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    std::string str("Hello");
    std::reverse<std::string::iterator>(str.begin(), str.end());
    std::cout << str << std::endl;
}

Upvotes: 3

Ranjan Kumar
Ranjan Kumar

Reputation: 1210

std:string is not null terminated string.
#include<iostream>
#include<string>

int main() {
    std::string stringOne{ "Hello" };
    int stringLength = stringOne.length();
    char* stringTwo = new char[stringLength+1]; // allocate one byte extra to store null character
    for (int i = 0; i < stringLength; ++i) {
        *(stringTwo + i) = stringOne[stringLength - i - 1];
    }
    *(stringTwo + stringLength) = '\0'; // add null character
    std::string stringThree = stringTwo;
    std::cout << stringThree << std::endl;
}

Upvotes: -1

Related Questions