I’m following the code from this libcurl example and everything seems to work perfectly as a standalone, so i wanted to make it a function in its own file to reuse in other code.
I added two parameters, one char *addr for the web address and another char *response to retrieve the response from curl, so the template became
extern int curlGet(char *addr, char *response);
and i return the resulting response with a
memcpy(response,chunk.memory,chunk.size+1);
and then i call it on my main file with the following code
int main(void){
char *tmp=malloc(1);
curlGet("www.google.com",tmp);
printf("%sn",tmp);
//free(tmp);
return 0;
};
The addr parameter works without a problem, but the tmp one constantly throws a segmentation fault (core dumped), and if i uncomment the free(tmp) i get a
free(): invalid next size (fast)
Aborted (core dumped)
my instincts tell me its something with memory allocation, but i’m too new to c to know why.