c - 在 C 中从字符串转换为整数时出现内存泄漏(?)

标签 c memory-leaks malloc realloc

所以我编写了这个程序,它从名为“input.txt”的文本文件中读取两行文本(它有两行,每行一个数字),然后将这两个数字转换为两个不同数组中的整数。对于整数数组,我使用 malloc 和 realloc 来动态分配内存。正如您在下图中看到的,它对于第二个数字效果很好,但第一个数字只是一些随机数字(每次都会改变)。为什么会发生这种情况?

(我正在使用代码块。)

enter image description here

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define SIZE_MAX 10
#define SIZE_USE (SIZE_MAX-1)

int input(char num_first[], char num_second[]);
int convert(int iinum_first[], int iinum_second[], char num_first[], char num_second[], int firstlen, int secondlen, int c, int c2);

int main()
{

    char num_first[SIZE_MAX]; // original input as string
    char num_second[SIZE_MAX];

    input(num_first, num_second);

    int firstlen = strlen(num_first)-1;
    int secondlen = strlen(num_second);

    int i, c=0, c2=0;
    for (i = 0; i <= firstlen; i++)
    {
        c++; // counts number of elements needed to resize array
    }
    for (i = 0; i <= secondlen; i++)
    {
        c2++;
    }

    int *iinum_first = NULL; // converted integer input
    int *iinum_second = NULL;

    iinum_first = (int*)malloc(sizeof(int));
    iinum_second = (int*)malloc(sizeof(int));


    convert(iinum_first, iinum_second, num_first, num_second, firstlen, secondlen, c, c2);

    printf("first integer: ");
    for (i = 0; i < firstlen; i++) // print first integer
    {
        printf("%d", iinum_first[i]);
    }
    printf("\nsecond integer: ");
    for (i = 0; i < secondlen; i++) // print second integer
    {
        printf("%d", iinum_second[i]);
    }
    puts("");

    return 0;
}

int input(char num_first[], char num_second[])
{
    FILE *fPTR;
    int i;

    if ((fPTR = fopen("input.txt", "r")) == NULL)
    {
        puts(":( File could not be opened.");
    }
    else
    {
        if (fgets(num_first, SIZE_MAX, fPTR) != NULL)
            printf("first string: "); // print string input
        puts(num_first);
        if (fgets(num_second, SIZE_MAX, fPTR) != NULL)
            printf("second string: ");
        puts(num_second);
        fclose(fPTR);
    }

    return 0;
}

int convert(int iinum_first[], int iinum_second[], char num_first[], char num_second[], int firstlen, int secondlen, int c, int c2)
{
    // dynamic memory allocation

    int i;

    int *temp = NULL;
    int *temp2 = NULL;

    temp = (int*)realloc(iinum_first, c * sizeof(int));
    if (temp != NULL)
    {
        iinum_first = temp; // moving temporary data to main array
    }
    else
    {
        free(iinum_first);
        printf("Error allocating memory!\n");
        return 1;
    }


    temp2 = (int*)realloc(iinum_second, c2 * sizeof(int));
    if (temp2 != NULL)
    {
        iinum_second = temp2; // moving temporary data to main array
    }
    else
    {
        free(iinum_second);
        printf("Error allocating memory!\n");
        return 1;
    }

    for (i = 0; num_first[i] != '\0'; i++)
    {
        switch (num_first[i])
        {
        case 48:
            iinum_first[i] = 0;
            break;
        case 49:
            iinum_first[i] = 1;
            break;
        case 50:
            iinum_first[i] = 2;
            break;
        case 51:
            iinum_first[i] = 3;
            break;
        case 52:
            iinum_first[i] = 4;
            break;
        case 53:
            iinum_first[i] = 5;
            break;
        case 54:
            iinum_first[i] = 6;
            break;
        case 55:
            iinum_first[i] = 7;
            break;
        case 56:
            iinum_first[i] = 8;
            break;
        case 57:
            iinum_first[i] = 9;
            break;
        }
    }

    for (i = 0; num_second[i] != '\0'; i++)
    {
        switch (num_second[i])
        {
        case 48:
            iinum_second[i] = 0;
            break;
        case 49:
            iinum_second[i] = 1;
            break;
        case 50:
            iinum_second[i] = 2;
            break;
        case 51:
            iinum_second[i] = 3;
            break;
        case 52:
            iinum_second[i] = 4;
            break;
        case 53:
            iinum_second[i] = 5;
            break;
        case 54:
            iinum_second[i] = 6;
            break;
        case 55:
            iinum_second[i] = 7;
            break;
        case 56:
            iinum_second[i] = 8;
            break;
        case 57:
            iinum_second[i] = 9;
            break;
        }
    }

    return 0;
}

最佳答案

我修好了。在调用转换函数之前,我必须在 main 中使用 realloc。

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define SIZE_MAX 10
#define SIZE_USE (SIZE_MAX-1)

