allocating memory in c

I recently had a need to generate arbitrary memory usage with arbitrary binary names for a monitoring effort I was working on. My typical go-to for generating resource usage is the excellent stress utility, but this didn’t meet the arbitrary binary name criteria, and I’m pretty sure the memory workers it spins up just consume as much memory as possible.

So, I wrote a quick and dirty C program to do the job. My first attempt was a simple malloc(arbitrary_bytes) followed by a sleep (shut up, I said it was quick and dirty =D), but interestingly, the binary ran with almost no memory usage. The reason is apparently that while malloc allocates the requested memory, it doesn’t initialize it. Switching this call to calloc(num_elements, element_size) produced the desired behavior. Huzzah!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s