gpt4 book ai didi

c - 段错误总是在 3 个输入后发生

转载 作者:行者123 更新时间:2023-11-30 15:31:26 25 4
gpt4 key购买 nike

我有以下代码来接受用户的任意行数并打印出长度> 80 个字符的行:-

#include <stdio.h>
#include <stdlib.h>
#include "shared.h"
#include <string.h>

int MAXLINE = 10;
int INCREMENT = 10;
int NUM = 1;

char* longest = NULL;
char* line = NULL;
char** row = NULL;

void _memcleanup(){

int i =0;

free(line);
free(longest);

for(i=0;i<NUM;i++){
free(row[i]);
}

free(row);
}

void print_lines(int len){

int i;

for(i=0;i<len;i++){

if(strlen(row[i])>80){
printf("%s\n",row[i]);
}

}


}

void copy(char** longest, char** line){

int i=0;

char* temp = realloc(*longest,(MAXLINE)*sizeof(char));

if(temp == NULL){
printf("%s","Unable to allocate memory");
_memcleanup();
exit(1);
}

*longest = temp;

while(((*longest)[i] = (*line)[i]) != '\0'){
++i;
}

longest[i] = '\0';

}

void store(char** s, int pos){

int i=0;



char* temp = realloc(row[pos],(MAXLINE)*sizeof(char));

if(temp == NULL){
printf("%s","Unable to allocate memory");
_memcleanup();
exit(1);
}

row[pos] = temp;

while((row[pos][i] = (*s)[i]) != '\0'){
++i;
}

row[pos][i] = '\0';

}

int _getline(char** s, int pos){

int i,c;

for(i=0; ((c=getchar())!=EOF && c!='\n'); i++){

if(i == MAXLINE - 2){

char* temp = realloc(*s,(MAXLINE + INCREMENT)*sizeof(char));

if(temp == NULL){
printf("%s","Unable to allocate memory");
_memcleanup();
exit(1);
}

*s= temp;

MAXLINE += INCREMENT;
}
(*s)[i] = c;
}

if(c == '\n'){
(*s)[i++] = c;
}

(*s)[i]= '\0';

store(s, pos);

return i;
}

int main(){

int max=0, len, i=0;

line = malloc(MAXLINE*sizeof(char));
longest = malloc(MAXLINE*sizeof(char));

//array of character pointers
row = malloc(NUM*sizeof(char*));

//allocate memory for each row in the array
for(i = 0; i < NUM; i++){
row[i]= malloc(MAXLINE*(sizeof(char)));
}

i=0;

//for(i=0; len = _getline(&line)) > 0; i++){
while((len = _getline(&line, i)) > 0){
printf("%d %d", len, MAXLINE);

/* if(len > max){ */
/* max = len; */
/* copy(&longest, &line); */
/* } */

i++;
}
/* if(max>0){ */
/* printf("%s",longest); */
/* } */

print_lines(i);

_memcleanup();
return 0;

}

我遵循的想法是当行数超过 NUM 时重新分配二维数组。现在为了测试它,我将 NUM 设置为 1。但是,即使在这样做之后,程序也乐意接受最多 3 个输入,并在第 4 个输入上出现段错误,即程序上下文中的 pos=3。

为什么它接受 3 个输入(理想情况下它本身应该给出段错误 pos=1,因为我给定的大小仅为 1 并且我没有为 2D 数组分配更多空间)

工作代码如下:

#include <stdio.h>
#include <stdlib.h>
#include "shared.h"
#include <string.h>

int MAXLINE = 10;
int INCREMENT = 10;
int NUM = 1;

char* longest = NULL;
char* line = NULL;
char** row = NULL;

void _memcleanup(){

int i =0;

free(line);
free(longest);

for(i=0;i<NUM;i++){
free(row[i]);
}

free(row);
}

void print_lines(int len){

int i;

for(i=0;i<len;i++){

if(strlen(row[i])>80){
printf("%s\n",row[i]);
}

}


}

void copy(char** longest, char** line){

int i=0;

char* temp = realloc(*longest,(MAXLINE)*sizeof(char));

if(temp == NULL){
printf("%s","Unable to allocate memory");
_memcleanup();
exit(1);
}

*longest = temp;

while(((*longest)[i] = (*line)[i]) != '\0'){
++i;
}

longest[i] = '\0';

}

void store(char** s, int pos){

int i=0;

if(pos == NUM){
char** temprow = realloc(row, (NUM + INCREMENT)*sizeof(char*));

if(temprow == NULL){
printf("%s","Unable to allocate memory");
_memcleanup();
exit(1);
}

row = temprow;
//allocate space for extra elements
for(i=NUM;i<NUM+INCREMENT;i++){
row[i] = malloc(MAXLINE*sizeof(char));
}

NUM = NUM + INCREMENT;

}

char* temp = realloc(row[pos],(MAXLINE)*sizeof(char));

if(temp == NULL){
printf("%s","Unable to allocate memory");
_memcleanup();
exit(1);
}

row[pos] = temp;

while((row[pos][i] = (*s)[i]) != '\0'){
++i;
}

row[pos][i] = '\0';

}

int _getline(char** s, int pos){

int i,c;

for(i=0; ((c=getchar())!=EOF && c!='\n'); i++){

if(i == MAXLINE - 2){

char* temp = realloc(*s,(MAXLINE + INCREMENT)*sizeof(char));

if(temp == NULL){
printf("%s","Unable to allocate memory");
_memcleanup();
exit(1);
}

*s= temp;

MAXLINE += INCREMENT;
}
(*s)[i] = c;
}

if(c == '\n'){
(*s)[i++] = c;
}

(*s)[i]= '\0';

store(s, pos);

return i;
}

int main(){

int max=0, len, i=0;

line = malloc(MAXLINE*sizeof(char));
longest = malloc(MAXLINE*sizeof(char));

//array of character pointers
row = malloc(NUM*sizeof(char*));

//allocate memory for each row in the array
for(i = 0; i < NUM; i++){
row[i]= malloc(MAXLINE*(sizeof(char)));
}

i=0;

//for(i=0; len = _getline(&line)) > 0; i++){
while((len = _getline(&line, i)) > 0){
printf("%d %d", len, MAXLINE);

/* if(len > max){ */
/* max = len; */
/* copy(&longest, &line); */
/* } */

i++;
}
/* if(max>0){ */
/* printf("%s",longest); */
/* } */

print_lines(i);

_memcleanup();
return 0;

}

最佳答案

你问的是:

Why does it accept 3 inputs (ideally it should give a segfault pos=1 itself since i have given size as only 1 and i am not allocating more space for the 2D array)

你是对的,如果你只为一行分配内存,那么当你尝试访问行[1]时,它会调用未定义行为。但这种行为只是 - 未定义 - 这意味着你不能依赖程序崩溃。任何事情都可能发生,包括程序似乎完美运行

关于c - 段错误总是在 3 个输入后发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24808975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com