Re: My solution to allocating memory



Here is an example snippet with this code.

-- snip.c --
#include "xmalloc.c"

void oh_no(void) { fprintf(stderr, "Something went wrong.\n"); }
int main(void) {
size_t n;
void *p;

xmalloc_init();
atexit(oh_no); /* its just an example so i dont check return value
*/

for(n = 0; n < 1024; n++)
(void)xmalloc(1, 0); /* no memory leak, the pointer is held
somewhere */
printf("allocated 1024 xmalloc(1, 0)!\n");
xmalloc_end(); /* free our resources */
xmalloc_init();
p = xmalloc(sizeof "hello world\n", 0);
strcpy(p, "hello world\n");
printf("%s", p);
xfree(p);
return 0;
}
-- snip.c --
.



Relevant Pages

  • Re: confusion: casting function pointers
    ... pointer from the 'actual/other modules' that takes arguments of type ... list to types of void *). ... int main{ ... without a prototype, a number of special "promotion" rules take ...
    (comp.lang.c)
  • Re: invalid pointer adress
    ... >> pointer causes the program to exit with a core dump. ... struct s2{void *p;}; ... int leseExterneHinweise_masch_storno ... typedef struct s_AusdatFeldbeschreibung ...
    (comp.lang.c)
  • Should io(read|write)(8|16|32)_rep take (const|) volatile u(8|16|32) __iomem *addr?
    ... the destination pointer on user-space ... @src: ... const void *data, int bytelen); ...
    (Linux-Kernel)
  • Re: function pointer help!
    ... //the return void and input prameters are defined in the manual... ... void MyProjectView::CallHandler(int,unsigned int, unsigned int, void*) ... You are attempting to use a C++ member function as the callback, but the callback is defined in terms or C, not C++. ... The underlying problem is that C++ functions receive a hidden parameter, the 'this' pointer, so their signature is incompatible with C definitions. ...
    (microsoft.public.vc.mfc)
  • Re: Whats the meaning of this code
    ... void * work ... Whats the meaning of this line:- ... The meaning...a function called work that accepts a pointer of type ... pointer value is cast to an int value. ...
    (comp.lang.c)