c - Realloc setting pointer to empty -
void test(){ char *c = malloc(strlen("i coffe") + 1); strcpy(c, "i coffe"); char **s = &c; while(strlen(*s) < 25) my_function(s); } void my_function(char **s){ char *w = *s; char *tmp = realloc(w, len + 2);//error here. *s gets = "" if(tmp != null) w = tmp; for(i= len; i>=p; i--){ w[i+1] = w[i]; } w[p] = c; } this function used insert new character inside char *.
also, function inside while loop. works fine 3rd time loop runs, sets *s = "".
thought using char *tmp keep data if wrong thing happen. can't understand why p *s been setted empty string.
you've forgotten assign new value *s in function contains realloc().
void test(void) // unaltered; still broken! { char *c = malloc(strlen("i coffe") + 1); strcpy(c, "i coffe"); char **s = &c; while (strlen(*s) < 25) my_function(s); } void my_function(char **s) // fixed 1 way { char *w = *s; size_t len = strlen(w) + 1; // define , initialize len char *tmp = realloc(w, len + 2); if (tmp != null) w = tmp; *s = w; // reassign `*s` } or, more simply:
void my_function(char **s) // fixed way { char *w = *s; size_t len = strlen(w); // define , initialize len char *tmp = realloc(w, len + 2); if (tmp != null) *s = tmp; // reassign `*s` } assigning w sets local variable copy of *s; not reset pointer in calling code.
note fix, loop in test() going run long time because nothing changes length of string in c. there's another problem: don't pass address of s my_function(), my_function() can't modify s.
void test(void) { char *c = malloc(strlen("i coffe") + 1); strcpy(c, "i coffe"); while (strlen(c) < 25) { my_function(&c); strcat(c, "az"); // grow string — not in real code printf("%2zu: <<%s>>\n", strlen(c), c); } } void my_function(char **s) { char *w = *s; size_t len = strlen(w) + 1; // define , initialize len char *tmp = realloc(w, len + 2); if (tmp != null) *s = tmp; // reassign `*s` } this away pointer pointer char in test(). if that's crucial, there's more thinking done.
code not formally tested yet!
code tested — can "pig's ear"? copy'n'paste of wrong material made test code fail. here's instrumented working version — valgrind gives clean bill of health.
#include <stdio.h> #include <stdlib.h> #include <string.h> static void my_function(char **s) { char *w = *s; size_t len = strlen(w) + 1; // define , initialize len printf("m1: %p: %2zu: <<%s>>\n", (void *)w, len, w); char *tmp = realloc(w, len + 2); if (tmp != null) *s = tmp; // reassign `*s` printf("m2: %p: %2zu: <<%s>>\n", (void *)*s, strlen(*s), *s); } static void test(void) { char *c = malloc(strlen("i coffe") + 1); if (c == 0) { fprintf(stderr, "out of memory\n"); exit(exit_failure); } strcpy(c, "i coffe"); printf("t1: %p: %2zu: <<%s>>\n", (void *)c, strlen(c), c); while (strlen(c) < 25) { my_function(&c); printf("t2: %p: %2zu: <<%s>>\n", (void *)c, strlen(c), c); if (c == null) { fprintf(stderr, "out of memory\n"); exit(exit_failure); } strcat(c, "az"); // grow string — not in real code printf("t3: %p: %2zu: <<%s>>\n", (void *)c, strlen(c), c); } free(c); } int main(void) { test(); return 0; }
Comments
Post a Comment