Woah, this just got spooky


#1

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


#2

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


#3

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


#4

Never heard of that term. Interesting read.

GIGO


#5

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


#6
for (startValue, stopCondition, step)

#7

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

#8

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

I get it now. Thanks, ionatan!


#9
#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.


#10

Aww, I liked the glitchy text :frowning:

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


#11

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