C – freeing structs

Simple answer : free(testPerson) is enough . Remember you can use free() only when you have allocated memory using malloc, calloc or realloc. In your case you have only malloced memory for testPerson so freeing that is sufficient. If you have used char * firstname , *last surName then in that case to store name … Read more

Is malloc thread-safe?

Question: “is malloc reentrant”? Answer: no, it is not. Here is one definition of what makes a routine reentrant. None of the common versions of malloc allow you to re-enter it (e.g. from a signal handler). Note that a reentrant routine may not use locks, and almost all malloc versions in existence do use locks … Read more

How to find the cause of a malloc “double free” error?

When an object is “double-freed”, the most common cause is that you’re (unnecessarily) releasing an autoreleased object, and it is later autoreleased when the containing autorelease pool is emptied. I’ve found that the best way to track down the extra release is to use the NSZombieEnabled environment variable for the affected executable in Xcode. For … Read more

When and why to use malloc

malloc is used for dynamic memory allocation. As said, it is dynamic allocation which means you allocate the memory at run time. For example, when you don’t know the amount of memory during compile time. One example should clear this. Say you know there will be maximum 20 students. So you can create an array … Read more

How does jemalloc work? What are the benefits?

jemalloc first appeared for FreeBSD, the brainchild of one “Jason Evans”, hence the “je”. I would ridicule him for being egotistical had I not once written an operating system called paxos 🙂 See this PDF for full details. It’s a white paper describing in detail how the algorithms work. The main benefit is scalability in … Read more

Why does `free` in C not take the number of bytes to be freed?

One-argument free(void *) (introduced in Unix V7) has another major advantage over the earlier two-argument mfree(void *, size_t) which I haven’t seen mentioned here: one argument free dramatically simplifies every other API that works with heap memory. For example, if free needed the size of the memory block, then strdup would somehow have to return … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)