所以,我在做这个练习:realloc()浪费了一大堆空间,我做错了什么?
编写一个C语言函数void出现(字符* S,焦炭C,焦*** OCCP, INT * N),给定一个字符串s和一个char C,计数的 出现在字符串s炭数c,返回OCCP炭的新数组,它包含每个C 发生s中不会忽略的ADRESS中n表示数,并返回在
主要样品:
#include <stdio.h>
int main(){
int i, n;
char** occ;
occorrenze("engineering", 'n', &occ, &n);
for (i=0; i<n; ++i) printf("%s\n", occ[i]); // prints ngineering neering ng
free(occ);
}
最初我写的功能是这样的:
void occurrences(char* s1, char c, char*** s, int* n){
*n=0;
char* arr[2];
int length=strlen(s1);
int i;
for(i=0; i<length; i++){
if(s1[i]==c)(*n)++;
}
*s=(malloc((*n)*sizeof(char**)));
int a=0;
for(i=0; i<length; i++){
if(s1[i]==c){
(*s)[a]= &s1[i];
a++;
}
}
}
效果不错,但我想尝试并重新写迭代字符串只是一个时间。我想用的realloc()函数,我从来没有使用过,最终我来到了这一点:
void occurrences(char* s1, char c, char*** s, int* n){
*n=0;
*s=malloc(0);
char* arr[2];
int length=strlen(s1);
int i,a=0;
for(i=0; i<length; i++){
if(s1[i]==c){
(*n)++;
*s=realloc(*s,(*n)*sizeof(char**));
(*s)[a]= &s1[i];
a++;
}
}
}
这一次似乎也很好的工作,但后来我跑Valgrind的:
==4893== HEAP SUMMARY:
==4893== in use at exit: 0 bytes in 0 blocks
==4893== total heap usage: 4 allocs, 4 frees, 48 bytes allocated
==4893==
==4893== All heap blocks were freed -- no leaks are possible
==4893==
==4893== For counts of detected and suppressed errors, rerun with: -v
==4893== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 2 from 2)
48字节分配?它应该是24个字节,对吧? 总堆大小为8 * n!而不是8 * N个......我想我失去了一些东西XD
编辑:复制权函数笑
为什么你认为它应该是24? – nos
@Andrew Medico复制/粘贴错误,很抱歉! – Atlas80b
32位或64位系统? 64位系统使用8个字节的指针,这将使48个。 – DoxyLover