1
/* 
INCLUDES & DEFINES
*/
#define _CRT_SECURE_NO_WARNINGS 1

#include "stdlib.h"
#include "stdio.h"
#include "string.h"

#define N 10

#define LINE_LENGTH (65537*6) /* assuming we deal with integers. 65536 is the largest integer. each number in line offers a ' ' or '\0' and each number consists of 5 chars max.*/
#define STR_MAIN_ERROR_MSG "error occured while attempting to read data from file. aborted!\n"
#define STR_MENU "(a) print double-dynamic array.\n(b) add new number.\n(c) add new column.\n(d) remove last number.\n(e) remove last column.\n(q) quit program.\n\nEnter your choice: "
#define STR_MENU_INPUT_ERROR "Wrong Choice, try again.\n"
#define STR_MENU_QUIT "End of program, Thank you & good bye...\n"
#define STR_MENU_SHORT "Menu: ...\nEnter your choice: "
/*
FUNCTION DECLARATIONS
*/
int allocate_subarray(int**, const int);
int parse_line_data(char*,int**,const int);
int read_file_successful(int**);
void start_menu(int**);
void terminate(int**);

void funcA(const int**);
void funcB(int**);
void funcC(int**);
void funcD(int**);
void funcE(int**);
/*
MAIN FUNCTION BODY
*/
int main(){
    int *Array[N]={NULL};
    if ( read_file_successful(Array)) start_menu(Array);
    else printf("%s",STR_MAIN_ERROR_MSG);
    return 0;
}
/*
OTHER FUNCTION BODIES
*/
int allocate_subarray(int** p, const int size){
    *p=(int*)malloc(sizeof(int)*size);
    return (p!=NULL);
}
int parse_line_data(char *S, int **A, const int i){
    int j;
    int size=atoi(strtok(S," "));
    if(!allocate_subarray(&A[i], size)) return 0;
    for(j=0; j<size; j++) A[i][j]=atoi(strtok(NULL," "));
    return 1;
}
int read_file_successful(int** A){   /* <----- STACK OVERFLOW HAPPENS HERE. */
    FILE *f=fopen("input3.txt", "r");
    if(f){
        char line[LINE_LENGTH*N];
        int i;
        for(i=0; fgets(line,sizeof(line),f) && i<N; i++) 
            if (!(parse_line_data(line, A, i))) return 0;
        fclose(f);
        return 1;
    }
    return 0;
}
void start_menu(int **A){
    char choice;
    printf("%s",STR_MENU);
    do{
        scanf("%c",&choice);
        while(getchar()!='\n');
        switch(choice){
            case 'q': terminate(A); printf("%s",STR_MENU_QUIT); return;
            case 'a': funcA(A); break;
            case 'b': funcB(A); break;
            case 'c': funcC(A); break;
            case 'd': funcD(A); break;
            case 'e': funcE(A); break;
            default: printf("%s",STR_MENU_INPUT_ERROR);
        }
    } while(printf("%s",STR_MENU_SHORT));
}
void terminate(int **A){
    int i;
    for(i=0; i<N && A[i]; i++) free(A[i]);
}
/**/
void funcA(const int **A){}
void funcB(int **A){}
void funcC(int **A){}
void funcD(int **A){}
void funcE(int **A){}

帮朋友做这件事,这是一个从文本文件中读取行并将它们转换为存储在 malloc 分配的数组中的整数的基本程序(文本中也给出了大小)。问题是,很久没有用 C 调试这个美了,它在我为你评论的地方给了我一个堆栈溢出错误消息。尝试了函数参数的不同变体: int* A[] , int* A[N].. 仍然让我绊倒@run-time。

4

1 回答 1

3

您正在分配 N * LINE_LENGTH 堆栈空间read_file_successful()。算一算。那是 10 * 65537 * 6,或 3932220 字节。从而炸毁你的筹码。

char line[LINE_LENGTH*N];

其中 LINE_LENGTH 为 65537*6,N 为 10。

于 2012-11-27T00:29:40.090 回答