Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> and then returning the duplicate of that. Shouldn’t that be Malloc’ed instead??

Like the sibling already wrote, that's what strdup does.

> Is it safe to return the duplicate of a stack allocated

Yeah sure, it's a copy.

> wouldn’t the copy be heap allocated anyway?

Yes. I wouldn't commit it like that, it is a naive implementation. But honestly I wouldn't commit leftpad at all, it doesn't sound like a sensible abstraction boundary to me.

> Not to mention it blows the stack and you get segmentation fault?

Yes and I already mentioned that in my comment.

---

> dynamic array right on the stack

Nitpick: It's a variable length array and it is auto allocated. Dynamic allocation refers to the heap or something similar, not already done by the compiler.



Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: