What are you working on, Jow Forums?
Last thread:
What are you working on, Jow Forums?
Last thread:
I got stuck on this book by chapter 2, too much math for a brainlet like me.
It worked because &XXX was getting interpreted by the browser as a character code like or similar.
Daily offer to prove that C++ is actually no really guys I'm serious capable of performing outstanding memory allocation optimisations:
>Then why don't you go ahead and run some C++ program with an overloaded malloc implementation that counts how many 64-Bytes-or-less allocations have been performed? And with a corresponding free overload you can see how long they survived. I also don't care about your synthetic benchmarks for a simple program, all right? I want real-world applications.
, and that's why it's better than C.
>inb4 the compiler cannot apply its optimisations
Most real-world applications link their shit dynamically. Dynamic linking means you can inject symbols via LD_PRELOAD on Linux (no idea about the interface on Windows however). Since the compiler has NOTHING to do with symbol resolving that should give you a proper idea of how much a C++ compiler is actually capable of optimisation.
>inb4 I'm too dumb to do that; gimme code/instructions!
Take the following code and put it into libcm.c:
/*$ gcc libcm.c -c -fPIC -o libcm.o -O3 -Wno-unused-result && gcc -shared -Wl,-soname,libcm.so.0 -o libcm.so.0.1 libcm.o
**$ LD_PRELOAD=./libcm.so.0.1 */
#include /*time*/
#include /*write*/
#include /*strlen*/
#include /*sprintf*/
/*No include file introduces the __libc_* symbols, but they're still present
**in libc.so - see:
**`readelf -s /lib/x86_64-linux-gnu/libc.so.6 | grep 'malloc` or whatever your
**glibc path is. As such we can simply introduce them here and then rely on
**the linker to resolve them for us.*/
extern void* __libc_malloc(size_t);
extern void* __libc_calloc(size_t,size_t);
extern void* __libc_realloc(void*,size_t);
extern void __libc_free(void*);
#define PRINT_SHIT
#define OUTPUT_LENGTH_MAX (128)
#define DO_PRINT write(STDERR_FILENO,output,output_length)
static size_t counters[4] = {0};
Oh that makes sense. Thanks again!
What exactly is a module? I just installed Eclipse after using a very simple IDE called TextPad. Upon creating a project I was prompted to make a module. I looked up what such a thing is in the context of java programming in this IDE, but I unfortunately still don't really understand what it is. As for now I'm going to just delete the file and create the classes I need.
__attribute__((destructor))
void cm_stats(void)
{
char output[OUTPUT_LENGTH_MAX];
int output_length;
output_length = sprintf
(
output,
"Mallocs: %lu\n"
"Callocs: %lu\n"
"Reallocs: %lu\n"
"Frees: %lu\n\n",
counters[0],
counters[1],
counters[2],
counters[3]
);
DO_PRINT;
}
/*Unfortunatey we cannot print the time in a proper fashion because calling
**localtime_r causes the program to lock up. Hard. I cannot even fathom what
**sort of lock it is that localtime_r and malloc are contesting for, and to be
**quite frank I don't fucking care either.*/
void*malloc(size_t size)
{
/*We cannot use printf/fprintf because those functions may request
**memory themselves. That's why we sprintf everything into our
**own buffer and write it directly to stderr.*/
#ifdef PRINT_SHIT
char output[OUTPUT_LENGTH_MAX];
int output_length; /*WHY THE FUCK IS THIS SIGNED?*/
time_t ut;
#endif
void*ptr;
ptr = __libc_malloc(size);
++counters[0];
#ifdef PRINT_SHIT
time(&ut);
output_length = sprintf(output,"[%lu]: Allocate %p|%lu\n",ut,ptr,size);
DO_PRINT;
#endif
return ptr;
}
void*calloc(size_t nmemb,size_t size)
{
#ifdef PRINT_SHIT
char output[OUTPUT_LENGTH_MAX];
int output_length;
time_t ut;
#endif
void*ptr;
ptr = __libc_calloc(nmemb,size);
++counters[1];
#ifdef PRINT_SHIT
time(&ut);
output_length = sprintf(output,"[%lu]: Callocate %p|%lu\n",ut,ptr,nmemb * size);
DO_PRINT;
#endif
return ptr;
}
void*realloc(void*ptr,size_t size)
{
#ifdef PRINT_SHIT
char output[OUTPUT_LENGTH_MAX];
int output_length;
time_t ut;
#endif
void*ptr_new;
ptr_new = __libc_realloc(ptr,size);
++counters[2];
#ifdef PRINT_SHIT
time(&ut);
output_length = sprintf(output,"[%lu]: Reallocate %p to %p|%lu\n",ut,ptr,ptr_new,size);
DO_PRINT;
#endif
return ptr_new;
}
void free(void*ptr)
{
#ifdef PRINT_SHIT
char output[OUTPUT_LENGTH_MAX];
int output_length;
time_t ut;
#endif
__libc_free(ptr);
++counters[3];
#ifdef PRINT_SHIT
time(&ut);
output_length = sprintf(output,"[%lu]: Free %p\n",ut,ptr);
DO_PRINT;
#endif
}
>inb4 cnile
>still no data
>still winning
¯\_(ツ)_/¯
You guys have been arguing about this for 3 days. This is like those threads on Canadian grocery prices.
There's barely any math in SICP. Try using the video lectures too, they're entertaining.
(it doesn't cost $10 anymore)