2 Replies - 204 Views - Last Post: 31 January 2019 - 12:46 PM Rate Topic: -----

#1 victorhjelmberg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 31-January 19

Is this a memory error? How can i make it better? (C)

Posted 31 January 2019 - 05:26 AM

Hey, so i'm working on this little program for fun, and so i can learn c. I code in xcode on mac, if that should be relevant. I've made a game using the output panel. The game is made of 5*5 squares, and a player in the middel im using a 2d array for this. I've made the player move with fprint commands.

The problems ocours when i move the player in a direction. The [0][0] point in the array gets screved up, and depending on the compiler it writes out a different result. In xcode i respect the output looking like:

1 . . . . .
2 . . o . .
3 . . . . .
4 . . . . .
5 . . . . .

But instead i get:

1 Minispil . . . .
2 . . o . .
3 . . . . .
4 . . . . .
5 . . . . .

The name minispil is a part of the code way above, and has nothing to do with any other part of the script. I've tried to remove the return point from the game function, and that have fixes it. I don't understand. I need a return value, to be able to stop the game, so that's not a solution i can use. The problem is really wierd, because i don't think it has anything to do with a codeing error. Rather a memory error.

I'll leave the code here. The nvm function has nothing to do with the game, but i though i'd just leave the whole thing in, so you can see what im dealing with.
When you run the code, i've made 2 options in the output channel. The second is the game, so just type "2" to launch it. When the game is running you can type "w", "d", "s" or "a" to control the player. I hope i've shared enough information about my problem, and i hope to learn of it.

The code is:

#include <stdio.h>
#include <string.h>

int nvm(void);
void gameprint(char *ada[5][5]);
int game(void);



int main()
{
    char Number[1];
    char * num1 = "1";
    char * num2 = "2";
    int rvalue;
    printf("Vælg dit program\n");
    printf("1 - Navnebedømmer\n");
    printf("2 - Minispil\n");
    scanf("%s",Number);
    if (strcmp(Number,num1) == 0) {
        printf("\n\n\n\n\n\n");
        int nummer = nvm();
        return nummer;
    } else if (strcmp(Number,num2) == 0){
        rvalue = game();
        return rvalue;
    }
}


int nvm(void)
{
    char navn[10];
    char * check = "Victor";
    char * ad = "Tim";
    char * clear = "c";
    char * end = "e";
    int i;
    printf ("Skriv et navn\n");
    for (i=0; i<100;i++){
        scanf ("%s",navn);
        if (strcmp(navn,check) == 0) {
            printf ("\n\n\n\n\n\n%s er et fucking fedt navn\n",navn);
        } else if (strcmp(navn,ad) == 0) {
            printf ("\n\n\n\n\n\n%s er et fucking klamt navn\n",navn);
        } else if (strcmp(navn,clear) == 0) {
            printf ("\n\n\n\n\n\n\n");
        } else if (strcmp(navn,end) == 0){
            return 0;
        } else {
            printf ("\n\n\n\n\n\n%s er et okay navn.\n",navn);
        }

    }
    return 0;
    
}


int game(void)
{
    char move[1];
    struct pointstruct{
        int x;
        int y;
    };
    struct pointstruct point;
    point.x = 2;
    point.y = 2;
    char *ada[5][5] = {
        {".",".",".",".","."} ,
        {".",".",".",".","."} ,
        {".",".","O",".","."} ,
        {".",".",".",".","."} ,
        {".",".",".",".","."}
    };
    gameprint(ada);
    for (;;)/>{
        scanf("%s",move);
        if (strcmp(move,"w") == 0) {
            ada[point.x][point.y] = ".";
            point.x -= 1;
            ada[point.x][point.y] = "O";
        } else if (strcmp(move,"s") == 0) {
            ada[point.x][point.y] = ".";
            point.x += 1;
            ada[point.x][point.y] = "O";
        } else if (strcmp(move,"d") == 0) {
            ada[point.x][point.y] = ".";
            point.y += 1;
            ada[point.x][point.y] = "O";
        } else if (strcmp(move,"a") == 0) {
            ada[point.x][point.y] = ".";
            point.y -= 1;
            ada[point.x][point.y] = "O";
        }
        
        if (point.x == 0){
            if (point.y == 0){
                return 0;
            }
        }
        gameprint(ada);
    }
}

void gameprint(char *ada[5][5])
{
    int i;
    int j;
    int g;
    for (j = 0; j<5; j++) {
        g = j + 1;
        printf("%d ",g);
        for (i = 0; i<5; i++) {
            if (i == 4){
            printf("%s",ada[j][i]);
            } else {
            printf("%s ",ada[j][i]);
            }
        }
        printf("\n");
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Is this a memory error? How can i make it better? (C)

#2 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7401
  • View blog
  • Posts: 15,338
  • Joined: 16-October 07

Re: Is this a memory error? How can i make it better? (C)

Posted 31 January 2019 - 07:15 AM

I don't see any compelling reason you should be using strings here. Instead:
    char ada[5][5] = {
        {'.','.','.','.','.'},
        {'.','.','.','.','.'},
        {'.','.','O','.','.'},
        {'.','.','.','.','.'},
        {'.','.','.','.','.'},
    };



Or perhaps better:
// no magic values!
#define ADA_ROWS 5
#define ADA_COLS 5
#define C_EMPTY '.'
#define C_PLAYER 'O'

typedef char Ada[ADA_ROWS][ADA_COLS];

void initAda(Ada);
void printAda(Ada);
void applyMove(Ada, char);


Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 6773
  • View blog
  • Posts: 23,077
  • Joined: 05-May 12

Re: Is this a memory error? How can i make it better? (C)

Posted 31 January 2019 - 12:46 PM

When you call scanf("%s",move), it will input a string. C strings will always have null terminator. So for a single letter string, you need space for at least two characters. Notice how on lines 12 and 62 where you have only room for one character. When scanf() tries to store the string into your buffer, it will have a buffer overrun. When you have a buffer overrun, you are into undefined behavior territory. Anything can happen, including the weird behavior you are seeing above.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1