beginner qn..malloc

Discussion in 'C Programming' started by nodrogbrown, Jan 23, 2008.

  1. nodrogbrown

    nodrogbrown Guest

    hi
    i want to create an array of strings .I know the number of items that
    the array will contain but the size of each string will be different
    and will not be known at compile time.

    eg: in
    char * mynames[]

    mynames[0]="mathew"
    mynames[1]="gordon_brown"
    mynames[2]="F:\gordon\docs\mycv.txt"

    how can i allocate the memory for this array? I know malloc() and
    free() need to be used but not sure since i am only a beginner
    thanx in adv
    gordon
    nodrogbrown, Jan 23, 2008
    #1
    1. Advertising

  2. nodrogbrown

    santosh Guest

    nodrogbrown wrote:

    > hi
    > i want to create an array of strings .I know the number of items that
    > the array will contain but the size of each string will be different
    > and will not be known at compile time.
    >
    > eg: in
    > char * mynames[]
    >
    > mynames[0]="mathew"
    > mynames[1]="gordon_brown"
    > mynames[2]="F:\gordon\docs\mycv.txt"
    >
    > how can i allocate the memory for this array? I know malloc() and
    > free() need to be used but not sure since i am only a beginner
    > thanx in adv
    > gordon


    You can use a loop:

    for (ctr = 0; ctr < ARR_SIZE; ctr++) {
    mynames[ctr] = malloc(elements);
    if (!mynames[ctr]) {
    handle_error();
    }
    else {
    elements = get_next_array_size();
    }
    }

    I hope the object and routine names are self descriptive.
    santosh, Jan 23, 2008
    #2
    1. Advertising

  3. nodrogbrown

    Ian Collins Guest

    nodrogbrown wrote:
    > hi
    > i want to create an array of strings .I know the number of items that
    > the array will contain but the size of each string will be different
    > and will not be known at compile time.
    >
    > eg: in
    > char * mynames[]
    >
    > mynames[0]="mathew"
    > mynames[1]="gordon_brown"
    > mynames[2]="F:\gordon\docs\mycv.txt"
    >
    > how can i allocate the memory for this array? I know malloc() and
    > free() need to be used but not sure since i am only a beginner


    Something like:

    #include <stdlib.h>

    enum { NumberOfNames = 42 };

    int main(void)
    {
    char* mynames[NumberOfNames] = {NULL};

    size_t stringSize = someValue;

    mynames[0] = malloc( stringSize );

    stringSize = someOtherValue;

    mynames[1] = malloc( stringSize );

    /* do stuff */

    for( unsigned n = 0; n < NumberOfNames; free( mynames[n++] ) );
    }

    --
    Ian Collins.
    Ian Collins, Jan 23, 2008
    #3
  4. nodrogbrown

    James Kuyper Guest

    nodrogbrown wrote:
    > hi
    > i want to create an array of strings .I know the number of items that
    > the array will contain but the size of each string will be different
    > and will not be known at compile time.
    >
    > eg: in
    > char * mynames[]
    >
    > mynames[0]="mathew"
    > mynames[1]="gordon_brown"
    > mynames[2]="F:\gordon\docs\mycv.txt"
    >
    > how can i allocate the memory for this array? I know malloc() and
    > free() need to be used but not sure since i am only a beginner
    > thanx in adv
    > gordon


    Other people have given you one kind of solution. However, if all of
    your strings come from string literals, there's a much simpler solution:

    char * mynames[] =
    {"mathew", "gordon_brown", "F:\gordon\docs\mycv.txt"};

    You probably need the more complicated solution, but since you're a
    beginner I didn't want to miss out on the possibility that the simpler
    solution would meet your needs.
    James Kuyper, Jan 23, 2008
    #4
  5. nodrogbrown

    santosh Guest

    James Kuyper wrote:

    > nodrogbrown wrote:
    >> hi
    >> i want to create an array of strings .I know the number of items that
    >> the array will contain but the size of each string will be different
    >> and will not be known at compile time.
    >>
    >> eg: in
    >> char * mynames[]
    >>
    >> mynames[0]="mathew"
    >> mynames[1]="gordon_brown"
    >> mynames[2]="F:\gordon\docs\mycv.txt"
    >>
    >> how can i allocate the memory for this array? I know malloc() and
    >> free() need to be used but not sure since i am only a beginner
    >> thanx in adv
    >> gordon

    >
    > Other people have given you one kind of solution. However, if all of
    > your strings come from string literals, there's a much simpler
    > solution:
    >
    > char * mynames[] =
    > {"mathew", "gordon_brown", "F:\gordon\docs\mycv.txt"};
    >
    > You probably need the more complicated solution, but since you're a
    > beginner I didn't want to miss out on the possibility that the simpler
    > solution would meet your needs.


    He specifically says above that the sizes of the strings will not be
    known at compile time.
    santosh, Jan 23, 2008
    #5
  6. In article <fn7g02$u41$>,
    santosh <> wrote:

    >>> i want to create an array of strings .I know the number of items that
    >>> the array will contain but the size of each string will be different
    >>> and will not be known at compile time.

    [...]
    >>> mynames[0]="mathew"
    >>> mynames[1]="gordon_brown"
    >>> mynames[2]="F:\gordon\docs\mycv.txt"
    >>>


    >He specifically says above that the sizes of the strings will not be
    >known at compile time.


    He also gives an example in which they are, so he might be confused.

    -- Richard
    --
    :wq
    Richard Tobin, Jan 23, 2008
    #6
  7. James Kuyper <> writes:
    > nodrogbrown wrote:
    >> i want to create an array of strings

    [...]
    >> char * mynames[]
    >>
    >> mynames[0]="mathew"
    >> mynames[1]="gordon_brown"
    >> mynames[2]="F:\gordon\docs\mycv.txt"

    [...]
    >
    > Other people have given you one kind of solution. However, if all of
    > your strings come from string literals, there's a much simpler
    > solution:
    >
    > char * mynames[] =
    > {"mathew", "gordon_brown", "F:\gordon\docs\mycv.txt"};


    The \g, \d, and \m in the string literal are going to cause problems.
    If you want a backslash character in a string literal, you need to
    double it:

    "F:\\gordon\\docs\\mycv.txt"

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 23, 2008
    #7
  8. nodrogbrown

    Guest

    On Jan 23, 12:26 pm, Keith Thompson <> wrote:
    > James Kuyper <> writes:
    > > nodrogbrown wrote:
    > >> i want to create an array of strings

    > [...]
    > >> char * mynames[]

    >
    > >> mynames[0]="mathew"
    > >> mynames[1]="gordon_brown"
    > >> mynames[2]="F:\gordon\docs\mycv.txt"

    > [...]
    >
    > > Other people have given you one kind of solution. However, if all of
    > > your strings come from string literals, there's a much simpler
    > > solution:

    >
    > > char * mynames[] =
    > > {"mathew", "gordon_brown", "F:\gordon\docs\mycv.txt"};

    >
    > The \g, \d, and \m in the string literal are going to cause problems.
    > If you want a backslash character in a string literal, you need to
    > double it:
    >
    > "F:\\gordon\\docs\\mycv.txt"


    That's actually a problem inherited from the original question, I just
    cut-and-pasted it. However, I should have mentioned the problem. All
    my programming for the last dozen years has been for Unix-like
    systems, where filenames seldom present the same kind of problem, so I
    didn't even think about it.
    , Jan 23, 2008
    #8
  9. nodrogbrown

    Joe Wright Guest

    nodrogbrown wrote:
    > hi
    > i want to create an array of strings .I know the number of items that
    > the array will contain but the size of each string will be different
    > and will not be known at compile time.
    >
    > eg: in
    > char * mynames[]
    >
    > mynames[0]="mathew"
    > mynames[1]="gordon_brown"
    > mynames[2]="F:\gordon\docs\mycv.txt"
    >
    > how can i allocate the memory for this array? I know malloc() and
    > free() need to be used but not sure since i am only a beginner
    > thanx in adv
    > gordon


    Your declaration of mynames[] and assignments to it won't work. Assuming
    your three strings, try..

    char **names;
    names = malloc(3 * sizeof *names);
    names[0] = malloc(strlen("mathew")+1);
    strcpy(names[0], "mathew");
    names[1] = malloc(strlen("gordon brown")+1);
    strcpy(names[1], "gordon brown");

    Not pretty, I admit.
    Your use of "F:\gordon\docs\mycv.txt" won't work either. the '\' is an
    escape character in C strings. "F:\\gordon\\docs\\mycv.txt" has a
    chance. Also "F:/gordon/docs/mycv.txt" has a chance.

    names[2] = malloc(strlen("F:/gordon/docs/mycv.txt")+1);
    strcpy(names[1], "F:/gordon/docs/mycv.txt");

    To free all this up..

    for (i = 0; i < 3; ++i)
    free(names);
    free(names);

    --
    Joe Wright
    "If you think Health Care is expensive now, wait until it's free."
    Joe Wright, Jan 26, 2008
    #9
  10. Joe Wright <> writes:
    [...]
    > Your use of "F:\gordon\docs\mycv.txt" won't work either. the '\' is an
    > escape character in C strings. "F:\\gordon\\docs\\mycv.txt" has a
    > chance. Also "F:/gordon/docs/mycv.txt" has a chance.

    [...]

    I'm not sure what you mean when you say that
    "F:\\gordon\\docs\\mycv.txt" "has a chance". Is that meant to imply
    that there's a chance it could fail? If so, how?

    "F:/gordon/docs/mycv.txt" is a different string than
    "F:\\gordon\\docs\\mycv.txt". They *might* be functionally equivalent
    in some contexts (but not all; the details are OS-specific), but
    they'll certainly [*] be displayed differently, and that might be what
    matters.

    [*] I'm assuming a display device on which the forward slash and
    backslash characters are visually distinct.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 26, 2008
    #10
  11. >I'm not sure what you mean when you say that
    >"F:\\gordon\\docs\\mycv.txt" "has a chance". Is that meant to imply
    >that there's a chance it could fail? If so, how?


    How many systems do you know of that DON'T have that file? (or,
    for that matter, don't even have a F: drive?) If so, trying to
    open it would fail. And there's always the chance a file open could
    fail due to bad sectors, insufficient permissions, Sony rootkits, etc.

    There are no defined characters for the escapes \g, \d, or \m. An
    implementation might take \g to be the Google logo and \m to be the
    Microsoft Windows logo, which normally aren't allowed in file names.
    \d might be considered an error unless the content rating allows
    material for 35+.

    There are defined characters for escapes such as \r, \n, \b, and
    \f, but it can get awkward putting carriage returns, newlines,
    backspaces, and form feeds in file names (UNIX will let you do it,
    but it's still awkward).
    Gordon Burditt, Jan 27, 2008
    #11
  12. (Gordon Burditt) writes:
    >>I'm not sure what you mean when you say that
    >>"F:\\gordon\\docs\\mycv.txt" "has a chance". Is that meant to imply
    >>that there's a chance it could fail? If so, how?


    I wrote the above. Stop snipping attributions.

    > How many systems do you know of that DON'T have that file? (or,
    > for that matter, don't even have a F: drive?) If so, trying to
    > open it would fail. And there's always the chance a file open could
    > fail due to bad sectors, insufficient permissions, Sony rootkits, etc.


    Who said anything about opening files? The original question was
    about storing strings (one of which happens to look very much like a
    file name).

    "F:\\gordon\\docs\\mycv.txt" is a perfectly valid string literal; I
    see no way it can fail *as a string literal*.

    In any case, I was asking what Joe Wright meant.

    > There are no defined characters for the escapes \g, \d, or \m. An
    > implementation might take \g to be the Google logo and \m to be the
    > Microsoft Windows logo, which normally aren't allowed in file names.
    > \d might be considered an error unless the content rating allows
    > material for 35+.
    >
    > There are defined characters for escapes such as \r, \n, \b, and
    > \f, but it can get awkward putting carriage returns, newlines,
    > backspaces, and form feeds in file names (UNIX will let you do it,
    > but it's still awkward).


    Of course, that was the point of changing \ to \\ several messages
    upthread.

    This article was written by Keith Thompson <>.
    Permission to quote without attribution is explicitly denied for this
    message and for every message I post here.

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Jan 27, 2008
    #12
  13. nodrogbrown

    CBFalconer Guest

    Keith Thompson wrote:
    >

    .... snip ...
    >
    > This article was written by Keith Thompson <>.
    > Permission to quote without attribution is explicitly denied for
    > this message and for every message I post here.


    I like this.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Jan 28, 2008
    #13
  14. On Wed, 23 Jan 2008 15:15:11 +0000, Richard Tobin wrote:

    > In article <fn7g02$u41$>, santosh
    > <> wrote:
    >
    >>>> i want to create an array of strings .I know the number of items that
    >>>> the array will contain but the size of each string will be different
    >>>> and will not be known at compile time.

    > [...]
    >>>> mynames[0]="mathew"
    >>>> mynames[1]="gordon_brown"
    >>>> mynames[2]="F:\gordon\docs\mycv.txt"
    >>>>
    >>>>

    >>He specifically says above that the sizes of the strings will not be
    >>known at compile time.

    >
    > He also gives an example in which they are, so he might be confused.


    He also uses some odd characters in his example. What, pray tell, does
    \g do? Or \d? \m?
    Kelsey Bjarnason, Jan 28, 2008
    #14
  15. In article <>,
    Kelsey Bjarnason <> wrote:

    >>>>> mynames[0]="mathew"
    >>>>> mynames[1]="gordon_brown"
    >>>>> mynames[2]="F:\gordon\docs\mycv.txt"


    >>>He specifically says above that the sizes of the strings will not be
    >>>known at compile time.


    >> He also gives an example in which they are, so he might be confused.


    >He also uses some odd characters in his example. What, pray tell, does
    >\g do? Or \d? \m?


    I think that problem was addressed several days ago.

    -- Richard



    --
    :wq
    Richard Tobin, Jan 29, 2008
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    13
    Views:
    687
  2. ravi
    Replies:
    0
    Views:
    439
  3. Peter
    Replies:
    34
    Views:
    1,916
    Richard Tobin
    Oct 22, 2004
  4. porting non-malloc code to malloc

    , Feb 18, 2005, in forum: C Programming
    Replies:
    3
    Views:
    469
    Walter Roberson
    Feb 19, 2005
  5. Johs32

    to malloc or not to malloc??

    Johs32, Mar 30, 2006, in forum: C Programming
    Replies:
    4
    Views:
    313
    Captain Winston
    Mar 30, 2006
Loading...

Share This Page