Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamic memory allocation in C using realloc
    primarykey
    data
    text
    <p>I have read the other SO question about using realloc to get a new pointer to the beginning of a bigger memory address space but I cant figure out what I am doing wrong. It prints a backtrace and memory dump. I later try to access strhldr but I dont think it even gets that far.</p> <pre><code>char *strhldr = (char *)malloc(strsize); int chrctr = 0; if(chrctr == strsize - 3){ // when you get close strsize = strsize*2; //double size char *temp = realloc(strhldr, strsize); //make more room if(temp == NULL) printf("reallocate failed\n"); else{ strhldr = temp; free(temp); // removed same issue } } // Later attempt to add it to an array cmdargs[i] = strhldr; </code></pre> <p>This is all within a while loop where chrctr and strsize get incremented</p> <p>complete code</p> <pre><code> int argctr = 64; char **cmdargs = (char **) malloc(argctr * sizeof(char*)); char c = getchar(); int i = 0; while(c != '\n' &amp;&amp; c != EOF){ //read through a String of stdin int strsize = 32; char *strhldr = (char *)malloc(strsize); char *strstarthldr = strhldr; if(c == ' ') c = getchar(); while(c != ' ' &amp;&amp; c != '\n' &amp;&amp; c != EOF){ int chrctr = 0; if(chrctr == strsize - 3){ // when you get close strsize = strsize*2; //double size char *temp = realloc(strhldr, strsize); //make more room if(temp == NULL) printf("reallocate failed\n"); else strhldr = temp; } //add that word to the array of strings strhldr[chrctr] = c; chrctr++; c = getchar(); } strhldr[charctr] = '\0'; //strhldr = strstarthldr; cmdargs[i] = strhldr; i++; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload