开发者

Problem with Array Input

So here is my code:

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX n
int t,n;
int readtime ();
int readboardsize();
void readboard(int board[MAX][MAX], int n);
void printboard(int board[MAX][MAX]);

int main(int argc, char * argv[]){
    int board[MAX][MAX]; 
    t = readtime();
    n = readboardsize();
    printf("%d\n",n);
    readboard(board,n);
    printboard(board);
 return 0;

}

int readtime() {
    int nvr, scannedt;

    printf("Enter t :");
    nvr = scanf("%d",&scannedt);
    if (scannedt>1000 || scannedt<0 || nvr==0) {
        printf("Incorrect input: t must be 0..1000\n");
        exit(EXIT_FAILURE);
    }    
return scannedt;
} 

int readboardsize() {
    int nvr,scannedn;
    printf("Enter n :");
    nvr = scanf("%d",&scannedn);
    if (scannedn>25 || scannedn<3) {
        printf("Incorrect input: n must be 3..25\n");
        exit(EXIT_FAILURE);
    }
return scannedn;
}
void readboard(int board[MAX][MAX], int n) {
    int i,j,nvr;

    printf("Enter %d by %d forrest:\n",n,n);
        for(i = 0; i < MAX; i++){
            for(j=0; j < MAX; j++){
                nvr = scanf("%d",&board[i][j]);      
            }
        }
}              
void printboard(int board[MAX][MAX]) {
    int i,j;
    printf("Here is the board:\n\n");
        for(i = 0; i < MAX; i++){
            for(j=0; j < MAX; j++){
                if (board[i][j]==0 ) {
                    printf(". ");
                }else if (board[i][j]==1){
                    printf("^ ");
                }else if (board[i][j]==2){
                    printf("* ");
                } 
            }      
        printf("\n");
        }
}

When I compile it, it completes successfully but after I input the array I get this error:

*** stack smashing detected ***: ./ass2 terminated
======= Backtrace: =========
/lib/libc.so.6(__fortify_fail+0x50)[0x3e3990]
/lib/libc.so.6(+0xe593a)[0x3e393a]
./ass2[0x8048768]
/lib/libc.so.6(__libc_start_main+0xe7)[0x314ce7]
./ass2[0x8048421]
======= Memory map: ========
00110000-0012a000 r-xp 00000000 08:01 129968     /lib/libgcc_s.so.1
0012a000-0012b000 r--p 00019000 08:01 129968     /lib/libgcc_s.so.1
0012b000-0012c000 rw-p 0001a000 08:01 129968     /lib/libgcc_s.so.1
0025e000-0025f000 r-xp 00000000 00:00 0          [vdso]
002fe000-00455000 r-xp 00000开发者_JS百科000 08:01 134635     /lib/libc-2.12.1.so
00455000-00456000 ---p 00157000 08:01 134635     /lib/libc-2.12.1.so
00456000-00458000 r--p 00157000 08:01 134635     /lib/libc-2.12.1.so
00458000-00459000 rw-p 00159000 08:01 134635     /lib/libc-2.12.1.so
00459000-0045c000 rw-p 00000000 00:00 0 
00c1f000-00c3b000 r-xp 00000000 08:01 130134     /lib/ld-2.12.1.so
00c3b000-00c3c000 r--p 0001b000 08:01 130134     /lib/ld-2.12.1.so
00c3c000-00c3d000 rw-p 0001c000 08:01 130134     /lib/ld-2.12.1.so
08048000-08049000 r-xp 00000000 08:01 427835     /home/stu/Work/comp1911/ass2/ass2
08049000-0804a000 r--p 00000000 08:01 427835     /home/stu/Work/comp1911/ass2/ass2
0804a000-0804b000 rw-p 00001000 08:01 427835     /home/stu/Work/comp1911/ass2/ass2
09c12000-09c33000 rw-p 00000000 00:00 0          [heap]
b7830000-b7831000 rw-p 00000000 00:00 0 
b783e000-b7842000 rw-p 00000000 00:00 0 
bfe46000-bfe67000 rw-p 00000000 00:00 0          [stack]
Aborted

Can anyone give me a hand with whats going on?

Kind Regards

Dennis


You #define MAX n and then int n; just below that. So, n will be initialized to zero as it has file scope (someone correct me on this if I'm wrong please, I checked a few examples but that's no guarantee and I don't have a copy of the standard handy). Then you have this in main:

int board[MAX][MAX];

And then a little bit later you finally assign a value to n and then proceed to use that. But, your board was created using the initial value of n. The result is that everything is assuming that board is bigger than it really is and that's how you smash your stack.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