[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Why does bash use xmalloc?
From: |
Peng Yu |
Subject: |
Why does bash use xmalloc? |
Date: |
Sat, 5 Nov 2016 08:21:06 -0500 |
Hi, The following example shows that bash uses xmalloc. But it seems
that using xmalloc is not a good practice. Is it better to use malloc
instead of xmalloc? In this test case, after `./main 1000000` failed I
still want to run the rest commands. So it sounds like malloc is
better.
http://stackoverflow.com/questions/7590254/what-is-the-difference-between-xmalloc-and-malloc
~$ cat main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:
ulimit -Sv 1000
set -v
./main 100000
./main 1000000
./main 10000000
./main 100000000
~$ cat main.c
// vim: set noexpandtab tabstop=2:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
//printf("%s\n", "Hello World!");
size_t size = atoi(argv[1]);
void *ptr;
if((ptr = malloc(size)) == NULL) {
perror("malloc");
exit(2);
} else {
printf("%p\n", ptr);
free(ptr);
}
return 0;
}
~$ ./main.sh
./main 100000
./main.sh: xmalloc: .././variables.c:3997: cannot allocate 1313 bytes
(53248 bytes allocated)
--
Regards,
Peng
- Why does bash use xmalloc?,
Peng Yu <=