Why can't I use alloca()?

Asked 2 years ago, Updated 2 years ago, 151 views

As far as I know, alloca() dynamically allocates memory in stack, not heap, and releases memory on its own when it returns from routine

Then shouldn't it be recommended because it's safer because it doesn't have to be free like malloc()? But why can't I use it?

c stack malloc allocation alloca

2022-09-22 22:22

1 Answers

If you look at the linux man page

RETURN VALUE The alloca() function returns a pointer to the beginning of the allocated space. If the allocation causes stack overflow, program behaviour is undefined.

That's what it says. It says it's a problem if there's a stack overflow, and it doesn't say not to use it.

It doesn't say never to use it It's a function that doesn't matter if you don't assign a big value It was a function that I used often in my project.


2022-09-22 22:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.