Results 1 to 4 of 4

Thread: Changing the array size

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    Changing the array size

    Is there any way to resize a 1 or 2 dimensional array.
    Let us I have:

    myarray[5][10]

    How can I make it:

    myarray[7][15]

    after I have declared it and I have also set the values in it?
    And also will it delete all the values in the array after I resize it?
    Baaaaaaaaah

  2. #2
    jim mcnamara
    Guest
    try realloc like this:

    Code:
    void main( void )
    {
       long *buffer;
       size_t size;
    
       if( (buffer = (long *)malloc( 1000 * sizeof( long ) )) == NULL )
          exit( 1 );
    
       size = _msize( buffer );
       printf( "Size of block after malloc of 1000 longs: %u\n", size );
       buffer[99] = 1;
       printf("%s %ld\n","before realloc buffer[99]=",buffer[99]);
    
       /* Reallocate and show new size: */
       if( (buffer = (long *)realloc( buffer, size + (1000 * sizeof( long )) )) 
            ==  NULL )
          exit( 1 );
       size = _msize( buffer );
       printf( "Size of block after realloc of 1000 more longs: %u\n", 
                size );
       printf("%s %ld\n","after realloc buffer[99]=",buffer[99]);
       free( buffer );
       return;
    }

  3. #3
    Hyperactive Member Warmaster199's Avatar
    Join Date
    Aug 2000
    Location
    Canada
    Posts
    306
    Just do this:

    int myarray[5][10];


    In a function do this:


    free(myarray); // gets rid of it...
    int myarray[7][17]; //makes the array again
    // This should do it

    Doing this will erase the variable's contents...


    I don't think that you can do it Jim's way if you need 2 dimensional arrays... If you absolutely need 2D arrays without using my function and without getting rid of the contents, you need assembler as you can do ABSOLUTELY ANYTHING with assembler...
    Designer/Programmer of the Comtech Operating System(CTOS)

  4. #4

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    Thanks guys. It makes sense now
    Baaaaaaaaah

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width