簡體   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