小编S. *_*rma的帖子

内存缩小时的realloc()行为

手册页中realloc()说:

realloc()函数将指向的存储块的大小更改ptrsize字节。从区域开始到新旧大小的最小值之间的内容将保持不变。如果新的大小大于旧的大小,则不会初始化添加的内存。

但是,手册页没有说明如果新大小小于旧大小会发生什么。例如,如果我有以下代码:

ptr = realloc(ptr, nsize); // Where nsize < the original size and ptr is of type void **
Run Code Online (Sandbox Code Playgroud)

如果原始大小为size,是否表示ptr + nsize + 1仍包含分配的条目?

任何帮助表示赞赏。

c realloc dynamic-memory-allocation

4
推荐指数
2
解决办法
162
查看次数

如何使用本机 qsort 对 C 中的 `int **` 数组进行排序

我一直找不到任何与此相关的问题,我想我想弄清楚这个问题有点疯狂。

\n\n

我有以下代码:

\n\n
#include <stdio.h>\n#include <stdlib.h>\n#include <errno.h>\n#include <time.h>\n\nint cmp_int(const void *a, const void *b)\n{\n  return * (int *)a - * (int *)b;\n}\n\nint main(int argc, char *argv[])\n{\n  int n = 10;\n  int **arr = calloc(n, sizeof(int *));\n  srand((unsigned int) time(NULL));\n  for (int i = n-1; i >= 0; i--) {\n    arr[i] = calloc(1, sizeof(int));\n    *(arr[i]) = rand() % 1000;\n  }\n  for (int i = 0; i < n; i++)\n    printf("%d ", *(arr[i]));\n  printf("\\n");\n  qsort(arr, 10, sizeof(void *), cmp_int);\n  for (int …
Run Code Online (Sandbox Code Playgroud)

c sorting qsort

0
推荐指数
1
解决办法
1485
查看次数

标签 统计

c ×2

dynamic-memory-allocation ×1

qsort ×1

realloc ×1

sorting ×1