数组输入问题
这是我的代码:
#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");
}
}
当我编译它时,它成功完成,但在输入数组后,我收到此错误:
*** 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 00000000 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
任何人都可以帮我解决发生的事情吗?
亲切的问候
丹尼斯
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 00000000 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您
#define MAX n
,然后在其下方int n;
。因此,n
将被初始化为零,因为它具有文件范围(如果我错了,请有人纠正我,我检查了一些示例,但这并不能保证,而且我没有副本标准方便)。然后你在 main 中得到了这个:然后过了一会儿你终于为
n
分配了一个值,然后继续使用它。但是,您的board
是使用n
的初始值创建的。结果是,一切都假设board
比实际情况大,这就是你粉碎堆栈的方式。You
#define MAX n
and thenint 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:And then a little bit later you finally assign a value to
n
and then proceed to use that. But, yourboard
was created using the initial value ofn
. The result is that everything is assuming thatboard
is bigger than it really is and that's how you smash your stack.