basic malloc query

This is a discussion on basic malloc query within the C Programming forums, part of the General Programming Boards category; Hi, Say I have an array of strings named S and I want to copy its values to another array ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    basic malloc query

    Hi,

    Say I have an array of strings named S and I want to copy its values to another array of strings D.

    How can I do the memory allocation?

    AK

  2. #2
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    http://www.cplusplus.com/ref/cstdlib/malloc.html


    the problem w/the example they have is they typecast malloc. the part of buffer = (char*) malloc (i+1); dont do that...it should be something more like buffer = malloc (i+1 * sizeof(char));
    Last edited by willc0de4food; 04-27-2006 at 08:46 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Couldnt get it!
    I'm dealing with array of strings!How to copy that?

  4. #4
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    lol well you'd do similar to the example, but you'd already have the string so you wouldn't have to prompt for the length. you'd simply get the length of the strings, malloc the pointer to the size of the strings, including the null at the end of each one, and then copy the information over..
    Registered Linux User #380033. Be counted: http://counter.li.org

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Could you show an example please?
    Its complicated for a newbie like me!
    Still fresh into pointer

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,588
    > How can I do the memory allocation?
    Start by showing how you've declared S and D

    How you do this depends on what you have to start with, there are many variations on the basic theme.
    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.

  7. #7
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    Code:
    #include <string.h>
    int i;
    
    if ((D = malloc(sizeof(*S)*SIZE)) == NULL)
        die();
    for (i = 0; i < SIZE; i++) {
        if ((D[i] = strdup(S[i])) == NULL) {
            //rollback
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 09:28 AM
  3. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 04:28 AM
  4. 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