int input(char num_first[], char num_second[]);
int convert(int iinum_first[], int iinum_second[], char num_first[], char num_second[], int firstlen, int secondlen, int c, int c2);

int main()
{

    char num_first[SIZE_MAX]; // original input as string
    char num_second[SIZE_MAX];

    input(num_first, num_second);

    int firstlen = strlen(num_first)-1;
    int secondlen = strlen(num_second);

    int i, c=0, c2=0;
    for (i = 0; i <= firstlen; i++)
    {
        c++; // counts number of elements needed to resize array
    }
    for (i = 0; i <= secondlen; i++)
    {
        c2++;
    }

    int *iinum_first = NULL; // converted integer input
    int *iinum_second = NULL;

    iinum_first = (int*)malloc(sizeof(int));
    iinum_second = (int*)malloc(sizeof(int));

    int *temp = NULL;
    int *temp2 = NULL;

    temp = (int*)realloc(iinum_first, c * sizeof(int));
    if (temp != NULL)
    {
        iinum_first = temp; // moving temporary data to main array
    }
    else
    {
        free(iinum_first);
        printf("Error allocating memory!\n");
        return 1;
    }


    temp2 = (int*)realloc(iinum_second, c2 * sizeof(int));
    if (temp2 != NULL)
    {
        iinum_second = temp2; // moving temporary data to main array
    }
    else
    {
        free(iinum_second);
        printf("Error allocating memory!\n");
        return 1;
    }

    convert(iinum_first, iinum_second, num_first, num_second, firstlen, secondlen, c, c2);

    printf("first integer: ");
    for (i = 0; i < firstlen; i++) // print first integer
    {
        printf("%d", iinum_first[i]);
    }
    printf("\nsecond integer: ");
    for (i = 0; i < secondlen; i++) // print second integer
    {
        printf("%d", iinum_second[i]);
    }
    puts("");

    return 0;
}

int input(char num_first[], char num_second[])
{
    FILE *fPTR;
    int i;

    if ((fPTR = fopen("input.txt", "r")) == NULL)
    {
        puts(":( File could not be opened.");
    }
    else
    {
        if (fgets(num_first, SIZE_MAX, fPTR) != NULL)
            printf("first string: "); // print string input
        puts(num_first);
        if (fgets(num_second, SIZE_MAX, fPTR) != NULL)
            printf("second string: ");
        puts(num_second);
        fclose(fPTR);
    }

    return 0;
}

int convert(int iinum_first[], int iinum_second[], char num_first[], char num_second[], int firstlen, int secondlen, int c, int c2)
{
    // dynamic memory allocation
    int i;

    for (i = 0; num_first[i] != '\0'; i++)
    {
        switch (num_first[i])
        {
        case 48:
            iinum_first[i] = 0;
            break;
        case 49:
            iinum_first[i] = 1;
            break;
        case 50:
            iinum_first[i] = 2;
            break;
        case 51:
            iinum_first[i] = 3;
            break;
        case 52:
            iinum_first[i] = 4;
            break;
        case 53:
            iinum_first[i] = 5;
            break;
        case 54:
            iinum_first[i] = 6;
            break;
        case 55:
            iinum_first[i] = 7;
            break;
        case 56:
            iinum_first[i] = 8;
            break;
        case 57:
            iinum_first[i] = 9;
            break;
        }
    }

    for (i = 0; num_second[i] != '\0'; i++)
    {
        switch (num_second[i])
        {
        case 48:
            iinum_second[i] = 0;
            break;
        case 49:
            iinum_second[i] = 1;
            break;
        case 50:
            iinum_second[i] = 2;
            break;
        case 51:
            iinum_second[i] = 3;
            break;
        case 52:
            iinum_second[i] = 4;
            break;
        case 53:
            iinum_second[i] = 5;
            break;
        case 54:
            iinum_second[i] = 6;
            break;
        case 55:
            iinum_second[i] = 7;
            break;
        case 56:
            iinum_second[i] = 8;
            break;
        case 57:
            iinum_second[i] = 9;
            break;
        }
    }

    return 0;
}

关于c - 在 C 中从字符串转换为整数时出现内存泄漏(?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29501396/

相关文章:

C 或 C++ 堆内存管理实现

c - atoi() 练习的问题

c - 如何在 C 中用另一个数组移动字符位置?

c++ - 分配的结构包含 vector 的内存泄漏

memory-leaks - ServiceStack Funq Container WeakReference 泛滥

linux - 为什么当我运行一个不断分配内存的进程时,Linux Free 命令没有显示更少的可用内存

c - 如何检查多个 mallocs 并在出错时释放它们?

c - main 的 ret 指令在哪里

c - 如何在c中打印 float 的二进制值

memory-leaks - 在 Lua 中丢失引用