Zac*_*van 2 c malloc realloc dynamic-memory-allocation
所以我一直在用C编写一个mtf编码器,无论我做什么,我都遇到了realloc()错误.我已经通过使用print语句查看我的逻辑中是否存在错误(可能存在错误),看看我是否超出了当前malloc'd数组的范围(添加了超出原始数组大小的字符串)这似乎不是问题.我使用过GDB和Valgrind,而GDB给我一个神秘的信息,而Valgrind遇到了一个分段错误.这是我第一次使用动态内存,我对这个问题很困惑,下面是我的代码以及GDB错误:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int count = 0;
move_to_front(int index, char** words){
int i;
char *t = words[index];
for(i = index; i>1; i--){
words[i] = words[i-1];
}
words[1] = t;
}
char** reallocate_words(char** words, int* words_size_pointer){
printf("We're entering here\n");
printf("%d", *words_size_pointer);
int temp = *words_size_pointer;
char** tempor;
temp = temp*2;
printf("%d", temp);
tempor = (char**) realloc(words, temp);
int i = *words_size_pointer;
for(i; i<temp; i++){
tempor[i] = (char*) malloc(120);
}
words_size_pointer = &temp;
return tempor;
}
void encode_word(int* words_size_pointer, FILE *f, char* word, char** words){
if(count == 0){
words[1] = word;
fputs(words[1], f);
count++;
}
int i;
for(i=0; i<=count; i++){
if(strcmp(words[i], word) == 0){
break;
}
}
if(i>=(*words_size_pointer)){
printf("%d\n", i);
words = reallocate_words(words, words_size_pointer);
words[count+1] = word;
count++;
fputc(count+128, f);
fputs(words[count], f);
move_to_front(count, words);
}
if(i>count){
words[count+1] = word;
count++;
fputc(count+128, f);
fputs(words[count], f);
move_to_front(count, words);
}
else{
fputc(i+128, f);
move_to_front(i, words);
}
}
void sep_words(char** words, char *line, int* words_size_pointer, FILE *f){
char* x;
int i = 0;
x = strtok(line, " ");
while(x != NULL){
encode_word(words_size_pointer,f, x, words);
x = strtok(NULL, " ");
}
}
void readline(FILE *f_two, FILE *f, char** words, int* words_size_pointer){
char *line;
size_t len = 0;
ssize_t temp;
int count;
do{
temp = getline(&line,&len,f);
printf("%s", line);
if(temp!= -1){
sep_words(words, line, words_size_pointer, f_two);
}
}while(temp!=-1);
}
int main(int argc, char *argv[]){
int x;
int i;
int j;
x = strlen(argv[1]);
char fi[x];
char mtf[3] = "mtf";
FILE *f;
FILE *f_two;
for(j = 0; j<(x-3); j++){
fi[j] = argv[1][j];
}
strcat(fi, mtf);
f = fopen(argv[1], "r");
f_two = fopen(fi, "w");
fputc(0xFA, f_two);
fputc(0XCE, f_two);
fputc(0XFA, f_two);
fputc(0XDF, f_two);
if(f == NULL){
return 1;
}
char** words;
words = (char **) malloc(20);
for(i = 0; i<20; i++){
words[i] = (char*) malloc(120);
}
int words_size = 20;
int* words_size_pointer = &words_size;
readline(f_two, f, words, words_size_pointer);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
至于GDB错误:
*** Error in `/file_loc/mtfcoding2': realloc(): invalid next size: 0x0000000000603490 ***
2040 \\This is due to print statements within my function.
Program received signal SIGABRT, Aborted.
0x00007ffff7a4acc9 in __GI_raise (sig=sig@entry=6)
at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56 ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
Run Code Online (Sandbox Code Playgroud)
感谢您的时间!:)
malloc并realloc要求字节数作为参数.但是你编写的代码如下:
char** words;
words = (char **) malloc(20);
for(i = 0; i<20; i++){
words[i] = (char*) malloc(120);
Run Code Online (Sandbox Code Playgroud)
你分配20字节,但然后你写了20个指针(可能需要80字节).要解决此问题,您需要计算存储20个指针所需的字节数.这样做的一种安全方法是使用SO推荐的malloc:
words = malloc(20 * sizeof *words);
Run Code Online (Sandbox Code Playgroud)
您的realloc通话中存在同样的问题.
这条线没有效果:words_size_pointer = &temp;.也许你的意思*words_size_pointer = temp;.确保你清楚地理解这两行之间的区别.
NB.可能还有其他错误.
| 归档时间: |
|
| 查看次数: |
343 次 |
| 最近记录: |