Malloc calloc 分配结构失败

Malloc calloc fails to allocate structure

我使用 malloc/calloc 的以下代码获得空内存。有时它无法为“name1”分配内存,然后 strcpy 失败。请指导。

struct testMalloc
{
    char name1[90];
    char name2[90]; 
    struct testMalloc* ptr;
};
int main(int argc, char* argv[])
{
    struct testMalloc* test = 0 ;   
    int size = 0;
    size = sizeof(struct testMalloc);

    printf("Size of struct is %d", size);

    test = (struct testMalloc*) calloc(sizeof(struct testMalloc));  
    
    strcpy((test->name1), "hdshdssdsdfsfffffffffffffffffffffffffffffh");

    return 0;

}

calloc 有两个参数:元素的数量和每个元素的大小。它会将分配的内存归零。您正在寻找的是 malloc,它只需要 1 个参数:分配的内存块的总大小,并且不会将分配的内存清零。

您不包含 <stdlib.h> 以使编译器知道 calloc 的签名,在这种情况下它使用 K&R 调用约定。

如果您包含 <stdlib.h>,代码将无法编译,无法正确调用 calloc

语法错误很少:

  1. struct testMalloc* test = NULL; NULL 指针是这样初始化的
  2. calloc(sizeof(struct testMalloc)); 传递给 calloc 的参数太少。 正确的形式是 calloc(no_of_elements, sizeof(type));

以下是您的代码的正确实现:

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

struct testMalloc
{
    char name1[90];
    char name2[90]; 
    struct testMalloc* ptr;
};

int main(int argc, char* argv[])
{
    struct testMalloc* test = NULL;
    size_t size = sizeof(struct testMalloc);

    printf("Size of struct is %ld\n", size);

    if((test = (struct testMalloc*)calloc(1, size)) == NULL){
        return -1; //Failed to allocate memory
    }
    else {
        strcpy((test->name1),"hdshdssdsdfsfffffffffffffffffffffffffffffh");
        printf("%s\n",test->name1);
    }
    
    return 0;
}

从上到下:

1.

您忘记 #include stdlib.h 使用 calloc()malloc()。自 C99 起禁止隐式声明。

2.

int main(int argc, char* argv[])

您的程序不需要将参数输入其中。

这个:

int main (void)

会更合适。

3.

int size = 0;

size 永远不应该有负值。所以声明为 unsigned int 甚至更好 size_t.

会更合适

4.

struct testMalloc* test = 0 ; 

您可以使用0 来初始化一个指针。它是完全有效的,因为 0 是一个 空指针常量 。但在处理指针时最好使用 NULL 而不是 0 以显示指针意图并增加可读性。

struct testMalloc* test = NULL; 

5.

calloc(sizeof(struct testMalloc)); 
malloc 相比,

calloc 需要两个参数。第一个需要是项目数,第二个是一个项目的大小。

calloc(sizeof(1,struct testMalloc)); 

6.

test = (struct testMalloc*) calloc(sizeof(struct testMalloc)); 

您不需要转换 malloc()calloc() 的 return 值。

  • Do I cast the result of malloc?

7.

如果分配失败,您忘记检查从 calloc() 指向的 returned 是否存在空指针。始终检查 memory-management 函数的 return 值。

test = calloc(1, sizeof(struct testMalloc)); 
if (test == NULL)
{
    fputs("Allocation failed!", stderr);
    // error routine.
}
  1. 始终检查 malloc 的结果。
  2. 使用对象而不是 sizeof
  3. 中的类型
  4. 尝试使用更安全的字符串函数版本(在此示例中,传递的字符串比数组长。
  5. 您不必转换 malloc 的结果,这被认为是不好的做法(现在已经过时但无论如何)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct testMalloc
{
    char name1[20];
    char name2[90]; 
    struct testMalloc* ptr;
};

struct testMalloc *allocAndCopy(const char *str)
{
    struct testMalloc *ptr = malloc(sizeof(*ptr));
    if(ptr)
    {
        strncpy(ptr -> name1, str, sizeof(ptr -> name1));
        ptr -> name1[sizeof(ptr -> name1) - 1] = 0;
    }
    return ptr;
}


int main(int argc, char* argv[])
{
    struct testMalloc* test = allocAndCopy("hdshdssdsdfsfffffffffffffffffffffffffffffh");

    if(test) printf("the string is: %s\n", test -> name1);
}

https://godbolt.org/z/zjvvYW