Raised This Month: $252 Target: $400
 63% 

Dynamic allocation Of 2D arrays


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
sjoytu
New Member
Join Date: Feb 2018
Old 02-22-2018 , 01:36   Dynamic allocation Of 2D arrays
Reply With Quote #1

Description: add to source, compile and run. very brief example usage (basically just the calling syntax) included in the comments two functions; the first dynamically allocates a 2D array with a user- or program-specified number of rows/columns; the second deallocates the same array. demonstrates the multi-dimensional usage of malloc and free. this snippet is also here: https://txeditor.com/jhn8gomb8g1

Code:
/*   allocate2D
/*   function to dynamically allocate 2-dimensional array using malloc.
/*
/*   accepts an int** as the "array" to be allocated, and the number of rows and
/*   columns.
*/
void allocate2D(int** array, int nrows, int ncols) {
     
     /*  allocate array of pointers  */
     array = ( int** )malloc( nrows*sizeof( int* ) );
     
     /*  allocate each row  */
     int i;
     for(i = 0; i < nrows; i++) {
          array[i] = ( int* )malloc( ncols*sizeof( int ) );
     }

}

/*   deallocate2D
/*   corresponding function to dynamically deallocate 2-dimensional array using 
/*   malloc.
/*
/*   accepts an int** as the "array" to be allocated, and the number of rows.  
/*   as with all dynamic memory allocation, failure to free malloc'ed memory
/*   will result in memory leaks
*/
void deallocate2D(int** array, int nrows) {
     
     /*  deallocate each row  */
     int i;
     for(i = 0; i < nrows; i++) {
          free(array[i]);
     }
     
     /*  deallocate array of pointers  */
     free(array);
     
}

/*   EXAMPLE USAGE:    
int** array1;

allocate2D(array1,1000,1000); //allocates a 1000x1000 array of ints

deallocate2D(array1,1000);    //deallocates the same array
*/
sjoytu is offline
hmmmmm
Senior Member
Join Date: Mar 2017
Location: ...
Old 02-22-2018 , 14:21   Re: Dynamic allocation Of 2D arrays
Reply With Quote #2

I'm not sure if you're meant to have C code in SourceMod tutorials section, but regardless I'm just gonna say that I prefer using 1D array then mapping to 2D so that I don't get into the allocation aids. Just 1 malloc (size=width*height)/free and thats it.

Then to access an element at x/y you can do array[x + y*width]

Last edited by hmmmmm; 02-22-2018 at 14:21.
hmmmmm is offline
TheXeon
Member
Join Date: May 2016
Location: GetClientAbsOrigin(Zeron
Old 03-16-2018 , 16:21   Re: Dynamic allocation Of 2D arrays
Reply With Quote #3

ArrayList of ArrayList?
__________________


EVERYTHING BREAKS ALWAYS!
TheXeon is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:05.


Powered by vBulletin®
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Theme made by Freecode