简体   繁体   English

段故障,malloc char指针

[英]Seg Fault, malloc char pointers

I keep getting a segmentation fault and i know its from the char pointer. 我不断收到分段错误,我从char指针知道它。 But I cant figure out why? 但是我不知道为什么吗?

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

In a comment from Alex (see below) the following information is added: 在Alex的评论(见下文)中,添加了以下信息:

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

As discussed in comments the program shown is fine. 正如评论中所讨论的,所示程序很好。

However, you should add some checks to avoid problems. 但是,您应该添加一些检查以避免出现问题。 Something like: 就像是:

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

I hope definition of your Whiskey structure is fine. 我希望您的威士忌酒结构定义正确。 Following code works fine for me: 以下代码对我来说很好用:

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

the following code 以下代码

  1. cleanly compiles 干净地编译
  2. performs appropriate error checking 执行适当的错误检查
  3. does not seg fault when run 运行时不分段故障
  4. performs the desired function 执行所需的功能
  5. cleans up properly by passing both allocated memory pointers to free() 通过将两个分配的内存指针都传递给free()来正确清理
  6. uses meaningful names for the passed parameters 为传递的参数使用有意义的名称

the code: 编码:

#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