Like Tree1Likes
  • 1 Post By c99tutorial

simple use of malloc and realloc

This is a discussion on simple use of malloc and realloc within the C Programming forums, part of the General Programming Boards category; Hi guys i want to improve my knowledge about the dyn allocation of char pointers... with this code i wanted ...

  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 05:49 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,543
    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 06:20 PM.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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,057
    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
    2,543
    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
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

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, 01:53 AM
  3. malloc and realloc
    By jayfriend in forum C Programming
    Replies: 4
    Last Post: 01-05-2007, 01: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, 12:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21