1
|
malloc、calloc、free、_alloca
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include
#include
int
main()
{
inti;
int
*pn=(
int
*)
malloc
(5*
sizeof
(
int
));
printf
(
"malloc%p\n"
,pn);
for
(i=0;i<5;i++)
pn[i]=i;
pn=(
int
*)
realloc
(pn,10*
sizeof
(
int
));
printf
(
"realloc%p\n"
,pn);
for
(i=5;i<10;i++)
pn[i]=i;
for
(i=0;i<10;i++)
printf
(
"%3d"
,pn[i]);
free
(pn);
return
0;
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
//realloc.c
#include
#include
main()
{
char
*p;
clrscr();
//clearscreen
p=(
char
*)
malloc
(100);
if
(p)
printf
(
"MemoryAllocatedat:%x"
,p);
else
printf
(
"NotEnoughMemory!\n"
);
getchar
();
p=(
char
*)
realloc
(p,256);
if
(p)
printf
(
"MemoryReallocatedat:%x"
,p);
else
printf
(
"NotEnoughMemory!\n"
);
free
(p);
getchar
();
return
0;
}
|
1
2
3
4
5
6
7
8
9
|
#include
voidmain()
{
char
*p,*q;
p=(
char
*)
malloc
(10);
q=p;
p=(
char
*)
realloc
(q,20);
//A行,通过realloc扩大p的空间,并把新的地址赋值给p。
//…………………………
}
|