It's possible to write a C program without memory allocation?

Attached: battering-ram-memmory.jpg (425x351, 100K)

Other urls found in this thread:

man7.org/linux/man-pages/man3/alloca.3.html
twitter.com/AnonBabble

no

what do you mean by "memory allocation"?

Depends on how the program is designed. The OS allocates a stack for each thread, you could (ab)use it to allocate memory as needed without ever using malloc, or even implement malloc around stack memory, but it's a very fast lane to segfault city.

I wrote a small server this way once, all global state was allocated on the main thread's stack, worker threads had their own state in each stack. It was barely larger than an instance of sh (as in ash, not the fat pig that is bash).

A better question would be why you would want to do that, other than being too afraid of leaking some.

idiot

it's impossible to write a program in any language without allocating at least some memory

It’s possible to write a C program without malloc.

maybe an embedded software?

You'd be limited to a stack size of under 2MB for every stack frame, that's pretty worthless.

>tfw entire operating systems and productivity suites used to fit in 1MB

Attached: 9732490257_648fc3a1a9_h-780x520.jpg (780x520, 80K)

Not a clear question.

A program must be loaded into memory to run. But it doesn't allocate additional memory unless done explicitly, either by declaring an array (in which case it's done before hand) or allocated dynamically (done when/if needed).

So by writing a program that doesn't use any additional memory, the compiler decides if and when to use the stack. During function calls, if they can't be inlined, return pointers are saved to the stack. Also certain registers are saved to the stack during C calls.

The only way to make sure the program doesn't allocate additional memory is to write it in asm without C calls.

>tfw entire operating systems and productivity suites used to fit in 1MB
Just what the fuck happened

Nothing. Start writing everything assembly again. Problem solved.

This is what makes C great, why wouldnt you want to?

right or just use C because muh architecture compatibility

As hardware got cheaper, software managers came to the conclusion that was also cheaper sacrifice computational resources using high level programming languages in order to save time and money they would use for programmers. As hardware gets faster and cheaper, businesses tends to add more and more layers of abstraction.

Attached: sacrifice.jpg (754x782, 995K)

Depends on how strict/autistic you're about your question. You can write C code without using malloc and similars, but there will be dynamic allocations when the os starts your application.

Just write all your programs in llvm.

man7.org/linux/man-pages/man3/alloca.3.html

Sounds nightmarish desu, but also interesting.

Early Mac programs were Pascal, C, and C++.

>using high level programming languages in order to save time and money
Do they save any time and money? And do they need to be so bloated? I'm not impressed on either point.

There's a NASA document somewhere that has guidelines for deep space probe software.

I seem to recall that one rule was no dynamic memory allocation. Now I don't know what the compilers do under the hood...if everything is on the stack or not...but basically no alloc/malloc/etc.

how'd it perform?

Assuming you mean without dynamic allocation, e.g. malloc / calloc, then yeah, it's quite easy.
Just use the stack (local variables) for the most part, and for large chunks of memory, use global array variables / static local array variables and then you aren't limited by the size of the stack.
You can have one big char mem_block[MEMORY_SIZE]; as a global variable and split it up as you need it and implement your own malloc / free equivalents using similar algorithms to those used by C libraries. While it is technically undefined behaviour to cast random pointers within this memory block to any type you want like you would with malloc, you should be fine in practice if you align them adequately. Or alternatively, you could use separate arrays for different types just to be strictly standards conforming.
If the only functions you have which require big blocks of memory don't get called recursively / in different threads / with a lot of shared state, then you should be able to just use fixed arrays as you need them as static local variables and not bother with a makeshift malloc at all.

Of course you can. What do you think a kernel is?

>Sounds nightmarish
As long as you know the limits and can constrain the program appropriately it's doable. Especially if you explicitly set the size of thread stacks when creating them, then you're only bound by system limitations.

>how'd it perform?
Like any other C program minus the overhead of malloc, pretty nippy. It was more of an experiment really, the scope of the project started growing beyond what I was comfortable keeping in stacks and ultimately I just rewrote the whole thing.

I also wrote a process supervisor for cloud containers in this same fashion, but it turned out to be only marginally lighter than just using s6 with alpine as base, so I dropped it.

nice!