Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Allocation of a simple matrix

Name: Anonymous 2011-07-02 8:03


#ifndef __ESTDLIB_H__
#define __ESTDLIB_H__
#include <stdlib.h>

void **mcalloc(size_t nmemb, size_t mmemb, size_t size);
void mmalloc(void ***matrix,size_t n, size_t m);
void mfree(void **matrix, size_t n);
void **mrealloc(void **matrix, size_t n, size_t m, size_t nsize, size_t msize);

void **mcalloc(size_t nmemb, size_t mmemb, size_t size)
{
    return NULL;
}

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: Anonymous 2011-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.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List