void mmalloc(void ***matrix, size_t n, size_t m)
{
register unsigned long int i, j;
if (matrix == NULL)
return;
*matrix = malloc(n);
if (*matrix == NULL)
return;
for (i = 0; i < n; ++i) {
*((*matrix)+i) = malloc(m);
if (*((*matrix)+i) == NULL)
break;
}
if (i < n+1) {
for (j = 0; j <= i; ++j) {
if (*((*matrix)+j) != NULL)
free(*((*matrix)+j));
}
free(*matrix);
}
return;
}
void mfree(void **matrix, size_t n)
{
register size_t i;
for (i = 0; i < n; ++i)
free(*(matrix+i));
free(matrix);
}
void **mrealloc(void **matrix, size_t n, size_t m, size_t nsize, size_t msize)
{
return NULL;
}
#endif
When I'm back from 2 weeks vacation, I'll bet that /prog/ (even the wizards) haven't solved this mystery. The amount segfault and other corruptions I got from my own code was amazing. Enjoy.
Name:
Anonymous2011-07-02 8:05
if (i < n+1) should be if (i < n)
Name:
Anonymous2011-07-02 8:06
Multidimensional arrays are stupid. Just malloc(n * m * sizeof(element)).
Reallocation is probably marginally more difficult, but at least this ensures contiguous memory.
Name:
Anonymous2011-08-03 18:21
>>4
Outstanding sir. I must say, I feel pretty stupid of not realising such a simple solution. Still, I'm bothered by my own solution. I can allocate a matrix, but the mfree'ing makes the program abort and I know that there is a grave design error somewhere, but I can't figure it out. Could somebody give me a hint?
I rewrote the code again today (with the flaw still being there):
auto unsigned int i, j, n = 5, m = 5;
auto unsigned int **matrix = (unsigned int **) mmalloc(sizeof(*matrix)*n,sizeof(**matrix)*m);
for (i = 0; i < n; ++i) {
for (j = 0; j < m; ++j)
matrix[i][j] = i+j;
}
for (i = 0; i < n; ++i) {
for (j = 0; j < m; ++j)
printf("%4u%c",matrix[i][j],j+1<m?' ':'\n');
}
mfree((void *) matrix);
matrix = NULL;
return 0;
}
I didn't look at your code, but as I said before, allocating matrix rows is completely retarded. Just malloc(n * m * sizeof(element)).
Name:
Anonymous2011-08-03 18:43
>>6
True and I know that I can use pointer arithmetic and % to do the memory address calculations. Sorry but I have a habit of first implementing a working naive solution and then think about how one could improve it; not some constant reduction but asymptotically.
>>3
It's way too much of a pain to have to calculate index location by yourself. ...It could be slightly better if I use macros though. Anyway my 2D array allocation and free as macros.
/* 2D array alloc */
#define MALLOC2(TYPE,I,m,n) \
({ \
int i; \
I = (TYPE**) malloc((m)*sizeof(TYPE*)); \
for(i=0; i<(m); i++) I[i] = (TYPE*) malloc((n)*sizeof(TYPE)); \
})
I'M PROGRAMMER
SON OF A BITCH C
C IS PIG
DO YOU WANT A COMPLETELY ILLEGIBLE CODE?
DO YOU WANT A BUFFER OVERFLOW?
C IS PIG DISGUSTING
DENNIS RITCHIE IS A MURDERER
FUCKING COMPUTER
Name:
Anonymous2011-08-04 14:24
If you sincerely believe that >>4 is hard to read you need to re-evaluate your decision to visit a programming textboard, there are two things happening and rest is error checking.
Contiguous memory is important, stop mallocing inside of loops.
Name:
52011-08-04 15:16
>>27
Is it to avoid memory fragmentation?
If yes, doesn't it become problematic when the memory is very fragmented and you want to allocate a very large block?
Name:
Anonymous2011-08-04 15:21
>>28
It's also very bad for memory caching, which is basically the thing that matter the most for performance nowadays.
Name:
Anonymous2011-08-04 17:39
>>28
Once you have virtual memory (which every OS now has), you don't have to worry about allocating large blocks. They would be spread out in physical memory if there is no contiguous space. And yeah, contiguous memory is important b/c of caching.