C:条件内存分配
C : conditional memory allocation
如何根据这些条件释放内存?:
如果已用内存小于系统容量的 25%,请释放 50% 的未使用内存。
这是我的代码尝试:
你的问题似乎是
if(rand() % 1 > 0) {
rand() % 1
总是 0
更改为:
if(rand() % 2 > 0) {
还有一个错字:
while(current = !NULL) {
改为
while(current != NULL) {
不要忘记将 next
节点重新连接到循环中的前一个节点,使用临时节点,例如:
temp = current;
while (current != NULL) {
next = current->next;
if (rand() % 2 > 0) {
temp->next = next;
free(current);
} else {
temp = current;
}
current = next;
}
如何根据这些条件释放内存?: 如果已用内存小于系统容量的 25%,请释放 50% 的未使用内存。
这是我的代码尝试:
你的问题似乎是
if(rand() % 1 > 0) {
rand() % 1
总是 0
更改为:
if(rand() % 2 > 0) {
还有一个错字:
while(current = !NULL) {
改为
while(current != NULL) {
不要忘记将 next
节点重新连接到循环中的前一个节点,使用临时节点,例如:
temp = current;
while (current != NULL) {
next = current->next;
if (rand() % 2 > 0) {
temp->next = next;
free(current);
} else {
temp = current;
}
current = next;
}