[英]Invalid read of size - C valgrind
int main(int argc, char **argv) {
if (argc < 3) {
printf("Aufruf: %s <anzahl> <bundesland>\n", argv[0]);
printf("Beispiel: %s 100 Bayern\n", argv[0]);
printf("Klein-/Großschreibung beachten!\n");
exit(1);
}
int anzahl = atoi(argv[1]);
char *bundesland = argv[2];
// Statisch allokierter Speicher
char staedte[MAX_LAENGE_ARR][MAX_LAENGE_STR];
char laender[MAX_LAENGE_ARR][MAX_LAENGE_STR];
int bewohner[MAX_LAENGE_ARR];
int len = read_file("staedte.csv", staedte, laender, bewohner);
// Hier implementieren
int x=0;
int d=0;
//int b=0;
//char* zeile_array=(char*) malloc (len * sizeof(char));
char *zeile_array[50];
char *zeile;
zeile=(char*) malloc(100); //allocating memory
for(x=0;x<len;x++)
{
if(strcmp(laender[x],bundesland) == 0 && bewohner[x] >= anzahl)
{
sprintf(zeile, "Die Stadt %p hat %d Einwohner.",staedte[x],bewohner[x]);
zeile_array[d]=zeile; // putting it into array
d=d+1;
}
}
//b++;
}
write_file(zeile_array,d);
free(zeile);
}
輸入3.c:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "input3.h"
int MAX_LAENGE_STR = 255;
int MAX_LAENGE_ARR = 100;
void write_file(char *result[], int len) {
FILE *fp = fopen("resultat.txt", "w");
if (fp == NULL){
perror("resultat.txt");
exit(1);
}
for (int i=0; i<len; i++) {
fprintf(fp, "%s\n", result[i]);
}
fclose(fp);
}
int read_file(char *dateiname, char staedte[][MAX_LAENGE_STR], char laender[][MAX_LAENGE_STR], int bewohner []) {
FILE *fp = fopen(dateiname, "r");
if (fp == NULL){
perror(dateiname);
exit(1);
}
char stadt[MAX_LAENGE_STR];
char land[MAX_LAENGE_STR];
int anzahl;
int i = 0;
int len;
while(fscanf(fp, "\"%[^\"]\";\"%[^\"]\";%d\n", stadt, land, &anzahl) != EOF)
{
if (i >= MAX_LAENGE_ARR) {
printf("ERROR: Die Datei ist größer als erwartet!");
return i;
}
len = strlen(land) + 1;
strncpy(laender[i], land, len-1);
laender[i][len-1] = '\0';
len = strlen(stadt) + 1;
strncpy(staedte[i], stadt, len-1);
staedte[i][len-1] = '\0';
bewohner[i] = anzahl;
i++;
}
fclose(fp);
return i;
}
輸入3.h:
extern int MAX_LAENGE_ARR;
extern int MAX_LAENGE_STR;
void write_file(char *result[], int len);
int read_file(char *dateiname, char staedte[][MAX_LAENGE_STR], char `laender[][MAX_LAENGE_STR], int bewohner []);
`
Invalid read of size 2
==23056== at 0x4EC361B: __GI_mempcpy (memcpy.S:83)
==23056== by 0x4EB054C: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1320)
==23056== by 0x4E82904: vfprintf (vfprintf.c:1661)
==23056== by 0x4E8B336: fprintf (fprintf.c:32)
==23056== by 0x400EE0: write_file (input3.c:17)
==23056== by 0x400E52: main (d.c:56)
==23056== Address 0x51fc391 is 33 bytes inside a block of size 100 free'd
==23056== at 0x4C2BDEC: free (in /usr/lib/valgrind/vgpreload_memcheck- amd64-linux.so)
==23056== by 0x400E14: main (d.c:50)
staedte.csv 的前 4 行:
"Berlin";"Berlin";3460725
"Hamburg";"Hamburg";1786448
"München";"Bayern";1353186
"Köln";"Nordrhein-Westfalen";1007119
"Frankfurt am Main";"Hessen";679664
我只是想將一些字符串寫入一個新文件,但我每次都遇到相同的內存錯誤。 我無法幫助自己我需要幫助來解決這個錯誤。 我不知道如何解決它,我很絕望。
以下是main()
的建議解決方案:
已更正的錯誤和增強功能列表:
1- 在格式化輸出中,將%p
(專用於指針)替換為%s
(專用於字符串)。
2- 在存儲在輸出字符串zeile_array[d]
將其分配給輸出文本zeile
的 len,包括空終止符。
3- 不是將輸出文本指針zeile
復制到輸出字符串zeile_array[d]
, zeile_array[d]
使用strcpy()
函數移動所有字符。
4- 為簡化起見,修改臨時字符串zeile
從char 指針動態分配在內存中的一個簡單的char 數組自動分配在堆棧中。
5- 保護,當數組char *zeile_array[50];
所有項時停止提取結果char *zeile_array[50];
被用過。 或者簡單地使用MAX_LAENGE_ARR
常量分配足夠的數組。
// Hier implementieren
int x=0;
int d=0;
//int b=0;
//char* zeile_array=(char*) malloc (len * sizeof(char));
char *zeile_array[MAX_LAENGE_ARR]; // instead of char *zeile_array[50];
char zeile[100];
// zeile=(char*) malloc(100); // allocated memory not needed
for(x=0;x<len;x++)
{
if(strcmp(laender[x],bundesland) == 0 && bewohner[x] >= anzahl)
{
// as Micheal Walz said, replace %p by %s
sprintf(zeile, "Die Stadt %s hat %d Einwohner.",staedte[x],bewohner[x]);
// allocate the storing item
zeile_array[d]=(char *)malloc(strlen(zeile)+1);
// copy the formatted text into the storing item
strcpy(zeile_array[d],zeile); // putting it into array
d=d+1;
// abort research when output array is full
if (d >= MAX_LAENGE_ARR) break;
}
}
//b++;
//} // remove unexpected end of block
write_file(zeile_array,d);
// free the allocated memory only
for(x=0;x<d;x++)
{
free(zeile_array[x]);
}
// free(zeile); is no more needed
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.