ISO/IEC 9899:TC2(即C99标准),§7.20.3规定:
如果请求的空间大小为零,则行为是实现定义的:返回空指针,或者行为就像大小是非零值一样,但返回的指针不应用于访问对象.
换句话说,malloc(0)可以返回NULL或有效指针,我可能不会取消引用.
这种行为背后的理由是什么?
并且定义malloc(0)导致UB不是更容易吗?
说我有以下程序
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int * i;
if ((i = malloc(sizeof(int) * 100)) == NULL) {
printf("EROOR: unable to allocate memory \n");
return -1;
}
/* memory is allocated successfully */
/* memory is not free'ed but program terminates */
// free(i);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的程序调用malloc分配一些内存而不调用free取消分配它.程序终止而不取消分配内存.
Valgrind清楚地发现内存泄漏.
<snap>
==14209== HEAP SUMMARY:
==14209== in use at exit: 400 bytes in 1 blocks
==14209== total heap usage: 1 allocs, 0 frees, 400 bytes allocated …Run Code Online (Sandbox Code Playgroud) 可能重复:
如何查找sizeof(指向数组的指针)
我正在学习如何在C中创建动态数组,但遇到了一个我无法弄清楚的问题.
如果我使用代码:
int num[10];
for (int i = 0; i < 10; i++) {
num[i] = i;
}
printf("sizeof num = %li\n sizeof num[0] = %li", sizeof(num), sizeof(num[0]));
Run Code Online (Sandbox Code Playgroud)
我得到输出:
sizeof num = 40
sizeof num[0] = 4
Run Code Online (Sandbox Code Playgroud)
这就是我期望发生的事情.但是,如果我malloc数组的大小如下:
int *num;
num = malloc(10 * sizeof(int));
for (int i = 0; i < 10; i++) {
num[i] = i;
}
printf("sizeof num = %li\n sizeof num[0] = %li", sizeof(num), sizeof(num[0]));
Run Code Online (Sandbox Code Playgroud)
然后我得到输出:
sizeof num = 8
sizeof num[0] = 4 …Run Code Online (Sandbox Code Playgroud) 我在C中有这个结构示例:
typedef struct
{
const char * array_pointers_of_strings [ 30 ];
// etc.
} message;
Run Code Online (Sandbox Code Playgroud)
我需要将此array_pointers_of_strings复制到新数组以进行排序字符串.我只需要复制地址.
while ( i < 30 )
{
new_array [i] = new_message->array_pointers_of_strings [i];
// I need only copy adress of strings
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何通过malloc()分配new_array [i]仅用于字符串的地址?
我想编写一个使用指针创建双数组副本的函数.到目前为止这是我的代码:
#include <stdio.h>
#include <stdlib.h>
double* copy (double *array, int size)
{
double *v=malloc(sizeof(double)*size);
for (int i=0; i<size; i++)
*(v+i)=*(array+i);
return v;
}
int main ()
{
//double array[];
int size;
printf ("size= "); scanf ("%i",&size);
double *array=malloc(sizeof(double)*size);
for (int i=0; i<size; i++)
scanf("%f",&array[i]);
copy(array,size);
free(array);
}
Run Code Online (Sandbox Code Playgroud)
我有2个编译错误,我无法摆脱.我明白了
从void*无效转换为double*
当我尝试使用malloc分配内存但我无法理解我做错了什么.
c代码:
// program break mechanism
// TLPI exercise 7-1
#include <stdio.h>
#include <stdlib.h>
void program_break_test() {
printf("%10p\n", sbrk(0));
char *bl = malloc(1024 * 1024);
printf("%x\n", sbrk(0));
free(bl);
printf("%x\n", sbrk(0));
}
int main(int argc, char **argv) {
program_break_test();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译以下代码时:
printf("%10p\n", sbrk(0));
Run Code Online (Sandbox Code Playgroud)
我收到警告提示:
format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘int’
问题1:为什么?
在我之后malloc(1024 * 1024),程序突破似乎没有改变.
这是输出:
9b12000
9b12000
9b12000
Run Code Online (Sandbox Code Playgroud)
问题2:进程在启动以备将来使用时是否在堆上分配内存?或者编译器改变分配的时间点?否则,为什么?
[更新]摘要:brk()或mmap()
在查看TLPI并检查手册页(在TLPI的作者的帮助下)之后,现在我了解了如何malloc()决定使用brk()或mmap(),如下所示:
mallopt() …
嗨,最近我在网上看到了很多代码(也在SO上),如:
char *p = malloc( sizeof(char) * ( len + 1 ) );
Run Code Online (Sandbox Code Playgroud)
为什么sizeof(char)?这不是必要的,不是吗?或者只是风格问题?它有什么优势?
今天,我出现了面试,面试官问我这个,
- 告诉我的步骤,你将如何设计自己的
free( )功能,用于解除分配所分配的内存.- 它如何比C的默认
free()功能更有效?你能得出什么结论?
我很困惑,想不出设计的方式.
你们觉得怎么样 ?
编辑:既然我们需要知道如何malloc()工作,你能告诉我编写自己的malloc()功能的步骤
我正在努力将许多C程序从Unix转换为Linux,这种free()语法引起了我的注意:
free((char *) area );
Run Code Online (Sandbox Code Playgroud)
这和之间有什么区别free( area );?
请考虑以下示例代码:
class C
{
public:
int* x;
};
void f()
{
C* c = static_cast<C*>(malloc(sizeof(C)));
c->x = nullptr; // <-- here
}
Run Code Online (Sandbox Code Playgroud)
如果由于任何原因我不得不忍受未初始化的内存(当然,如果可能的话,我会打电话new C()),我仍然可以调用放置构造函数.但是,如果我省略这一点,如上所述,并手动初始化每个成员变量,是否会导致未定义的行为?即绕过构造函数本身未定义的行为,或者用类外的一些等效代码替换调用它是否合法?
(通过另一个完全不同的问题遇到这个问题;要求好奇......)