简体   繁体   English

c malloc导致崩溃

[英]c malloc cause a crash

Why the 3 malloc above crashes? 为什么上面的3个malloc崩溃? sometimes they work but only for (globale->dim_schema) > 10 or (globale->dim_schema) >100 有时它们起作用,但仅适用于(globale-> dim_schema)> 10或(globale-> dim_schema)> 100

struct GLOBALE {

    int dim_schema;
    char *schema;
    int *celle_usate;
    char *punteggi;
    char *percorso_aiuto;
    struct LISTA_SOLUZIONI *soluzioni; 
};

typedef struct GLOBALE *struct_globale;

void modalita_interattiva() {

    int i;
    char lettera;

    char bonus;
    char *parola;
    struct_globale globale;
    globale = malloc(sizeof(struct_globale));

    if(globale == NULL) {

        printf("Impossibile creare struct globale\n");
        exit(EXIT_FAILURE);

    globale->soluzioni = NULL;

    do{
        printf("Quanto grande e' lo schema di ruzzle che vuoi usare? (>0)\n");
        scanf("%d", &(globale->dim_schema));
        printf("Dimensione: %d \n", globale->dim_schema);
    }while(globale->dim_schema<=0);

    globale->celle_usate = malloc(globale->dim_schema * globale->dim_schema * sizeof(int)); <----CRASH
    printf("celle usate\n");
    globale->punteggi = malloc((globale->dim_schema) * (globale->dim_schema) * sizeof(char)); <----CRASH
    printf("punteggi\n");
    globale->schema = malloc(globale->dim_schema * globale->dim_schema * sizeof(char));<----CRASH
    printf("schema\n"); 
...etc etc

This is a good example of why it's not a good idea to hide pointers within a typedef : 这是一个很好的例子,说明为什么在typedef隐藏指针不是一个好主意:

globale = malloc(sizeof(struct_globale));

struct_globale is a typedef for struct GLOBALE * . struct_globalestruct GLOBALE *的typedef。 So the above allocation only allocates enough space for a pointer to struct GLOBALE (typically 4 or 8 bytes depending on the machine / compiler). 因此,以上分配仅为用于struct GLOBALE的指针分配了足够的空间(通常为4或8个字节,具体取决于机器/编译器)。 Since the struct is larger than this, you're writing to members that are past the memory offset of the allocated size. 由于该结构大于此结构,因此您将写入超出分配大小的内存偏移量的成员。 This results in undefined behavior. 这导致未定义的行为。

You need to allocate space for the size of the struct: 您需要为结构的大小分配空间:

globale = malloc(sizeof(struct GLOBALE));

Or alternaltely: 或完全是:

globale = malloc(sizeof(*globale));

The function call 函数调用

malloc(sizeof(struct_globale))

returns only a region of memory the size of the pointer struct_globale , not the size of the record it refers to. 仅返回指针struct_globale大小的内存区域,而不返回它引用的记录的大小。 Obviously, memory allocation with malloc and its cousins is rather error prone. 显然,使用malloc及其表亲进行内存分配非常容易出错。 However, it can be improved by introducing the following function macros: 但是,可以通过引入以下函数宏来进行改进:

#define NEW_ARRAY(ptr, n) (ptr) = malloc((n) * sizeof (ptr)[0])
#define NEW(ptr) NEW_ARRAY((ptr), 1)

With these in place you can simply say 有了这些,您可以简单地说

NEW(globale);
NEW_ARRAY(globale->celle_usate, globale->dim_schema * globale->dim_schema);

etc. 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM