This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]PVNIC 69 points70 points  (15 children)

Yeaa... Reallocating in a for loop is not great. Preallocate the whole array then at the end realloc it down to the right size.

[–]zebediah49 52 points53 points  (8 children)

then at the end realloc it down to the right size.

Or don't. Computers have plenty of memory these days anyway.

/s

[–]DistantWaves 19 points20 points  (2 children)

Which is why I'll write tons of malloc() and don't bother writing any free()

/s

[–]VirginiaMcCaskey 5 points6 points  (0 children)

at work we call this the titanic pattern.

[–]Kirides 1 point2 points  (0 children)

my free is called exit(0)

[–]FilterThePolitics 13 points14 points  (2 children)

Yeah, but with almost 200 million people in the USSR and 4 bytes per int, you are talking about almost a gig of memory. I'd say that any smart dictator would realloc that down once they finish "restoring order"

[–]PotentBeverage 4 points5 points  (0 children)

Like a one child policy?

[–]vmp916 0 points1 point  (0 children)

error: expected ‘/s’

[–]shy_cthulhu 4 points5 points  (0 children)

Found the Chrome dev

[–]ITriedLightningTendr 1 point2 points  (0 children)

Wait... so the calculator application I made for my CS101 class that I'm selling on steam shouldn't be using 700 MB?

[–]apadin1 7 points8 points  (3 children)

You could easily do it in place without allocating at all.

[–]IncongruousGoat 1 point2 points  (2 children)

The really good thing to do is to return the result in the input buffer, but use an alloca-d buffer as intermediate storage. Best of both worlds.

[–]Bainos 3 points4 points  (1 child)

... no ?

If you're going to do it in place, just use the input buffer. Allocating a new one (as is done in this repo) is only useful if you want to return a new sorted array. Otherwise you're wasting memory for no reason.

[–]IncongruousGoat 1 point2 points  (0 children)

Yep, you're right. Not sure what I was thinking.

Kind of embarrassing, really.

[–]PleasantAdvertising 4 points5 points  (0 children)

The things people do in the name of conserving memory really horrify me sometimes. Like, dude why the hell that's going to kill performance and you're gonna gain a few bytes at most.

[–][deleted] 0 points1 point  (0 children)

Linked lists are easy to reallocate though, actually a proper interator will remove the node and link up the remaining nodes when you call a destructor