69,382
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <malloc.h>
typedef struct NUM
{
int a;
struct NUM *next;
} num, *pnum;
pnum c_list(); //创建链表
void s_list(pnum head); //输出链表
void d_list(pnum head, int a); //删除链表(删除指定的值)
int main(void)
{
int a;
while (1 == scanf("%d", &a))
{
pnum p;
p = c_list(); //创建链表,并将链表首节点赋给P
s_list(p); //输出链表
d_list(p, a); //删除链表(删除值为2的节点)
s_list(p); // 输出链表
// 我在main()函数里增加了循环输入欲删除节点成员值的功能
// 增加了输出下面的字节、内存地址信息
// 其它地方只改了删除链表节点函数的一行:增加了一句else
// 申请获取内存空间的代码没改
printf("sizeof(num) is %ld\n",sizeof(num));
printf("sizeof(pnum) is %ld\n",sizeof(pnum));
printf("sizeof(struct NUM) is %ld\n",sizeof(struct NUM));
printf("hex address of p is %x\n",&p);
printf("hex address of p->next is %x\n",p->next);
printf("hex address of p->next->next is %x\n",p->next->next);
}
return 0;
}
//创建链表
pnum c_list()
{
pnum p;
pnum pend; //尾节点
int i;
pnum head; //首节点,指向头节点
head = (pnum)malloc(sizeof(pnum)); // 所有的malloc里的sizeof都保持原样sizeof(pnum)
head->next = NULL;
p = head;
for (i = 0; i < 3; i++)
{
pend = (pnum)malloc(sizeof(pnum));
pend->a = i;
pend->next = NULL;
p->next = pend;
p = p->next;
}
return head;
}
//输出链表
void s_list(pnum head)
{
pnum p;
p = head; //接收传过来的首节点
while (NULL != p->next)
{
printf("%d ", p->next->a);
p = p->next;
}
printf("\n");
}
//删除链表
void d_list(pnum head, int a) //int a 接收删除指定的节点
{
pnum p;
pnum q; //用于指向需要删除的节点
p = head; //接收传过来的首节点
while (NULL != p->next)
{
if (p->next->a == a)
{
q = p->next;
p->next = q->next;
free(q);
q = NULL;
}
else //只增加了这一行else。下面的 p = p->next; 是原来就有的。
p = p->next;
}
}
head = (pnum)malloc(sizeof(pnum));
// glibc/malloc/malloc.c
/*
* Vital statistics:
Supported pointer representation: 4 or 8 bytes
Supported size_t representation: 4 or 8 bytes
Note that size_t is allowed to be 4 bytes even if pointers are 8.
You can adjust this by defining INTERNAL_SIZE_T
Alignment: 2 * sizeof(size_t) (default)
(i.e., 8 byte alignment with 4byte size_t). This suffices for
nearly all current machines and C compilers. However, you can
define MALLOC_ALIGNMENT to be wider than this if necessary.
Minimum overhead per allocated chunk: 4 or 8 bytes
Each malloced chunk has a hidden word of overhead holding size
and status information.
Minimum allocated size: 4-byte ptrs: 16 bytes (including 4 overhead)
8-byte ptrs: 24/32 bytes (including, 4/8 overhead)
When a chunk is freed, 12 (for 4byte ptrs) or 20 (for 8 byte
ptrs but 4 byte size) or 24 (for 8/8) additional bytes are
needed; 4 (8) for a trailing size field and 8 (16) bytes for
free list pointers. Thus, the minimum allocatable size is
16/24/32 bytes.
Even a request for zero bytes (i.e., malloc(0)) returns a
pointer to something of the minimum allocatable size.
*/
// 测试最小分配到的内存空间大小,是否为32bytes
// 测试环境OS为64bit,size_t为8bytes
#include <stdio.h>
#include <malloc.h>
typedef struct mall_test
{
int a;
struct mall_test *next;
} MT, *PMT;
int main(int argc, char const *argv[])
{
char *test; // 指针变量
printf("size of type : size_t is %ld\n", sizeof(size_t));
printf("size of type : int is %ld\n", sizeof(int));
printf("size of type : unsigned int is %ld\n", sizeof(unsigned int));
printf("size of type : * is %ld\n", sizeof(test));
printf("size of type : struct mall_test is %ld\n", sizeof(MT));
// 申请内存空间
PMT p[5] = {NULL};
for (int i = 0; i < 5; ++i)
{
p[i] = (PMT)malloc(0);
if (NULL == p[i])
printf("p[%d] malloc failed.\n", i);
else
{
p[i]->a = i + 1;
p[i]->next = NULL;
printf("p[%d] = (char*)malloc(0) returns hex address of p[%d] : %x\n", i, i, p[i]);
}
}
// 访问成员
for (int i = 0; i < 5; ++i)
{
printf("p[%d]->a is %d\n",i,p[i]->a);
}
// 释放内存空间
for (int i = 0; i < 5; ++i)
{
free(p[i]);
p[i] = NULL;
}
return 0;
}