change array size at runtime

Discussion in 'C Programming' started by sunny_rao, Apr 15, 2005.

  1. sunny_rao

    sunny_rao Guest

    Is it possible to change the size of an array dynamically at runtime ?
    sunny_rao, Apr 15, 2005
    #1
    1. Advertising

  2. sunny_rao

    Eric Sosman Guest

    sunny_rao wrote:
    > Is it possible to change the size of an array dynamically at runtime ?


    No.

    In the older "C90" Standard, every array has a size
    that is fixed at compile time; the size is a compile-time
    constant. In the newer "C99" Standard the array's size
    can be a run-time expression (e.g., a variable), but once
    the array is created its size remains constant for as long
    as the array exists; changing the value of the size-giving
    variable after the array is created does not change the
    size of the array.

    A region of dynamically-allocated memory can act as an
    array for many purposes, and the realloc() function can
    change the size (and possibly the location) of the region.
    It's not *quite* the same thing, but it often suffices.

    --
    Eric Sosman, Apr 15, 2005
    #2
    1. Advertising

  3. sunny_rao

    Jason Guest

    sunny_rao wrote:
    > Is it possible to change the size of an array dynamically at runtime

    ?

    Check out "void *realloc(void *p, size_t size)" in <stdlib.h>.

    -Jason
    Jason, Apr 15, 2005
    #3
  4. On Fri, 15 Apr 2005 17:46:06 -0400, sunny_rao wrote:

    > Is it possible to change the size of an array dynamically at runtime ?


    C'mon, read the FAQ, it was posted earlier today for crying out loud...

    Rob Gamble
    Robert Gamble, Apr 15, 2005
    #4
  5. sunny_rao wrote:
    > Is it possible to change the size of an array dynamically at runtime ?


    A declared array cannot be resized. A region of memory allocated via
    malloc, calloc, or realloc can be resized with realloc.
    Martin Ambuhl, Apr 15, 2005
    #5
  6. sunny_rao

    Null_Ptr_72 Guest

    Hey,

    If you needed to resize any data-structure at run-time, i suggest
    you read more on malloc _and_ realloc, a very good practice would be
    for you to create a Linked List, wait for user inpput and
    adjust&&readjust the size at run time.

    As far as Arrays[ ], ISO forbids for us to change the size at
    run-time, depending on your implementation (i.e Borland C++ builder 6)
    will flag an error, while gcc Won't. The whole idea behind this is to
    help us not "write post fence" data in memory.

    Hope this helps
    Null_Ptr_72, Apr 16, 2005
    #6
  7. "Null_Ptr_72" <> writes:
    [...]
    > As far as Arrays[ ], ISO forbids for us to change the size at
    > run-time, depending on your implementation (i.e Borland C++ builder 6)
    > will flag an error, while gcc Won't. The whole idea behind this is to
    > help us not "write post fence" data in memory.


    gcc won't flag an error for what? I don't know how you'd even *try*
    to change the size of an array. Perhaps something like
    sizeof(array_obj) *= 2;
    but I'd expect any compiler to reject that.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Apr 16, 2005
    #7
  8. The comp.lang.c Oracle has pondered your question deeply.

    Your question was:

    > Is it possible to change the size of an array dynamically at runtime

    ?

    And thus spake the Oracle:

    } Not a fixed-size array, but if you declare a pointer, you can change
    the
    } amount of space it points to via realloc().
    } See the code in FreeDOS Edlin 2.4 for a complete dynamic array
    implementation.

    Gregory Pietsch
    Gregory Pietsch, Apr 17, 2005
    #8
  9. sunny_rao wrote on 15/04/05 :
    > Is it possible to change the size of an array dynamically at runtime ?


    If it was dynamically allocated, yes, by a proper use of realloc() (see
    the FAQ). Static arrays can't be resized.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "Clearly your code does not meet the original spec."
    "You are sentenced to 30 lashes with a wet noodle."
    -- Jerry Coffin in a.l.c.c++
    Emmanuel Delahaye, Apr 17, 2005
    #9
  10. sunny_rao

    Null_Ptr_72 Guest

    Dear Thompson,
    "but I'd expect any compiler to reject that."
    There are many tricks we can play to go on doing illegal operations
    at compile time! "Implementation" however can determine how limited we
    are in "being" slick &&|| Tricky.

    Try your code "sizeof(array_obj) *= 2" on gcc, and try it on a
    different compiler, see the results. One will accept it "because its
    tricky" while the other won't "because its expensive!"
    Null_Ptr_72, Apr 17, 2005
    #10
  11. "Null_Ptr_72" <> writes:
    > Dear Thompson,
    > "but I'd expect any compiler to reject that."
    > There are many tricks we can play to go on doing illegal operations
    > at compile time! "Implementation" however can determine how limited we
    > are in "being" slick &&|| Tricky.
    >
    > Try your code "sizeof(array_obj) *= 2" on gcc, and try it on a
    > different compiler, see the results. One will accept it "because its
    > tricky" while the other won't "because its expensive!"


    I just tried it on five different compilers, including gcc; all of
    them rejected it. I would be astonished if *any* compiler supported
    modifying the size of an array object in this manner; it would be very
    difficult to do so while maintaining the required C semantics.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Apr 17, 2005
    #11
    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. Wongod
    Replies:
    2
    Views:
    693
  2. sunny
    Replies:
    5
    Views:
    2,090
    CBFalconer
    Aug 17, 2006
  3. Stefano Sabatini
    Replies:
    9
    Views:
    360
    terminator
    Dec 4, 2007
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,524
    Michael Jung
    May 25, 2008
  5. Sean Nakasone
    Replies:
    1
    Views:
    175
Loading...

Share This Page