I'm using this code to dynamically create a 2d array:
char **FileTables; int rows = 1000; int i; FileTables = (char**)malloc(rows * sizeof(char)); for (i = 0; i < rows; i++) { FileTables[i] = (char*)malloc(256 * sizeof(char)); } Problem is with 1000 rows, and there could be more, it takes a couple of seconds to allocate all the memory. Is there any faster/better method to doing this?
EDIT: Is there an advantage to using one of these methods over the other, besides the obvious simpler code?
char **FileTables; int rows = 1000; int i; FileTables = malloc(rows * sizeof(char*)); FileTables[0] = malloc(rows * 256 * sizeof(char)); for (i = 0; i < rows; i++) { FileTables[i] = FileTables[0] + i * 256; } And..
char (*FileTables)[256]; int rows = 1000; FileTables = malloc(rows * sizeof(*FileTables)); (And yes, I fixed the unnecessary casting)
for (i = (int)0; (int)i < 12; ++(int)i).++(int)idoes not compile.++requires an lvalue and(int)iis not an lvalue.