簡體   English   中英

段故障,malloc char指針

[英]Seg Fault, malloc char pointers

我不斷收到分段錯誤,我從char指針知道它。 但是我不知道為什么嗎?

Whiskey* createWhiskey(int a, double p, char* n){

    Whiskey* whiskey = malloc(sizeof(Whiskey));
    whiskey->age = a;
    whiskey->proof = p;
    whiskey->name = malloc((strlen(n)+1) * sizeof(char));
    strcpy(whiskey->name, n);
    return whiskey;
}
int main(){

    Whiskey* burbon;
    burbon = createWhiskey(12, 90.0, "MakersMark");

    free(burbon);

    return 0;
}

在Alex的評論(見下文)中,添加了以下信息:

typedef struct{ int age; double proof; char* name; }Whiskey;

正如評論中所討論的,所示程序很好。

但是,您應該添加一些檢查以避免出現問題。 就像是:

typedef struct{ int age; double proof; char* name; } Whiskey;

Whiskey* createWhiskey(int a, double p, char* n){
    Whiskey* whiskey = malloc(sizeof(Whiskey));
    if (whiskey) 
    {
        whiskey->age = a;
        whiskey->proof = p;
        if (strlen(n) > SOME_MAXIMUM)
        {
            free(whiskey);
            printf("Some error... maybe\n");
            return NULL;
        }
        whiskey->name = malloc((strlen(n)+1) * sizeof(char));
        if (whiskey->name)
        {
            strcpy(whiskey->name, n);
        }
        else
        {
            free(whiskey);
            printf("Some error... \n");
            return NULL;
        }
    }
    return whiskey;
}

int main(){

    Whiskey* burbon;
    burbon = createWhiskey(12, 90.0, "MakersMark");
    if (!burbon)
    {
        printf("Some error... \n");
    }    

    // code....

    if (burbon)
    {
        free( burbon->name);
        free(burbon);
    }
    return 0;
}

我希望您的威士忌酒結構定義正確。 以下代碼對我來說很好用:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct Whisk {
        int age;
        double proof;
        char *name;
} Whiskey;
Whiskey* createWhiskey(int a, double p, char* n){

    Whiskey* whiskey = malloc(sizeof(Whiskey));
    whiskey->age = a;
    whiskey->proof = p;
    whiskey->name = malloc((strlen(n)+1) * sizeof(char));
    strcpy(whiskey->name, n);
    return whiskey;
}

int main(){

    Whiskey* burbon;
    burbon = createWhiskey(12, 90.0, "MakersMark");
    if (!burbon)
    {
        printf("Some error... \n");
    }

    // code....

    if (burbon)
    {
        free( burbon->name);
        free(burbon);
    }
    return 0;
}

以下代碼

  1. 干凈地編譯
  2. 執行適當的錯誤檢查
  3. 運行時不分段故障
  4. 執行所需的功能
  5. 通過將兩個分配的內存指針都傳遞給free()來正確清理
  6. 為傳遞的參數使用有意義的名稱

編碼:

#include <string.h>  // malloc()
#include <stdlib.h>  // exit(), EXIT_FAILURE
#include <stdio.h>   // perror()

typedef struct
{
    int age;
    double proof;
    char* name;
} Whiskey;

Whiskey* createWhiskey(int age, double proof, char* name)
{

    Whiskey* whiskey = NULL;
    if( NULL == (whiskey = malloc(sizeof(Whiskey)) ) )
    { // then, malloc failed
        perror( "malloc for Whiskey failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    whiskey->age = age;
    whiskey->proof = proof;
    whiskey->name = NULL;

    if( NULL == (whiskey->name = malloc( strlen(name)+1) ) )
    { // then malloc failed
        perror( "malloc for name field failed" );
        free( whiskey );
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    strcpy(whiskey->name, name);
    return whiskey;
} // end function: createWhiskey


int main( void )
{

    Whiskey* burbon;
    burbon = createWhiskey(12, 90.0, "MakersMark");

    free( burbon->name );
    free( burbon );

    return 0;
} // end function: main

暫無
暫無

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

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