Thread: simple use of malloc and realloc

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    4

    simple use of malloc and realloc

    Hi guys i want to improve my knowledge about the dyn allocation of char pointers... with this code i wanted to type a string and insert the string in a array created dynamically:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main()
    {
    
    
    char c;
    char *test=NULL;
    unsigned int len;
    
    
    test = (char*) malloc (sizeof(char));
    printf("Gimme a string: ");
    while(c != '\n'){
    
    
                scanf("%c", &c);
                test = realloc (test, sizeof(char));
                len = strlen(test);
                test[len]=c;
                test[len+1]='\0';
    }
    
    
    printf("\nThis is the dynamic string: %s", test);
    
    
    printf("\n\n");
    system("PAUSE");
    return 0;
    }
    That crew this output:


    Gimme a string: Hello World

    This is the dynamic string: °):Hello World
    and i don't understand why there are these 3 initial character '°' ')' ':' that i didn't have typed...
    Last edited by zmone; 01-27-2013 at 06:49 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    test = realloc (test, sizeof(char));

    You allocate ONLY a single char then you over-write your program stack; you are lucky your program ran without crashing.

    I suggest re-reading the docs realloc - C++ Reference

    You have a single byte (char) allocated then you tell realloc to change it to a single byte once more. It does NOT add a byte.

    Tim S.
    Last edited by stahta01; 01-27-2013 at 07:20 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    4
    i want to allocate a single char for each cicle and for each char.. one by one... can you explain me the correct way to do this operation in a right way?

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by zmone View Post
    i want to allocate a single char for each cicle and for each char.. one by one...
    You didn't initialize len or c, which is a problem. Helpful is to see that if you allocate len bytes, the index len-1 is always the last index. Try the following

    Code:
    char c='\0';
    char *test=NULL;
    unsigned int len=1; // initial size is 1 byte
    
    test = (char*) malloc (len*sizeof(char));
    test[len-1] = '\0';
    printf("Gimme a string: ");
    
    while(c != '\n'){ 
                scanf("%c", &c);
                test[len-1]=c;
                len++;
                test = realloc (test, len*sizeof(char));
                test[len-1]='\0';
    }

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by zmone View Post
    i want to allocate a single char for each cicle and for each char.. one by one... can you explain me the correct way to do this operation in a right way?
    Read and learn; if you do not understand ask smart questions.
    realloc - C++ Reference
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using malloc & realloc
    By shruthi in forum C Programming
    Replies: 7
    Last Post: 09-12-2012, 03:04 AM
  2. Use of malloc and realloc
    By mrbains in forum C Programming
    Replies: 5
    Last Post: 11-11-2010, 02:53 AM
  3. malloc and realloc
    By jayfriend in forum C Programming
    Replies: 4
    Last Post: 01-05-2007, 02:25 PM
  4. malloc, realloc
    By figo2476 in forum C Programming
    Replies: 3
    Last Post: 04-28-2006, 10:11 PM
  5. Need help on malloc and realloc
    By YevGenius in forum C Programming
    Replies: 8
    Last Post: 03-06-2004, 01:55 AM