malloc

This is a discussion on malloc within the C Programming forums, part of the General Programming Boards category; can anyone explain malloc and how it can be used when trying to input a set of strings and then ...

  1. #1
    jpre
    Guest

    Arrow malloc

    can anyone explain malloc and how it can be used when trying to input a set of strings and then outputing a set of strings

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    Is this
    0. a known number of strings of fixed length?
    1. a known number of strings of variable length?
    2. an unknown number of strings of variable length?
    3. an unknown number of strings of fixed length?

    And can you do this using char arrays?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Unregistered
    Guest
    I am trying to use it in a program that takes in a user input of a any lentgh string, the user will use up to 10 strings, then I was going to store them into an array type; and output from an array type; using a for loop of course.
    Thanx jpre

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    23
    Hey,
    I don't know if its actually possible to take in an array of any size.
    (unless you take it in as a command line argument).

    There needs to be a maximun value for the size of the input string.

    Or you could possibly use malloc.. but that would require the user to enter any number of smaller string (fixed size) then you could join all the strings together into a larger string using malloc.

    of course i could be wrong. this is how i understand it though.
    hope i helped!
    ActionMan

    "THE DAY IS MYNE!!!!
    I'll take famouse titties for $400"
    -Sean Connery, Saturday Night Live

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,496
    > takes in a user input of a any lentgh string, the user will use up to 10 strings

    This is a simple implementation - if lines are longer than BUFSIZ, it will need some modification.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAX_LINES   10
    
    int main ( ) {
        char buff[BUFSIZ];
        char *lines[MAX_LINES];
        int i;
    
        for ( i = 0 ; i < MAX_LINES ; i++ ) {
            fgets( buff, BUFSIZ, stdin );
            lines[i] = malloc( strlen(buff) + 1 );
            strcpy( lines[i], buff );
        }
    
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. the basics of malloc
    By nakedBallerina in forum C Programming
    Replies: 21
    Last Post: 05-20-2008, 02:32 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 11:26 AM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 08:08 AM

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