Woah, this just got spooky

So a while ago, I decided to fiddle around with C++, until this happened:


here is the code:

#include <iostream>

using namespace std;

int main()
{
    string funnyString = " y a;er^&%&^%^&$%&$%7667567m.";
    string otherString = " 43098rf39rk34fertkibuertbuiertb045b645456$#^#$%^6$#%^$#%^$%&^$^&@#$#!$#$@$`~~```~``";
    for (int x = 1; x+=1; x++) {
        cout << funnyString[x] << otherString[x];
        cout << otherString[x];
        cout << funnyString[x];
        cout << funnyString[x] << otherString[x];
        cout << otherString[x];
        cout << funnyString[x];
        cout << funnyString[x] << otherString[x];
        cout << otherString[x];



    }

    return 0;
}

Can someone tell me why I am getting these weird characters lol

1 Like

You’re reading garbage, x isn’t a valid index and cpp doesn’t stop you from doing that

1 Like

What did we used to call that back in the day? GIGO?

1 Like

Never heard of that term. Interesting read.

GIGO

3 Likes

That was what I was thinking, it’s getting the index of it but once it exceeds its length, it starts… getting these random characters I guess xD

1 Like
for (startValue, stopCondition, step)
3 Likes

It’s not random, it’s whatever happens to be in that piece of memory. Bugs like this can cause programs to leak information they’re not meant to share, for example a first string might be a username and the next one might be a password. Keep reading and you get both.

#include <cstring>
#include <iostream>

struct user {
    char user[100];
    char password[100];
};


int main() {
    user bob;
    // zero out the fields to reduce spam in output
    memset(bob.password, 0, sizeof(bob.password));
    memset(bob.user, 0, sizeof(bob.user));
    // write to the string fields
    strcpy(bob.user, "bobthegreat");
    strcpy(bob.password, "123carrotlover");

    for (int i = 0; i < 200; i++) {
        // note only reading from user, not password
        std::cout << bob.user[i];
    }
    std::cout << std::endl;
}

output:

bobthegreat123carrotlover
1 Like

ohhh, I get it. So this weird index thing will just expose some data.

I get it now. Thanks, ionatan!

1 Like
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string funnyString = " y a;er^&%&^%^&$%&$%7667567m.";
    string otherString = " 43098rf39rk34fertkibuertbuiertb045b645456$#^#$%^6$#%^$#%^$%&^$^&@#$#!$#$@$`~~```~``";
    for (int x = 0; x<10; x++) 
        cout<< funnyString[x];
        
    for(int x=0; x<10; x++)
    	cout<<otherString[x]; 
}

I changed a few things and got the strings to print out correctly. They only print to the tenth character, but you could change that to whatever to print the whole string out.

1 Like

Aww, I liked the glitchy text :frowning:

Oh well, thanks for trying to make it function properly, though!

1 Like

Yeah. That did look cool I must admit. Good that it’s functioning properly.

1 Like