this post was submitted on 31 Aug 2025
330 points (96.3% liked)

Programmer Humor

26373 readers
1272 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 2 years ago
MODERATORS
 

Finally I have a valid reason to learn about memory management. It was also hella weird when encountering it.

top 50 comments
sorted by: hot top controversial new old
[–] boredsquirrel@slrpnk.net 47 points 2 weeks ago (2 children)

Unused memory is wasted memory

[–] mmmac@lemmy.zip 23 points 2 weeks ago (1 children)

Cloud providers LOVE you with this one quick trick!

[–] henfredemars@infosec.pub 3 points 2 weeks ago

Also goes for mobile. You use more memory and apps get killed.

[–] sus@programming.dev 7 points 2 weeks ago

and with a good enough leak, the amount of unused memory will become negative!

[–] henfredemars@infosec.pub 28 points 2 weeks ago* (last edited 2 weeks ago) (5 children)

Not freeing your memory at all is a memory management strategy. I think some LaTeX compilers use it as well as surprisingly many Java applications.

[–] cows_are_underrated@feddit.org 8 points 2 weeks ago (2 children)

That's the funny thing. I had a (yet) very basic Programm and did not care at all about memory management. When I did some testing I realised, that for some reason when I printed string 1 I also got characters from string 2.

[–] henfredemars@infosec.pub 12 points 2 weeks ago

That sounds like it could be memory corruption. That should not happen because every string should be separated by a null terminator.

[–] NightFantom@slrpnk.net 2 points 2 weeks ago (1 children)

Sounds interesting, want to share a minimal example?

[–] cows_are_underrated@feddit.org 9 points 2 weeks ago* (last edited 2 weeks ago) (3 children)

This is the code I used:

#include <stdio.h>
#include <string.h>

#define MAX_ACCOUNTS 255

typedef struct
{
    unsigned int id;
    char account_creation_date [10];
    char first_name [255];
    char last_name [255];
    char country_code [2];
    unsigned int iban;
    char password [255];
    double balance;
} account;

account accounts_db[MAX_ACCOUNTS];
unsigned int accounts_created = 0;

account get_account_id (unsigned int id)
{
    int i = 0;
    while(i < MAX_ACCOUNTS)
    {
        if(accounts_db[i].id == id)
        {
            return accounts_db[i];
        }
        i++;
    }
    account account;
    account.id = -1;
    return account;
}

void create_account(char first_name [255], char last_name [255], char password [255], char country_code [2])
{
    account new_account;
    new_account.id = accounts_created;
    strcpy(new_account.first_name, first_name);
    strcpy(new_account.last_name, last_name);
    strcpy(new_account.password, password);
    strcpy(new_account.country_code, country_code);
    strcpy(new_account.account_creation_date, "");
    new_account.balance = 0.0;
    new_account.iban = 0;
    accounts_db[accounts_created] = new_account;
    accounts_created++;
}

int main()
{
    char first_name [255]  = "Max";
    char last_name [255] = "Mustermann";
    char country_code [2] = "DE";
    char password [255]= "password";
    create_account(first_name, last_name, password,country_code);
    account account = get_account_id(0);
    printf("Name: %s %s \n", account.first_name, account.last_name);
    printf("Account creation date: %s\n", account.account_creation_date);
    printf("IBAN: %s %d", account.country_code, account.iban);
}```

When you run it you can see, that behind the country code of the IBAN you get the first two letters of the surename
[–] LedgeDrop@lemmy.zip 17 points 2 weeks ago (2 children)

Without getting too critical of your code (congrats BTW), never use strcpy instead use strlcpy.

strcpy will happily allow you to create buffer overflows (a common challenge with C) which will cause your application to crash.

You'll find more details here.

Good luck!

[–] cows_are_underrated@feddit.org 10 points 2 weeks ago

Thanks, I did not knew this. I always appreciate constructive criticism. I am quite new to C so theres a shit ton of stuff I have never done or dont even know about.

[–] ozymandias117@lemmy.world 6 points 2 weeks ago

And understand when you can use them...

I've seen too much code following this advice blindly that just does something like

strncpy(dst, src, strlen(src))

[–] cows_are_underrated@feddit.org 8 points 2 weeks ago (2 children)

I found the mistake. Since the country code char array only has a size of 2 it overwrites the \0 char causing the memory to leak.

[–] silasmariner@programming.dev 16 points 2 weeks ago

Usually what's meant by a memory leak is memory that's allocated but never freed. Writing outside of array allocation would usually be considered an overflow. Which sounds kinda similar but is not the same.

load more comments (1 replies)
load more comments (1 replies)
[–] wewbull@feddit.uk 8 points 2 weeks ago

.net

Anything I run in C# or similar seems to allocate 512GB of virtual address space and then just populates what it actually uses.

[–] entwine@programming.dev 6 points 2 weeks ago (1 children)

This non-sarcastically. The operating system is better at cleaning up memory than you, and it's completely pointless to free all your allocations if you're about to exit the program. For certain workloads, it can lead to cleaner, less buggy code to not free anything.

It's important to know the difference between a "memory leak" and unfreed memory. A leak refers to memory that cannot be freed because you lost track of the address to it. Leaks are only really a problem if the amount of leaked memory is unbounded or huge. Every scenario is different.

Of course, that's not an excuse to be sloppy with memory management. You should only ever fail to free memory intentionally.

[–] henfredemars@infosec.pub 3 points 2 weeks ago* (last edited 2 weeks ago)

Absolutely. I once wrote a server for a factory machine that spawned child processes to work each job item. Intentionally we did not free any memory in the child process because it serves only one request and then exits anyway. It’s much more efficient to have the OS just clean up everything and provides strong guarantees that nothing can be left behind accidentally for a system where up time was money. Any code to manage memory was pointless line noise and extra developer effort.

In fact I think in the linker we specifically replaced free with a function that does nothing.

[–] csm10495@sh.itjust.works 5 points 2 weeks ago (1 children)

Upvoted. This is something I learned rather recently. Sometimes it's more performant to slowly leak than it would be to free properly. Then take x amount of time to restart every n amount of time.

load more comments (1 replies)
load more comments (1 replies)
[–] ulterno@programming.dev 16 points 2 weeks ago (1 children)

Back when I was a kid and was learning C, I used to wonder why people considered pointers hard.
My usage of pointers was like:

void func (int * arg1)
{
    // do sth with arg1
}
int main ()
{
    int x;
    func (&x);
    return 0;
}

I didn't know stuff like malloc and never felt the need in any of the program logic for the little thingies I made.
Pointers are not hard. Memory management makes it hard.

load more comments (1 replies)
[–] bjoern_tantau@swg-empire.de 12 points 2 weeks ago

You haven't lived until you've produced a memory leak in JavaScript.

[–] favoredponcho@lemmy.zip 9 points 2 weeks ago

Valgrind to the rescue

[–] JasonDJ@lemmy.zip 9 points 2 weeks ago* (last edited 2 weeks ago) (1 children)

Congratulations! Now you can get a job at Fortinet.

(Fortinet is a network security vendor...think firewalls, HLBs, etc. They get an ungodly amount of memory leak bugs, or at least far more than you would expect from an enterprise firewall)

[–] diemartin@sh.itjust.works 3 points 2 weeks ago

Get a job playing Fortnite, got it

(Insert image of Kronk here)

[–] resipsaloquitur@lemmy.world 9 points 2 weeks ago (1 children)

RAII.

Can’t leak what never leaves the stack frame.

[–] CookieOfFortune@lemmy.world 10 points 2 weeks ago (1 children)
[–] resipsaloquitur@lemmy.world 7 points 2 weeks ago (1 children)

Classes are just pretentious structs.

[–] CookieOfFortune@lemmy.world 3 points 2 weeks ago (3 children)

How do you get destructor behavior in C?

load more comments (3 replies)
[–] _cryptagion@anarchist.nexus 6 points 2 weeks ago (2 children)

is this some genX meme that I'm too millennial to understand?

[–] cows_are_underrated@feddit.org 17 points 2 weeks ago (1 children)

No. I am just learning C and was curious how long it would take to get my first memory leak.

[–] PlutoParty@programming.dev 3 points 2 weeks ago (1 children)

I think the confusion comes from this not exactly being a positive thing. It reads almost like a new driver saying, "Thank God I finally got my first speeding ticket!"

[–] Jankatarch@lemmy.world 3 points 2 weeks ago* (last edited 2 weeks ago)

I did my first "that one thing where you park and want to press breaks but press gas instead by accident."

Gen-x likes to call it "right of passage."

[–] alsaaas@lemmy.dbzer0.com 4 points 2 weeks ago* (last edited 2 weeks ago) (1 children)

Learn some C you whippersnapper! You ain't gonna become a proper programmer unless you understand your basic low level concepts well!

The youth nowadays, always hiding behind abstractions of abstractions that 90% of the time are just C wrappers anyway /j

(written by an early 20s PHP dev LOL. Although I do plan on learning C quite a bit before I move to C++ and ultimately Rust)

[–] Crashumbc@lemmy.world 4 points 2 weeks ago (2 children)

You're not a "real" programmer unless you started with FORTRAN :p

load more comments (2 replies)
[–] iAvicenna@lemmy.world 6 points 2 weeks ago (1 children)
[–] cows_are_underrated@feddit.org 3 points 2 weeks ago (1 children)

I did not knew this existed, so thanks for the tip.

load more comments (1 replies)
[–] diemartin@sh.itjust.works 5 points 2 weeks ago

It'll be fun when you get to funny errors because you used freed memory.

When I was learning about linked lists and decided to use them in a project, I "removed" items by making the previous item's next point to this item's next, except I misplaced a call to free before using the fields, and it somehow still worked most of the time on debug builds, but on optimized builds it would cause a segmentation fault 100% of the time.

load more comments
view more: next ›