簡體   English   中英

malloc:錯誤檢查和釋放內存

[英]malloc: error checking and freeing memory

我正在使用malloc來判斷是否可以為特定陣列z1分配內存。 ARRAY_SIZE是預定義的數值。 我使用鑄造,因為我讀過它是安全的。

long double *z1 = (long double *)malloc(sizeof (long double) * ARRAY_SIZE);  
if(z1 == NULL){  
   printf("Out of memory\n");  
   exit(-1);  
}

以上只是我的代碼片段,但是當我添加錯誤檢查部分(包含在上面的if語句中)時,我在visual studio 2008中遇到了很多編譯時錯誤。正是這個錯誤檢查部分產生了所有的錯誤。 我究竟做錯了什么?

在malloc的一個相關問題上,我知道在使用變量/數組z1之后需要釋放/釋放內存。 對於數組z1,我使用:

free(z1);
z1 = NULL;

第二行z1 = NULL必要?

我得到102個錯誤......好吧MVS2008在102處停止錯誤。錯誤類型如下:

error C2143: syntax error : missing ';' before 'type'  
error C2065: 'L' : undeclared identifier
// this error repeats for all my identifiers

並且這指向if語句中的結束}之后。

ARRAY_SIZE非常大。 我把它定義為

#define ARRAY_SIZE 2500001

我上面的代碼太長了。 但是我有一個較小的代碼,它給了我相同的行為。 抱歉格式化。 我似乎無法做對。

#include stdio.h //note I have the actual < > in my code
#include stdlib.h
#include math.h
#define ARRAY_SIZE 11
#define VECTOR_SIZE 5

main()
{
    long double *z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
    if(z == NULL){
        printf("Out of memory\n");
        exit(-1);
    }

    long double *k = (long double*) malloc(sizeof (long double) * VECTOR_SIZE);
    int i;
    long double A, B;
    void f(long double fa[], long double fb[], long double fA, long double fB);

    A = 0.5;
    B = 2;

    for(i = 0; i < VECTOR_SIZE; i++){
        k[i] = 0;
    }

    k[1] = 4;
    k[2] = 8;

    for(i = 0; i < ARRAY_SIZE; i++){
        z[i] = 0;
    }

    z[1] = 5;


    f(k, z, A, B);

    free(z);
    free(k);
    z = NULL;
    k = NULL;
}


void f(fa, fb, fA, fB)
long double fa[], fb[], fA, fB;
{
    fa[0] = fb[1]* fA;
    fa[1] = fa[1] - 1;
    fb[0] = 2* fB - fa[2];
    printf("fa[2] is 8 and is the same as *[fa + 2] and is  %3.3Le\n", *(fa + 2));
    printf("\nAddress of &fa[2] is %x\n", &fa[2]);
    printf("same address is fa + 2 is %x\n", fa + 2);
    return;
}

代碼中的問題

好吧。 現在您已經提供了所有代碼,因此更容易解釋您的問題:

  1. 您正試圖在函數的“中間”定義變量。 C不允許這樣做 你必須在開始時定義所有變量。 這就是給你的東西
    error C2143: syntax error : missing ';' before 'type'
    錯誤。
  2. 函數聲明也是如此(需要位於函數的頂部)。

因此,將代碼更改為以下代碼可以使其工作:

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

#define ARRAY_SIZE 11
#define VECTOR_SIZE 5

main() {
    void f(long double fa[], long double fb[], long double fA, long double fB);

    long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
    long double* k = (long double*) malloc(sizeof (long double) * VECTOR_SIZE);
    int i;
    long double A, B;

    if (z == NULL) {
        printf("Out of memory\n");
        exit(-1);
    }

    A = 0.5;
    B = 2;

    for (i = 0; i < VECTOR_SIZE; i++) {
        k[i] = 0;
    }

    k[1] = 4;
    k[2] = 8;

    for (i = 0; i < ARRAY_SIZE; i++) {
        z[i] = 0;
    }

    z[1] = 5;

    f(k, z, A, B);

    free(z);
    free(k);
    z = NULL;
    k = NULL;
}

void f(fa, fb, fA, fB)  
long double fa[], fb[], fA, fB;  
{
    fa[0] = fb[1]* fA;
    fa[1] = fa[1] - 1;
    fb[0] = 2* fB - fa[2];

    printf("fa[2] is 8 and is the same as *[fa + 2] and is  %3.3Le\n", *(fa + 2));
    printf("\nAddress of &fa[2] is %x\n", &fa[2]);
    printf("same address is fa + 2 is %x\n", fa + 2);

    return;
}


其他幾點

現在我將添加一些提示,可能不是嚴格的錯誤(意思是,它們仍然編譯......),但不是很好的編碼實踐:

  1. 正如我之前所說,使用const來定義常量而不是#define
  2. 正確定義main() - 即int main() {...而不僅僅是main()而沒有返回類型。 它在C中工作,但在C ++中不起作用,我認為它很糟糕。 (為什么我本來應該假設函數返回int,如果沒有其他的說?為什么不能無效?)
  3. 接下來,您應該顯式地從main()返回一個值。
  4. 我更喜歡聲明void f(long double fa[], long double fb[], long double fA, long double fB); main()之外的函數原型。
  5. 定義函數時使用現代語法 - 您在原型中使用的語法 - 而不是古代語法:
    void f(fa, fb, fA, fB)
    long double fa[], fb[], fA, fB;
    {
    應該成為:
    void f(long double fa[], long double fb[], long double fA, long double fB) {

這樣你的代碼就變成了:

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

void f(long double fa[], long double fb[], long double fA, long double fB);

int main() {
    const int ARRAY_SIZE = 11;
    const int VECTOR_SIZE = 5;

    long double* z = (long double*) malloc(sizeof (long double) * ARRAY_SIZE);
    long double* k = (long double*) malloc(sizeof (long double) * VECTOR_SIZE);

    int i;
    long double A, B;

    if (z == NULL) {
        printf("Out of memory\n");
        exit(-1);
    }

    A = 0.5;
    B = 2;

    for (i = 0; i < VECTOR_SIZE; i++) {
        k[i] = 0;
    }

    k[1] = 4;
    k[2] = 8;

    for (i = 0; i < ARRAY_SIZE; i++) {
        z[i] = 0;
    }

    z[1] = 5;

    f(k, z, A, B);

    free(z);
    free(k);
    z = NULL;
    k = NULL;

    return 0;
}

void f(long double fa[], long double fb[], long double fA, long double fB) {
    fa[0] = fb[1]* fA;
    fa[1] = fa[1] - 1;
    fb[0] = 2* fB - fa[2];

    printf("fa[2] is 8 and is the same as *[fa + 2] and is  %3.3Le\n", *(fa + 2));
    printf("\nAddress of &fa[2] is %x\n", &fa[2]);
    printf("same address is fa + 2 is %x\n", fa + 2);

    return;
}  

我覺得哪個更好。


第一篇文章

請提供您的所有代碼。 我在Visual C ++ 2008 Express上測試了以下代碼,禁用了“語言擴展”和4級警告。 它工作得很好:

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

int main() {
    const int ARRAY_SIZE = 1024*1024;

    long double *z1 = (long double *)malloc(sizeof (long double) * ARRAY_SIZE);
    if (z1 == NULL) {
        printf("Out of memory\r\n");
        exit(-1);
    }

    printf("Awesome!\r\n");

    return 0;
}

也許你忘記了一個包含,也許你做了別的錯誤。 代碼片段本身似乎完全沒問題。 您描述的第二個錯誤似乎完全不相關: error C2065: 'L' : undeclared identifier // this error repeats for all my identifiers

順便說一句,更喜歡const不是#define

嘗試#include-ing stdio.h和stdlib.h以確保實際定義了NULL。

並回答你的第二個問題,設置z1為NULL是沒有必要的,雖然它可以幫助你確保你沒有無意中嘗試使用z1后,因為取消引用空指針將崩潰。 所以這是一個很好的防御性的事情,但不是必需的。

如果分配了z1您可能需要檢查代碼的其他位置。 將其設置為NULL是告訴內存未分配給指針的好方法。

暫無
暫無

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

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