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: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):

estdlib.h

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

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

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

void *mmalloc(size_t n, size_t m)
{
    auto void **matrix;
    register size_t i = 0, j;   
    matrix = malloc(n+1);
    if (matrix == NULL)
        return (void *) matrix;
   
    do
        *(matrix+i) = malloc(m);
    while (i < n && matrix[i++] != NULL);
   
    if (i < n) {
        for (j = 0; j <= i; ++j) {
            if (matrix[j] != NULL)
                free(matrix[j]);
        }
        free(matrix);
        matrix = NULL;
        return (void *) matrix;
    }
    *(matrix+n) = NULL;

    return *matrix;
}

void mfree(void *matrix_ptr)
{
    auto void **matrix = (void **) matrix_ptr;
    register size_t i = 0;
    if (matrix == NULL)
        return;
   
    do
        free(&matrix[i++]);
    while (matrix[i] != NULL);
    free(matrix);

    return;
}

void *mrealloc(void *matrix_ptr, size_t nmemb, size_t mmemb, size_t nsize, size_t msize)
{
    return NULL;
}

#endif


estdlib.c

#include <stdio.h>
#include "estdlib.h"

int main(int argc, char *argv[])
{
   
    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;
}


Compile with:
gcc -ansi -pedantic -Wall -O0 -o estdlib estdlib.c

When executed, prints the following message:

*@star:~/devel/c$ ./estdlib
   0    1    2    3    4
   1    2    3    4    5
   2    3    4    5    6
   3    4    5    6    7
   4    5    6    7    8
*** glibc detected *** ./estdlib: double free or corruption (out): 0x0000000001758050 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x78a8f)[0x7f3822ab3a8f]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x73)[0x7f3822ab78e3]
./lstdlib[0x4006d1]
./lstdlib[0x400821]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xff)[0x7f3822a59eff]
./lstdlib[0x4004c9]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:03 665109                             /home/*/devel/c/estdlib
00600000-00601000 r--p 00000000 08:03 665109                             /home/*/devel/c/estdlib
00601000-00602000 rw-p 00001000 08:03 665109                             /home/*/devel/c/estdlib
01758000-01779000 rw-p 00000000 00:00 0                                  [heap]
7f381c000000-7f381c021000 rw-p 00000000 00:00 0
7f381c021000-7f3820000000 ---p 00000000 00:00 0
7f3822825000-7f382283a000 r-xp 00000000 08:03 623067                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f382283a000-7f3822a39000 ---p 00015000 08:03 623067                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f3822a39000-7f3822a3a000 r--p 00014000 08:03 623067                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f3822a3a000-7f3822a3b000 rw-p 00015000 08:03 623067                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7f3822a3b000-7f3822bc5000 r-xp 00000000 08:03 622980                     /lib/x86_64-linux-gnu/libc-2.13.so
7f3822bc5000-7f3822dc4000 ---p 0018a000 08:03 622980                     /lib/x86_64-linux-gnu/libc-2.13.so
7f3822dc4000-7f3822dc8000 r--p 00189000 08:03 622980                     /lib/x86_64-linux-gnu/libc-2.13.so
7f3822dc8000-7f3822dc9000 rw-p 0018d000 08:03 622980                     /lib/x86_64-linux-gnu/libc-2.13.so
7f3822dc9000-7f3822dcf000 rw-p 00000000 00:00 0
7f3822dcf000-7f3822df0000 r-xp 00000000 08:03 622977                     /lib/x86_64-linux-gnu/ld-2.13.so
7f3822fc9000-7f3822fcc000 rw-p 00000000 00:00 0
7f3822fec000-7f3822fef000 rw-p 00000000 00:00 0
7f3822fef000-7f3822ff0000 r--p 00020000 08:03 622977                     /lib/x86_64-linux-gnu/ld-2.13.so
7f3822ff0000-7f3822ff2000 rw-p 00021000 08:03 622977                     /lib/x86_64-linux-gnu/ld-2.13.so
7fffa7f75000-7fffa7f96000 rw-p 00000000 00:00 0                          [stack]
7fffa7fff000-7fffa8000000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted

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