Realloc没有任何特定原因失败

发布于 2025-01-24 01:42:36 字数 6788 浏览 4 评论 0原文

我正在尝试解决一个简单的C问题。

问:使用int指针接收用户的“无限”输入,直到使用malloc和realloc接收到EOF为止。

我定义了这样的int指针:

int *inputBuffer = NULL;

并使用以下方法初始化了它:

    /* #define BUFFER_SIZE 8  - already defined at top of code, posted here for more info */    
/* Creates buffer by using malloc - 
            if succeded returns true, otherwise false */
        int createBuffer(int **inputBuffer)
        {
            *inputBuffer = (int*) calloc(BUFFER_SIZE, sizeof(char)); /* Allocate memory to get input from user */
            if(*inputBuffer == NULL)    
                return FALSE;
            return TRUE;
        }

通过调用createBuffer(& inputbuffer)> 到目前为止,记忆已成功分配。 在开始从用户接收字符之前,我定义了以下属性:

int totalCharacters = 0;
int bufferExtendsCounter = 1;
int character;

下一步是从用户接收字符输入:

   /* #define MEMORY_SAFE_GAP 4  - already defined at top of code, posted here for more info */
  while((character = getchar()) != EOF && inputBuffer != NULL)
  {
        /* Check if reallocate needs to be called since almost maxed out buffer */
        if(totalCharacters - MEMORY_SAFE_GAP > (BUFFER_SIZE * bufferExtendsCounter))
        {
            /* Add 1 to extends buffer times */
            bufferExtendsCounter+=1;
            if(!extendBuffer(&inputBuffer, totalCharacters))
                printf("\nFailed to allocate more memory.");
        }       

        /* Initialize buffer with character, this is safe since there is a memory safe gap */
        inputBuffer[totalCharacters] = character;

        totalCharacters++;
  }

扩展缓冲区看起来像这样:

/* Extends buffer size by using realloc 
    if succeded returns true, otherwise false */
int extendBuffer(int **inputBuffer, int minimumBufferSize)
{
    /* Check how many times buffer needs to be multiple (at least) */
    int multipleBufferNumber = (minimumBufferSize / BUFFER_SIZE) + 1;
    int newBufferSize = BUFFER_SIZE * multipleBufferNumber * sizeof(char);
    while(newBufferSize < minimumBufferSize)
    {
        multipleBufferNumber+=1;
        newBufferSize = BUFFER_SIZE * multipleBufferNumber * sizeof(char);
    }
    /* Reallocate memory for next chunck of data */
    *inputBuffer = realloc(*inputBuffer, newBufferSize);
    /* Check if memory successfully allocated */
    if(*inputBuffer == NULL)
        return FALSE;
    return TRUE;
}

看起来我扩展了缓冲区大小,以便用户更多输入,但仍然会出现错误:

corrupted size vs. prev_size: 0x08f86010 ***

示例输入:

