#include
#include
void Getmemory(char* p)
{
p = (char*)malloc(100);
}
void Test(void)
{
char* str = NULL;
GetMemory(str);
strcpy(str, "hello world\n");
printf(str);
}
int main()
{
Test();
return 0;
}
请问运行Test 函数会有什么样的结果?
这段代码主要存在两个问题:
1.对NULL指针进行了解引用操作,程序会崩溃(这里主要指在strcpy内部需要对传入的两个参数解引用,而第一个传入的参数是str为NULL,即对NULL解引用)
2.malloc申请空间后,没有释放空间,存在内存泄漏的问题。
char* Getmemory()
{
char p[] = "hello world";
return p;
}
void Test()
{
char* str = NULL;
str = Getmemory();
printf(str);
}
int main()
{
Test();
return 0;
}
请问运行Test 函数会有什么样的结果?
当出来Getmemory()函数后,创建的p[ ]被销毁,这样返回的地址赋值给str,str就成为野指针了,此时对str指向的空间的访问属于非法访问。
void GetMemory(char** p, int num)
{
*p = (char*)malloc(num);
}
void Test()
{
char* str = NULL;
GetMemory(&str, 100);
strcpy(str,"hello");
printf(str);
}
int main()
{
Test();
return 0;
}
请问运行Test 函数会有什么样的结果?
在使用完开辟的空间后,忘记用free(str)释放了。
void Test()
{
char* str = (char*)malloc(100);
strcpy(str, "hello");
free(str);
if (str != NULL)
{
strcpy(str, "world");
printf(str);
}
}
int main()
{
Test();
}
请问运行Test 函数会有什么样的结果?
这段代码考察的是在free(str);之后没有把str=NULL;
1.栈区(stack):在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限。
栈区主要存放运行函数而分配的局部变量、函数参数、返回数据、返 回地址等。
2.堆区(heap):一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收 。分配方式类似于链表。
3.数据段(静态区)(static)存放全局变量、静态数据。程序结束后由系统释放。
4.代码段:存放函数体(类成员函数和全局函数)的二进制代码。
有了这幅图,我们就可以更好的理解static关键字修饰局部变量的例子了。实际上普通的局部变量是在栈区分配空间的,栈区的特点是在上面创建的变量出了作用域就销毁。但是被static修饰的变量存放在数据段(静态区),数据段的特点是在上面创建的变量,直到程序结束才销。所以生命周期变长。
也许你从来没有听说过柔性数组(flexible array)这个概念,但是它确实是存在的。C99 中,结构中的最后一个元素允许是未知大小的数组,这就叫做『柔性数组』成员。
例如:
struct S
{
int n;
int arr[];//柔性数组
};
有些编译器会报错无法编译可以改成:
struct S
{
int n;
int arr[0];//柔性数组
};
结构中的柔性数组成员前面必须至少一个其他成员。
sizeof 返回的这种结构大小不包括柔性数组的内存。
包含柔性数组成员的结构用malloc ()函数进行内存的动态分配,并且分配的内存应该大于结构的大小,以适应柔性数组的预期大小。
例如:
struct S
{
int n;
int arr[0];//柔性数组
};
int main()
{
printf("%d\n",sizeof(struct S));//输出结果为4
return 0;
}
struct S
{
int n;
int arr[0];//柔性数组
};
int main()
{
int i = 0;
struct S* p = (struct S*)malloc(sizeof(struct S) + 100 * sizeof(int));
p->n = 100;
for (i = 0; i < 100; i++)
{
p->arr[i] = i;
}
free(p);
return 0;
}
这样柔性数组成员a,相当于获得了100个整型元素的连续空间。
//代码1
struct S
{
int n;
int arr[0];//柔性数组
};
int main()
{
int i = 0;
struct S* p = (struct S*)malloc(sizeof(struct S) + 100 * sizeof(int));
if (p == NULL)
{
perror("malloc");
return 1;
}
p->n = 100;
for (i = 0; i < 100; i++)
{
p->arr[i] = i;
}
//空间不够,需要增容
struct S* ptr = realloc(p, sizeof(struct S) + 60);
if (ptr == NULL)
{
perror("realloc");
return 1;
}
p = ptr;
p->n = 15;
for (i = 0; i < 15; i++)
{
printf("%d\n", p->arr[i]);
}
//释放
free(p);
p = NULL;
return 0;
}
上述的结构也可以设计为:
//代码2
struct S
{
int n;
int* arr;//柔性数组
};
int main()
{
struct S* ps = (struct S*)malloc(sizeof(struct S));
if (ps == NULL)
{
perror("malloc->ps");
return 1;
}
ps->n = 100;
ps->arr = (int*)malloc(40);
if (ps->arr == NULL)
{
perror("malloc->arr");
return 1;
}
int i = 0;
for (i = 0; i < 10; i++)
{
ps->arr[i] = i + 1;
}
//调整
int* ptr = (int*)realloc(ps->arr, 60);
if (ptr == NULL)
{
perror("realloc");
return 1;
}
else
{
ps->arr = ptr;
}
//打印
for (i = 0; i < 15; i++)
{
printf("%d\n",ps->arr[i]);
}
//释放
free(ps->arr);
ps -> arr = NULL;
free(ps);
ps = NULL;
return 0;
}
上述代码1和代码2可以完成同样的功能,但是方法1的实现有两个好处:
第一个好处是:方便内存释放
如果我们的代码是在一个给别人用的函数中,你在里面做了二次内存分配,并把整个结构体返回给用户。用户调用free可以释放结构体,但是用户并不知道这个结构体内的成员也需要free,所以你不能指望用户来发现这个事。所以,如果我们把结构体的内存以及其成员要的内存一次性分配好了,并返回给用户一个结构体指针,用户做一次free就可以把所有的内存也给释放掉。
第二个好处是:这样有利于访问速度
连续的内存有益于提高访问速度,也有益于减少内存碎片。