Thread: Resize an array

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    7

    Question Resize an array

    How would I resize an array at runtime? I'm using VC++6.0

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you declare your array something like this:
    char myarray[100];
    then the answer is you can't resize it.

    To do dynamic resizing, you need a dynamically created array. Lookup the new keyword.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    32
    My preferred method:

    long *array = (long*) malloc(width * height * sizeof(long));

    then to access, use like a one dimensional array
    array[(y * width) + x] = 4;

    When done (MUST do this at end of app):
    free(array);

    There are other ways like using the New operator but I prefer to use malloc, for some reason.
    malloc is in stdlib.h
    - Tigs

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >>To do dynamic resizing, you need a dynamically created array. Lookup the new keyword.

    Or look up STL Vectors.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM