Realloc in C and NULL-pointer -


why program not work every input? (read in input , print out in reverse order) xcode6 generated error message: hw5(14536,0x7fff7c23f310) malloc: * error object 0x100103aa0: pointer being realloc'd not allocated * set breakpoint in malloc_error_break debug unfortunately not understand this.

#include <stdio.h> #include <stdlib.h>  int main() {   char *input;   unsigned long long index;   input = malloc(1);    (index = 0; (input[index] = getchar()) != eof; index++)   {     if (input == null)     {       free(input);       printf("error: out of memory!\n");       return 0;     }     realloc(input, index + 2);   }    (index = index - 1; index != 0; index--)   {     putchar(input[index]);   }   printf("\n");    free(input);    return 0; } 

realloc() returns pointer new object. i'm using temporary variable because if realloc() fails reallocate memory, null returned , input remains valid.

char* temp = realloc(input, index + 2); if( !temp ) {     //deal error ... }  input = temp ; 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -