What is the LD_PRELOAD trick?

If you set LD_PRELOAD to the path of a shared object, that file will be loaded before any other library (including the C runtime, libc.so). So to run ls with your special malloc() implementation, do this:

How do I solve the following errors: “Undefined reference to WinMain”, “[Error] Id returned 1 exit status”?

This error means that the linker is looking for a function named WinMain to use as the entry point. It would be doing that because you configured the project to target the GUI subsystem, but did not provide a WinMain function. My guess is that you want to produce a console application and have provided a main function. Target the console … Read more

Using ssize_t vs int

There’s no guarantee in the POSIX standard that sizeof(int) >= sizeof(ssize_t), nor the other way around. Typically ssize_t is larger than int, but the safe and portable option in C99 is to use intmax_t instead for the argument and the return value. The only guarantees you have wrt. the relationship between int and ssize_t are: int can store values of at least the range [-2^15 … … Read more

munmap_chunk(): invalid pointer

In the function second(), the assignment word = “ab”; assigns a new pointer to word, overwriting the pointer obtained through malloc(). When you call free() on the pointer later on, the program crashes because you pass a pointer to free() that has not been obtained through malloc(). Assigning string literals does not have the effect of copying their content as you might have thought. To … Read more

Categories C Tags

Error “initializer element is not constant” when trying to initialize variable with const

In C language, objects with static storage duration have to be initialized with constant expressions, or with aggregate initializers containing constant expressions. A “large” object is never a constant expression in C, even if the object is declared as const. Moreover, in C language, the term “constant” refers to literal constants (like 1, ‘a’, 0xFF and so on), enum members, and results of … Read more

What does #pragma once mean in C? [duplicate]

In the C and C++ programming languages, #pragma once is a non-standard but widely supported preprocessor directive designed to cause the current source file to be included only once in a single compilation. Thus, #pragma once serves the same purpose as #include guards, but with several advantages, including: less code, avoiding name clashes, and improved … Read more

The importance of c enumeration (typedef enum) [duplicate]

I recently saw this in an answer that was posted for me: What is typedef enum and why should we use it? I googled and found the following: http://www.programiz.com/c-programming/c-enumeration Right now it sounds slightly too technical for me so I dont think I understand what is going on or why one would use that. Bonus (optional): … Read more

Categories C Tags