char* malloc
时间: 2023-10-22 19:07:16 浏览: 40
(char) is a function in the C programming language that allocates a block of memory of a specified size and returns a pointer to the first byte of the allocated memory. The memory allocated by malloc is not initialized, meaning that the contents of the memory block are undefined and may contain garbage values. The function takes a single argument, which is the number of bytes to allocate. The return type of malloc is a pointer to void, which can be cast to any other pointer type as needed. The memory allocated by malloc should be freed using the free() function when it is no longer needed.
相关问题
too many arguments to function 'char* malloc()'
This error message typically occurs when you are trying to allocate memory using the malloc() function and you are passing too many arguments to the function.
The malloc() function takes only one argument, which is the size of the memory block you want to allocate in bytes. If you pass more than one argument, you will get this error.
For example, if you try to allocate memory like this:
```c
char* str = malloc(100, sizeof(char));
```
You will get the "too many arguments" error because you have passed two arguments to the malloc() function.
To fix this error, simply pass only the size argument to the malloc() function like this:
```c
char* str = malloc(100 * sizeof(char));
```
This will allocate 100 bytes of memory for the string.
c++.char ** a;a = (char**)malloc(sizeof(char**));
你创建了一个指向指针的指针 `a`,并使用 `malloc` 函数为其分配了内存空间。根据你的代码,你分配了 `sizeof(char**)` 字节的内存空间,但是通常情况下应该使用 `sizeof(char*)` 来分配指针所占的内存空间。另外,你也可以使用 `calloc` 函数来分配内存并初始化为零。
正确的写法应该是:
```c
char **a;
a = (char**)malloc(sizeof(char*));
```
或者使用 `calloc` 函数:
```c
char **a;
a = (char**)calloc(1, sizeof(char*));
```
记得在使用完 `a` 后,要调用 `free(a)` 来释放之前分配的内存空间,以避免内存泄漏。