TestProgramTest (Pressed Enter after last 't')
(DebugPrint: Received 13 characters)
(DebugPrint: Reallocating to size 16)
*** Error in `./test': corrupted size vs. prev_size: 0x08f86010 ***

编辑(由于缺乏代码部分): 以下部分是在之后的,

    inputBuffer[totalCharacters] = '\0';
    printf("\nInput by user:\n");
/* #define LINE_LENGTH 5  - already defined at top of code, posted here for more info */
    printBuffer(inputBuffer, LINE_LENGTH, totalCharacters);
    /* free memory */
    free(inputBuffer);

而loop :printbuffer看起来像:

/* Prints the buffer data to pretty output */
void printBuffer(int *inputBuffer, int lineLength, int totalCharacters)
{
    int i;
    for(i = 0; i < totalCharacters; i++)
    {
        /* Print each char value */
        printf("%c", inputBuffer[i]);
        /* Check if got to line limit, if so enter new line */
        if((i+1) % lineLength == 0)
            printf("\n");
    }
}

第二次编辑: 将所有INT指针零件更改为char指针。 完整的代码看起来像:

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

#define LINE_LENGTH 5
#define BUFFER_SIZE 8
#define TRUE 1
#define FALSE 0
#define MEMORY_SAFE_GAP 4

int createBuffer(char **inputBuffer);
int extendBuffer(char **inputBuffer, int minimumBufferSize);
void printBuffer(char *inputBuffer, int lineLength, int totalCharacters);

int main(void)
{
    char *inputBuffer = NULL;
    if(!createBuffer(&inputBuffer))
    {
        printf("Memory cannot be allocated, program will exit now.");
        exit(-1);
    }
    int totalCharacters = 0;
    int bufferExtendsCounter = 1;
    char character;

    printf("Please enter a string:\n");
    /* Loop till EOF received */ 
    while((character = getchar()) != EOF && inputBuffer != NULL)
    {
        /* Check if reallocate needs to be called since almost maxed out buffer */
        if(totalCharacters - MEMORY_SAFE_GAP > (BUFFER_SIZE * bufferExtendsCounter))
        {
            /* Add 1 to extends buffer times */
            bufferExtendsCounter+=1;
            if(!extendBuffer(&inputBuffer, totalCharacters))
                printf("\nFailed to allocate more memory.");
        }       

        /* Initialize buffer with character, this is safe since there is a memory safe gap */
        inputBuffer[totalCharacters] = character;

        totalCharacters++;
    }
    inputBuffer[totalCharacters] = '\0';      
    printBuffer(inputBuffer, LINE_LENGTH, totalCharacters);
    /* free memory */
    free(inputBuffer);
    return 0;
}
/* Creates buffer by using malloc
    if succeded returns true, otherwise false */
int createBuffer(char **inputBuffer)
{
    /* Allocate memory to get input from user */
    *inputBuffer = (char*) calloc(BUFFER_SIZE, sizeof(char)); 
    if(*inputBuffer == NULL)    
        return FALSE;
    return TRUE;
}

/* Extends buffer size by using realloc 
    if succeded returns true, otherwise false */
int extendBuffer(char **inputBuffer, int minimumBufferSize)
{
    /* Check how many times buffer needs to be multiple (at least) */
    int multipleBufferNumber = (minimumBufferSize / BUFFER_SIZE) + 1;
    int newBufferSize = BUFFER_SIZE * multipleBufferNumber * sizeof(char);
    while(newBufferSize < minimumBufferSize)
    {
        multipleBufferNumber+=1;
        newBufferSize = BUFFER_SIZE * multipleBufferNumber * sizeof(char);
    }
    /* Reallocate memory for next chunck of data */
    *inputBuffer = realloc(*inputBuffer, newBufferSize);
    /* Check if memory successfully allocated */
    if(*inputBuffer == NULL)
        return FALSE;
    return TRUE;
}

/* Prints the buffer data to pretty output */
void printBuffer(char *inputBuffer, int lineLength, int totalCharacters)
{
    printf("Printing buffer\n");
    int i;
    for(i = 0; i < totalCharacters; i++)
    {
        /* Print each char value */
        printf("%c", inputBuffer[i]);
        /* Check if got to line limit, if so enter new line */
        if((i+1) % lineLength == 0)
            printf("\n");
    }
}

任何帮助都很好!

提前致谢。

I am trying to solve a "suppose to be" a simple C question.

Q: Receive "infinite" input by user using int pointer until EOF received using malloc and realloc.

I defined a int pointer like this:

int *inputBuffer = NULL;

and initialized it using this method:

    /* #define BUFFER_SIZE 8  - already defined at top of code, posted here for more info */    
/* Creates buffer by using malloc - 
            if succeded returns true, otherwise false */
        int createBuffer(int **inputBuffer)
        {
            *inputBuffer = (int*) calloc(BUFFER_SIZE, sizeof(char)); /* Allocate memory to get input from user */
            if(*inputBuffer == NULL)    
                return FALSE;
            return TRUE;
        }

by calling createBuffer(&inputBuffer)
so far so good, memory is allocated successfully.
before starting to receive characters from user I defined the following properties:

int totalCharacters = 0;
int bufferExtendsCounter = 1;
int character;

Next step is to receive characters inputs from user like this:

   /* #define MEMORY_SAFE_GAP 4  - already defined at top of code, posted here for more info */
  while((character = getchar()) != EOF && inputBuffer != NULL)
  {
        /* Check if reallocate needs to be called since almost maxed out buffer */
        if(totalCharacters - MEMORY_SAFE_GAP > (BUFFER_SIZE * bufferExtendsCounter))
        {
            /* Add 1 to extends buffer times */
            bufferExtendsCounter+=1;
            if(!extendBuffer(&inputBuffer, totalCharacters))
                printf("\nFailed to allocate more memory.");
        }       

        /* Initialize buffer with character, this is safe since there is a memory safe gap */
        inputBuffer[totalCharacters] = character;

        totalCharacters++;
  }

extend buffer looks like this:

/* Extends buffer size by using realloc 
    if succeded returns true, otherwise false */
int extendBuffer(int **inputBuffer, int minimumBufferSize)
{
    /* Check how many times buffer needs to be multiple (at least) */
    int multipleBufferNumber = (minimumBufferSize / BUFFER_SIZE) + 1;
    int newBufferSize = BUFFER_SIZE * multipleBufferNumber * sizeof(char);
    while(newBufferSize < minimumBufferSize)
    {
        multipleBufferNumber+=1;
        newBufferSize = BUFFER_SIZE * multipleBufferNumber * sizeof(char);
    }
    /* Reallocate memory for next chunck of data */
    *inputBuffer = realloc(*inputBuffer, newBufferSize);
    /* Check if memory successfully allocated */
    if(*inputBuffer == NULL)
        return FALSE;
    return TRUE;
}

It looks like I extend the buffer size enough for more input by user, but still gets error:

corrupted size vs. prev_size: 0x08f86010 ***

Example input:

TestProgramTest (Pressed Enter after last 't')
(DebugPrint: Received 13 characters)
(DebugPrint: Reallocating to size 16)
*** Error in `./test': corrupted size vs. prev_size: 0x08f86010 ***

EDIT (Due to lack of code parts):
The following part is right after while loop:

    inputBuffer[totalCharacters] = '\0';
    printf("\nInput by user:\n");
/* #define LINE_LENGTH 5  - already defined at top of code, posted here for more info */
    printBuffer(inputBuffer, LINE_LENGTH, totalCharacters);
    /* free memory */
    free(inputBuffer);

and printBuffer looks like:

/* Prints the buffer data to pretty output */
void printBuffer(int *inputBuffer, int lineLength, int totalCharacters)
{
    int i;
    for(i = 0; i < totalCharacters; i++)
    {
        /* Print each char value */
        printf("%c", inputBuffer[i]);
        /* Check if got to line limit, if so enter new line */
        if((i+1) % lineLength == 0)
            printf("\n");
    }
}

Second edit:
Changed all int pointer parts to char pointer.
Full code looks like:

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

#define LINE_LENGTH 5
#define BUFFER_SIZE 8
#define TRUE 1
#define FALSE 0
#define MEMORY_SAFE_GAP 4

int createBuffer(char **inputBuffer);
int extendBuffer(char **inputBuffer, int minimumBufferSize);
void printBuffer(char *inputBuffer, int lineLength, int totalCharacters);

int main(void)
{
    char *inputBuffer = NULL;
    if(!createBuffer(&inputBuffer))
    {
        printf("Memory cannot be allocated, program will exit now.");
        exit(-1);
    }
    int totalCharacters = 0;
    int bufferExtendsCounter = 1;
    char character;

    printf("Please enter a string:\n");
    /* Loop till EOF received */ 
    while((character = getchar()) != EOF && inputBuffer != NULL)
    {
        /* Check if reallocate needs to be called since almost maxed out buffer */
        if(totalCharacters - MEMORY_SAFE_GAP > (BUFFER_SIZE * bufferExtendsCounter))
        {
            /* Add 1 to extends buffer times */
            bufferExtendsCounter+=1;
            if(!extendBuffer(&inputBuffer, totalCharacters))
                printf("\nFailed to allocate more memory.");
        }       

        /* Initialize buffer with character, this is safe since there is a memory safe gap */
        inputBuffer[totalCharacters] = character;

        totalCharacters++;
    }
    inputBuffer[totalCharacters] = '\0';      
    printBuffer(inputBuffer, LINE_LENGTH, totalCharacters);
    /* free memory */
    free(inputBuffer);
    return 0;
}
/* Creates buffer by using malloc
    if succeded returns true, otherwise false */
