繁体   English   中英

在C中从字符串转换为整数时发生内存泄漏(?)

[英]Memory leak (?) when converting from string to integer in C

因此,我编写了此程序,该程序从名为“ input.txt”的文本文件中读取两行文本(它有两行,每行一个数字),然后将两个数字转换为两个不同数组中的整数。 对于整数数组,我使用malloc和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));


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

您可以使用atoi而不是创建新的weel来简化程序,但是我看到的唯一区别是

int firstlen = strlen(num_first)-1; 代替

int firstlen = strlen(num_first);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM