Ferramentas de Usuário

Ferramentas de Site


dev_geral:c:snippet:xalloc

xalloc - Alternativa ao alloc

Funções para usar em vez das tradicionais funções de alocação de memória, com verificação implícita. As funções são wrappers das funções tradicionais disponibilizadas pela stdlib, que adicionam uma chamada a uma função (que o utilizador pode escolher), em caso de erro.

Ficheiro: xalloc.h

/*
 *  GPL
 *
 *  Written by Diogo Sousa aka orium
 *  Copyright (C) 2007 Diogo Sousa
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef XALLOC_H_
#define XALLOC_H_
#include <stdlib.h>
 
extern void xalloc_set_error_func(void (*)(void));
extern void xmemerror(void);
extern void *xmalloc(size_t);
extern void *xcalloc(size_t, size_t);
extern void *xrealloc(void *, size_t);
extern char *xstrdup(const char *);
 
#endif /*xalloc.h included*/

Ficheiro: xalloc.c

/*
 *  GPL
 *
 *  Written by Diogo Sousa aka orium
 *  Copyright (C) 2007 Diogo Sousa
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
void xalloc_set_error_func(void (*f)(void));
void xmemerror(void);
void *xmalloc(size_t size);
void *xcalloc(size_t nmemb, size_t size);
void *xrealloc(void *ptr, size_t size);
char *xstrdup(const char *s);
 
static void (*mem_error_func)(void)=xmemerror;
 
void xalloc_set_error_func(void (*f)(void))
{
        mem_error_func=f;
}
 
void xmemerror(void)
{
        fprintf(stderr,"Erro na alocação de memória");
        exit(EXIT_FAILURE);
}
 
void *xmalloc(size_t size)
{
        void *r;
 
        r=malloc(size);
 
        if (r == NULL)
                mem_error_func();
 
        return r;
}
 
void *xcalloc(size_t nmemb, size_t size)
{
        void *r;
 
        r=calloc(nmemb,size);
 
        if (r == NULL)
                mem_error_func();
 
        return r;
}
 
void *xrealloc(void *ptr, size_t size)
{
        void *r;
 
        r=realloc(ptr,size);
 
        if (r == NULL)
                mem_error_func();
 
        return r;
}
 
char *xstrdup(const char *s)
{
        char *r;
 
        r=strdup(s);
 
        if (r == NULL)
                mem_error_func();
 
        return r;
}

Exemplo de uso

As funções deverão ser usadas como as funções equivalentes disponibilizadas pela stdlib.

int* x = xmalloc(sizeof(int)); // alocar inteiro com malloc
int* y = xcalloc(10, sizeof(int)); // alocar array de inteiros com calloc
dev_geral/c/snippet/xalloc.txt · Última modificação em: 2018/05/14 21:37 (edição externa)