簡體   English   中英

如果您不釋放函數中動態分配的內存會怎樣?

[英]What happens if you don't free dynamically allocated memory in a function?

我正在學習如何在C語言中編寫函數以接受數組並返回修改后的數組。

在函數testfunc (應該簡單地將10添加到輸入數組b每個元素中)中,我正在使用mallocnpts個整數分配內存。 但是由於我想使用指針返回此數組,所以我沒有在函數末尾釋放此內存。 假設我像在代碼中一樣調用此函數100次,那么在代碼期間分配的所有內存又會如何? 代碼使用的內存量是100*10*4字節嗎? 對於在動態內存分配上不起作用的函數,我認為分配給變量的內存會在該函數返回最終值時消失,並且在再次調用該函數時,它將再次分配內存,依此類推。 但是我對這種情況下發生的事情感到困惑。

我無法釋放函數內部分配的內存,因為我需要它將數組返回到主函數,但是我還需要為不同的數組調用此函數100次以上,因此,如果它不斷反復分配,它將內存不足

有沒有一種方法可以檢查代碼正在使用多少內存? (而不是在Mac-OSX上查看“活動監視器”)。

謝謝 !

/* code to test returning array from functions */

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

int* testfunc(int *a,int npts);

int main(int argc, char* argv[])
{
    int *b;
    int a[10],i,j,npts=10;

    b=(int *) malloc(sizeof(int)*npts);

    for (j=0; j <100; j++)
    {
        printf("iteration number %d \n",j);
        for (i=0; i<npts; i++)
        {
            a[i]=i;
            printf("%d \n",a[i]);
        }

        b=testfunc(a,npts);

        printf("returned array \n");
        for (i=0; i<npts; i++)
        {
            printf("%d \n",b[i]);
        }
    }
    printf("the size of one integer is %d \n",sizeof(int));

    return 0;
}
int* testfunc(int *b,int npts)
{
    int *c;
    int i=0;

    c=(int *) malloc(sizeof(int)*npts);

    for (i=0; i<npts; i++)
    {
        c[i]=b[i]+10;
    }

    return c;
}

這是避免在函數內部分配內存並能夠多次調用該函數的可能解決方案

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

void testfunc(int *c,int *d,int npts);

int main(int argc, char* argv[])
{
    int *a,*b;
    int i,j,npts=10;

    a=malloc(sizeof(int)*npts);
    b=malloc(sizeof(int)*npts);

    for (j=0; j <100; j++)
    {
        printf("iteration number %d \n",j);
        for (i=0; i<npts; i++)
        {
            a[i]=i;
            printf("%d \n",a[i]);
        }

        testfunc(a,b,npts);

        printf("returned array \n");
        for (i=0; i<npts; i++)
        {
            printf("%d \n",b[i]);
        }
    }
    printf("the size of one integer is %d \n",sizeof(int));

    free(a);
    free(b);

    return 0;
}
void testfunc(int *c,int *d,int npts)
{
    int i=0;

    for (i=0; i<npts; i++)
    {
        d[i]=c[i]+10;
    }
}
c=(int *) malloc(sizeof(int)*npts);
:
return c;

這樣既可以傳回內存, 可以管理內存。

完成后釋放它已成為調用者(此cae中的main )的責任。

您真正的問題所在在main

b=(int *) malloc(sizeof(int)*npts);
:
for (some number of iterations)
    b=testfunc(a,npts);  // overwrites b

在該“覆蓋”行上,實際上是發生了內存泄漏,因為您無法訪問當前為b分配的內存(無論是原始的還是在循環的先前迭代中)。


而且,順便說一句,請不要投的返回值malloc在C.這是沒有必要的,可以隱藏你真的希望有調試:-)某些細微的錯誤

由於您正在使用malloc ,這意味着您是從而不是堆棧中分配內存。
首先應該弄清楚堆和棧的機制。在C語言中, malloc將幫助程序員從堆中分配內存,這與C ++中的新功能相同。
因此,即使函數返回最終值,分配的內存也不會被釋放,如果您調用100次,它將為您分配100次內存。
至於工具,您可以參考Valgrind ,這是一個功能強大的工具,可以檢查是否存在內存錯誤。

如果不釋放動態分配的內存,則將導致內存泄漏,並且系統將耗盡內存。 這可能導致程序崩潰。

主要是必須釋放()分配的空間,但是必須確保在函數執行期間不要修改空間的值。

引用調用的目的是修改輸入指針並返回給調用者。 表示修改后的值,即仍與調用方聯系。

您可以簡單地進行修改而無需在函數中分配內存

第二種方法只是糾正

無效的testfunc(int * c,int npts){int i = 0;

for (i=0; i<npts; i++)
{
    c[i] += 10; //YOU JUST INCREMENT VALUE by 10 
}

}

我寫函數的方法:

  1. 沒有內存分配
  2. 沒有多個參數
  3. 保留必要的輸入,輸出參數,返回值
  4. 這里int * c,npts-輸入參數,輸出[您在int * c位置修改的值]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM