0

I used the codes below to create dynamic memory.

unsigned int *mem  ;
mem = (unsigned int*) malloc(mallocSize);

However, I prefer to create an array of pointers. Each pointers will link to one of the memory block.

arachide
  • 8,006
  • 18
  • 71
  • 134
  • 2
    ***Do not cast the return value of malloc()!*** –  Jul 13 '12 at 06:12
  • @H2CO3 why? the compiler will complain about it. – Samy Vilar Jul 13 '12 at 06:15
  • @samy.vilar about what? void * isimplicitly comptible with any pointer type! http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc –  Jul 13 '12 at 06:17
  • 1
    Casting with `malloc` is necessary in C++, but discouraged in C. – Ilmo Euro Jul 13 '12 at 06:18
  • @H2CO3 it is automatically casted to. There is no danger if user cast the return value of malloc(). Why did you say "do not cast" ? –  Jul 13 '12 at 06:19
  • `t.c:5: error: invalid conversion from ‘void*’ to ‘int*’` yep if I used `g++` it complains being that my first language was `C++` I guess I just do it out of habit thank you Euro. – Samy Vilar Jul 13 '12 at 06:20
  • @gcc if you forget to include , the compiler *will* warn you when *not* using the cast. – Ilmo Euro Jul 13 '12 at 06:21
  • @IlmoEuro but there is no danger. You can fix the warning **easily** –  Jul 13 '12 at 06:21
  • @gcc but when you do the cast, the compiler won't warn you, and if `sizeof(int) != sizeof(void *)`, you end up with a segmentation fault, or worse. – Ilmo Euro Jul 13 '12 at 06:23
  • @IlmoEuro if we think all worst case, life become miserable. –  Jul 13 '12 at 06:25
  • @gcc unfortunately the worst cases tend to be the most important, at least this is what they hammered into me at school :( with a really big hammer at that. – Samy Vilar Jul 13 '12 at 06:26
  • 2
    @gcc if you want flowers and peace, choose some other language. C is a really sharp scalpel, and you need to be extra careful with it. – Ilmo Euro Jul 13 '12 at 06:28
  • @IlmoEuro ( gcc || g++ ) && gdb && valgrind is for make our life easier. –  Jul 13 '12 at 06:36
  • You can use the [link](http://www.eskimo.com/~scs/cclass/int/sx9b.html) for the best example on Dynamically Allocating Multidimensional Arrays. – Naren Karthik Jul 13 '12 at 06:05

4 Answers4

1

I guess the below code should do it for you. You can create an array of pointers and then store the pointer to each of the memory block in each element of the array. But, the important point is that if you are having an array of unsigned int *, the size passed to malloc must be sizeof(unsigned int). You can modify the below example for other types.

unsigned int *mem[100];

for (i=0; i<100; i++)
{
  mem[i] = malloc(sizeof(unsigned int));
}
Jay
  • 24,173
  • 25
  • 93
  • 141
  • You can allocate memory for more than one `unsigned int` per array element; that's neccessary when using pointer arithmetic, for example. – Ilmo Euro Jul 13 '12 at 06:08
1

but I prefer to create an array of pointers each pointer links to one of the memory block above

unsigned int **mem = (unsigned int **)malloc(sizeof(unsigned int *) * number_of_pointers);
// memset(mem, NULL, sizeof(unsigned int *) * number_of_pointers); // recommend it but not needed here, we always set NULL for safety.
for (int index = 0; index < number_of_pointers; index++)
    mem[index] = (unsigned int *)malloc(sizeof(unsigned int) * number_of_ints);

to access individual elements mem[row_index][column_index]

to de-allocate, to reduce or remove memory leaks.

for (int index = 0; index < number_of_pointers; index++)
    free(mem[index]);
free(mem);

rule of thumb, for me anyway, free should be call as often as malloc

Samy Vilar
  • 10,800
  • 2
  • 39
  • 34
0

Arrays have always compile-time fixed size in C. If that's okay with you, use array syntax:

unsigned int *mem[NUM_PTRS];
for (int i=0; i<NUM_PTRS; i++) {
    mem[i] = malloc(mallocSize);
}

If you need to decide the size at runtime, you need a pointer-to-pointer:

unsigned int **mem;
mem = malloc(sizeof(unsigned int *) * num_ptrs);
for (int i=0; i<num_ptrs; i++) {
    mem[i] = malloc(mallocSize);
}
Ilmo Euro
  • 4,925
  • 1
  • 27
  • 29
0
unsigned int **pMemory;
pMemory = (int**)malloc(sizeof(unsigned int *) *number_of_pointers);

for(int index_to_pointer = 0; \
    index_to_pointer < number_of_pointers; \
    index_to_pointer++)

  {  pMemory[index_to_pointer] = (int*)malloc(sizeof(unsigned int));}

I think this is how we allocated dynamic double dimension memory allocation. hope this will help the purpose.

Ashutosh
  • 169
  • 7