So, I was doing this exercise:
Write a C function void occurrences(char* s, char c, char*** occp, int* n) that, given a string s and a char c, counts the number of occurrences of char c in the string s, returns that number in n and returns in occp the adress of a new array of char that contains the adresses of each c occurrence in s
main sample:
#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);
}
Initially I wrote the function this way:
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++;
}
}
}
Worked well, but I wanted to try and re-write it iterating the string just one time. I thought of using realloc(), function which I never used before and eventually I came up with this:
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++;
}
}
}
This one seemed to work well too, but then I run 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 bytes allocated ? It should've been 24 bytes, right ? The total heap size is 8*n! instead of 8*n... I guess I am missing something XD
EDIT: copied the right function lol