using malloc dynamically...?

This is a discussion on using malloc dynamically...? within the C Programming forums, part of the General Programming Boards category; So, I'm trying to learn C as I'm going to be needing it quite extensively. Now suppose I have a ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    245

    using malloc dynamically...?

    So, I'm trying to learn C as I'm going to be needing it quite extensively.

    Now suppose I have a file containing an arbitrary list of characters

    a z y a g y s h e p o................ etc

    How do I read this in character by character, and store these in an array without knowing the size?

    so basically, everytime i encounter a character, I have to allocate more memory and store it in my char*

    I don't want to have a pre-sized buffer of, say, 100....

    I know this might not be the best way to do it, but the point is, I need to learn how to create memory like this, on the fly, and attach this memory to my variable (which already has memory that was created dynamically)


    Any help would be appreciated!

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Basically:
    Start with some amount of memory and start adding elements.
    When you run out of room, increase the size and continue.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    245
    Quote Originally Posted by NeonBlack View Post
    Basically:
    Start with some amount of memory and start adding elements.
    When you run out of room, increase the size and continue.
    Thank you for your response.

    How would I increase the memory?

    Say I just have an int * a_ptr ?

    I guess, I'm just having trouble with syntax and understanding the concept of how to add memory to a_ptr, and to assign values to that new memory location...

    I mean, I know malloc calloc, but I don't know how I would assign that new memory to a_ptr while maintaining the memory currently at a_ptr

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Something like realloc.
    Code:
    if( ( newptr = realloc( oldptr, newsize ) ) != NULL )
    {
        ...allocation worked...
        oldptr = newptr;
    }
    else
    {
        ...allocation failed, you still have oldptr...
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    245
    Quote Originally Posted by quzah View Post
    Something like realloc.
    Code:
    if( ( newptr = realloc( oldptr, newsize ) ) != NULL )
    {
        ...allocation worked...
        oldptr = newptr;
    }
    else
    {
        ...allocation failed, you still have oldptr...
    }
    Quzah.
    realloc!! I had no idea realloc existed. Just tried this, and it works.

    excellent!

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Malloc/realloc() calls are expensive, so don't do this one element at a time, except maybe for this practice exercise. Generally realloc'ing in 1K or 4K blocks (or more) is probably good.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    MK27, Comrade, you may want to check Snopes on that quote of yours.

  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If you are going to read characters from a file, you would be better served by using a function like stat() (UNIX) for example, to get the file size then allocate the appropriate amount of memory for the file in one go.

    Another approach is to read a line at the time and discard it when it's time to read the next, if your application allows for it of course.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    245
    Quote Originally Posted by Subsonics View Post
    If you are going to read characters from a file, you would be better served by using a function like stat() (UNIX) for example, to get the file size then allocate the appropriate amount of memory for the file in one go.

    Another approach is to read a line at the time and discard it when it's time to read the next, if your application allows for it of course.
    Right, I agree with you, however, I just need practice using malloc/calloc/realloc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures and dynamically allocated arrays
    By Bandizzle in forum C Programming
    Replies: 7
    Last Post: 10-04-2009, 02:05 PM
  2. Dynamically allocating 3D array
    By ssharish2005 in forum C Programming
    Replies: 8
    Last Post: 02-26-2008, 03:07 PM
  3. Alternative to malloc
    By stellastarr in forum C Programming
    Replies: 13
    Last Post: 04-30-2007, 04:10 PM
  4. 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
  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