realloc:无效的下一个大小和 malloc:内存损坏(快速) [英] realloc: invalid next size and malloc: memory corruption (fast)

查看:74
本文介绍了realloc:无效的下一个大小和 malloc:内存损坏(快速)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在根据 K 和 R C 编程书做一个有趣的练习.该程序用于从用户输入的一组行中找出最长的行并打印出来.

I am doing an exercise for fun from K and R C programming book. The program is for finding the longest line from a set of lines entered by the user and then prints it.

This is a test

This is another long test

this is another long testthis is another long test

观察:

它对于前两个输入运行良好,但对于较大的字符串(第三个输入)失败

Observation:

It runs fine for the first two inputs but fails for the larger string (3rd input)

Error in `./longest': realloc(): invalid next size: 0x000000000246e010 ***
Error in `./longest': malloc(): memory corruption (fast): 0x000000000246e030 ***

我的努力:

自 2 天以来我一直在尝试调试它(橡皮鸭调试),但逻辑似乎很好.GDB 指向 _getline 函数中的 realloc 调用,并在顶部显示了带有 glibc.so 内存分配调用的巨大回溯.

My efforts:

I have been trying to debug this since 2 days (rubber duck debugging) but the logic seems fine. GDB points to the realloc call in the _getline function and shows a huge backtrace with glibc.so memory allocation calls at the top.

以下是我写的(部分,部分直接取自书中):-

Here is what I have written (partially, some part is taken from the book directly):-

#include <stdio.h>
#include <stdlib.h>

int MAXLINE =  10;
int INCREMENT = 10;

char* line = NULL, *longest = NULL;

void _memcleanup(){
    free(line);
    free(longest);      
}

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;
    }    
}

int _getline(char s[]){
  int i,c;
  for(i=0; ((c=getchar())!=EOF && c!='\n'); i++){
    if(i == MAXLINE - 1){
      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';

  return i;
} 

int main(){
  int max=0, len;

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

  while((len = _getline(line)) > 0){
    printf("%d%d", len, MAXLINE);
    if(len > max){
      max = len;
      copy(longest, line);
    }
  }
  if(max>0){
    printf("%s",longest);
  }

  _memcleanup();
  return 0;
}

推荐答案

您正在重新分配复制的地址(因为参数).
C 中的参数每次都是原始值的副本;在
的情况下一个指针,它将指向相同的位置,但地址本身被复制.

You´re reallocating on copied addresses (because parameters).
A parameter in C is a copy of the original value everytime; in case of
a pointer it will point to the same location but the address itself is copied.

realloc 调整与地址相关的缓冲区的大小,到目前为止一切正常.
但它可以重新定位整个事物并分配一个全新的地址,
并且这个新地址(如果发生了)在函数返回到 main 后会丢失.

realloc resizes the buffer asociated with the address, everything fine so far.
But it can relocate the whole thing and assign a completely new address,
and this new address (if it happens) will be lost after the function returns to main.

使用双指针:
传递一个 char **s 而不是 char *s (==char s[]) 作为形参,
通过 &xyz 而不是 xyz 作为实际值,并在函数内部,
使用 *xyz**xyz(或 (*xyz)[index])作为地址和值.

Use a double pointer:
Pass a char **s instead of char *s (==char s[]) as formal parameter,
pass &xyz intead of xyz as actual value, and inside the function,
use *xyz and **xyz (or (*xyz)[index]) for address and value.

其他:
全局变量是丑陋的(并且在命名与参数相同时令人困惑),
乘以 sizeof(char) 是无稽之谈,因为它每次都是 1,
大写的名称应该用于#define而不是变量.

Other things:
Global variables are ugly (and confusing when named same as parameters),
multiplying with sizeof(char) is nonsense because it´s be 1 everytime,
and names in capitals should be used for #define´s rather than variables.

这篇关于realloc:无效的下一个大小和 malloc:内存损坏(快速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