How do you change the size of a global array?

This is a discussion on How do you change the size of a global array? within the C Programming forums, part of the General Programming Boards category; I've been trying to find the solution to this, but have been consistently coming up empty handed. Say I have ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    26

    How do you change the size of a global array?

    I've been trying to find the solution to this, but have been consistently coming up empty handed.

    Say I have 2 global arrays, one of char and one of int

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int intArray[]; // assumed to have 1 element
    char charArray[]; // assumed to have 1 element
    
    int main(void)
    {
    int newsize;
    maxsize = findNewSize();
    }
    Is it possible to change the size of the arrays once I have calculated the max size? How exactly would this be done?

    Thank you very much for any help.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    No. But you can change your program to have global pointers to arrays, and malloc what you initially need, then realloc later on if they need to grow.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    so I would have....

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int *intArray[]; 
    char *charArray[];
    
    int main(void)
    {
    int newsize;
    maxsize = findNewSize();
    }
    How would I use maxsize and realloc what I need?

    My problem is that I don't know the size that I need the arrays until the program starts and I read data from an input file.
    Last edited by kbfirebreather; 10-17-2009 at 07:35 AM.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Here's some code to get you going, as an example. You can look up realloc to see how it works.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int * intArray ; 
    char * charArray ; 
    
    #define INITSIZE 5
    
    int main(void)
    {
    	int newsize;
    	intArray = malloc(INITSIZE * sizeof(int)) ; 
    	charArray = malloc(INITSIZE * sizeof(char)) ; 
    	.
    	.
    	.
    	maxsize = findNewSize();
    	
    	return 0 ; 
    }
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,639
    Just keep in mind that whatever you 'malloc'/'realloc' should be cleaned up with a call to 'free'.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    Thank you very much, I'm gonna go ahead and mess around with this.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    26
    If an INITSIZE isn't required for my case, I can go ahead and findNewSize(); and use that value to malloc the array. Is this correct?

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Sure. You do a malloc first, then realloc.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,639
    Here's a little more robust approach:

    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int* a1 = 0, *a2 = 0;
    int buffer_size = 0;
    
    int reset_global_arrays( size_t size )
    {
    	free( a1 );
    	free( a2 );
    	if( size == 0 )
    	{
    		a1 = a2 = 0;
    	}
    	else
    	{
    		a1 = realloc( a1, size * sizeof( *a1 ) );
    		a2 = realloc( a2, size * sizeof( *a2 ) );
    		if( !a1 || !a2 )
    		{
    			reset_global_arrays( 0 );
    			return 0;
    		}
    	}	
    	buffer_size = size;
    	return 1;
    }
    
    void fatal( const char* message, int code )
    {
    	perror( message );
    	exit( code );
    }
    
    int main( void )
    {
    	size_t initial_size = 1024;
    	if( !reset_global_arrays( initial_size ) )
    		fatal( "reset_global_arrays", 1 );
    // ...do stuff...	
    	reset_global_arrays( 0 );	
    	return 0;	
    }
    Last edited by Sebastiani; 10-17-2009 at 09:32 AM. Reason: Fixed realloc size bug.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Dino View Post
    Sure. You do a malloc first, then realloc.
    realloc can be used in place of the first malloc. There's no reason you must use malloc first.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Ah. Thanks.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global array - size
    By Tupcia in forum C Programming
    Replies: 8
    Last Post: 04-15-2008, 03:27 PM
  2. Array size
    By tigrfire in forum C Programming
    Replies: 5
    Last Post: 11-14-2005, 07:45 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. templates, unresolved external error
    By silk.odyssey in forum C++ Programming
    Replies: 9
    Last Post: 06-09-2004, 04:39 PM
  5. Class member array size with constuctor initalizer
    By rafe in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2002, 10:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21