I'd like to take advantage of MALLOC_PERTURB_ environment variables that can change memory allocation parameters (man 3 mallopt). However, I'd like to control allocation parameters on application level, not entire system level. Ideally, if I could control them through project's makefile. I've tried to change mentioned variables through makefile, yet, without success.
For testing, I've created this test.c file:
#include<stdlib.h>
#include<stdio.h>
#define N 50
int main()
{
char *chars;
int i;
if (NULL == (chars = malloc(N * sizeof(*chars))))
return EXIT_FAILURE;
free(chars);
for (i = 0; i < N; ++i)
printf("%c", chars[i]);
printf("\n");
return EXIT_SUCCESS;
}
Yes, I'm aware that I am reading from freed memory, yet that's a whole point of using MALLOC_PERTURB_.
Expected result: 50 chars of ASCII character of value MALLOC_PERTURB_.
Close enough:
$ export MALLOC_PERTURB_=97
$ gcc test.c -o test
$ ./test
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa��
$ export MALLOC_PERTURB_=105
$ gcc test.c -o test
$ ./test
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii��
Then I've tried to enclose compilation in makefile, yet without success.
Exporting variable (as proposed here)
Makefile
all:
export MALLOC_PERTURB_=110
gcc test.c -o test
Result (I was expecting 'n' letters for 110)
$ export MALLOC_PERTURB_=105
$ make
export MALLOC_PERTURB_=110
gcc test.c -o test
$ ./test
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii��
Calling make recursively (as proposed here)
Makefile
all:
MALLOC_PERTURB_=110
$(MAKE) rec
rec:
gcc test.c -o test
Result (I was expecting 'n' letters for 110)
$ export MALLOC_PERTURB_=105
$ make
MALLOC_PERTURB_=110
make rec
make[1]: Entering directory '~/test'
gcc test.c -o test
make[1]: Leaving directory '~/test'
$ ./test
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii��
I've tried to find any inspiration from github's makefiles that included MALLOC_PERTURB_, yet they are too complex for me to understand. Some examples: (1), (2), (3)
Some technical info:
Linux 4.0.1-1-ARCH x86_64
gcc version 4.9.2 20150304 (prerelease) (GCC)
GNU Make 4.1