int createBuffer(char **inputBuffer)
{
    /* Allocate memory to get input from user */
    *inputBuffer = (char*) calloc(BUFFER_SIZE, sizeof(char)); 
    if(*inputBuffer == NULL)    
        return FALSE;
    return TRUE;
}

/* Extends buffer size by using realloc 
    if succeded returns true, otherwise false */
int extendBuffer(char **inputBuffer, int minimumBufferSize)
{
    /* Check how many times buffer needs to be multiple (at least) */
    int multipleBufferNumber = (minimumBufferSize / BUFFER_SIZE) + 1;
    int newBufferSize = BUFFER_SIZE * multipleBufferNumber * sizeof(char);
    while(newBufferSize < minimumBufferSize)
    {
        multipleBufferNumber+=1;
        newBufferSize = BUFFER_SIZE * multipleBufferNumber * sizeof(char);
    }
    /* Reallocate memory for next chunck of data */
    *inputBuffer = realloc(*inputBuffer, newBufferSize);
    /* Check if memory successfully allocated */
    if(*inputBuffer == NULL)
        return FALSE;
    return TRUE;
}

/* Prints the buffer data to pretty output */
void printBuffer(char *inputBuffer, int lineLength, int totalCharacters)
{
    printf("Printing buffer\n");
    int i;
    for(i = 0; i < totalCharacters; i++)
    {
        /* Print each char value */
        printf("%c", inputBuffer[i]);
        /* Check if got to line limit, if so enter new line */
        if((i+1) % lineLength == 0)
            printf("\n");
    }
}

Any help would be great!

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拒绝两难 2025-01-31 01:42:36

在这里,

  *inputBuffer = (int*) calloc(BUFFER_SIZE, sizeof(char)); 

您可以为8个字符保留空间,但是尝试在其中存储8个ints,

为什么不只是一个char*输入袋?由于您现在已经存储的内容

已经解决了 - 看一下,

if (totalCharacters  - MEMORY_SAFE_GAP > (BUFFER_SIZE * bufferExtendsCounter))

我不知道“ memory_safe_gap”的意图是什么,但是它错误地

看了我输入字符8

  if(8 - 4 > 8 * 1)

是错误的情况,所以您不扩展缓冲。

此“安全差距”确保您始终逃跑,如果您的

 if (totalCharacters  >= (BUFFER_SIZE * bufferExtendsCounter))

输出仍然有些乱,您的代码将不再崩溃,但您可能可以解决这个问题。我输入1234567890并得到

Please enter a string:
1234567890
^Z
Printing buffer
12345
678═0

Right here

  *inputBuffer = (int*) calloc(BUFFER_SIZE, sizeof(char)); 

You reserve space for 8 chars but try to store 8 ints in it

Why isnt inputBuffer just a char*? since thats what you are storing

Now you have fixed that - look at this

if (totalCharacters  - MEMORY_SAFE_GAP > (BUFFER_SIZE * bufferExtendsCounter))

I do not know what the intention of the 'MEMORY_SAFE_GAP' is but its wrong

Look at what happens when I input character number 8

  if(8 - 4 > 8 * 1)

is false, so you do not extend the buffer.

This 'SAFE-GAP ensure that you always run off the end, your code no longer crashes if you just have

 if (totalCharacters  >= (BUFFER_SIZE * bufferExtendsCounter))

output is still a little garbled but you can probably fix that. I input 1234567890 and got

Please enter a string:
1234567890
^Z
Printing buffer
12345
678═0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文