/*
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] ..仍然讓我在運行時跳起來。C:INT *陣列時,傳遞給函數
你有什麼裁剪出來?還是這一切? – weston
是的,我做了,這是無關緊要的東西。 – ckid
此代碼對[ideone(http://ideone.com/ivKC07) – janisz