array created on stack can not be returned from function in C programming

preview_player
Показать описание
Returning an array from a function is kind of a daily job of a software engineer, but if you are beginner then you might do it wrong.
You can't just return the array from the function because it was created on stack and the moment function is returned the memory will be claimed and receiver end will try to access freed memory which is not good.

So the solution to this problem is that you can use the static array and then return that array that will not have any memory issue because static variable or array or object just get created once and will remain there till the program exist.

JOIN ME

#cprogramming #interview #softwareengineer
Рекомендации по теме
Комментарии
Автор

It’s very interesting, the way you explain things. Thanks!👍

LittleThings..
Автор

You can also use a dynamic array and returns its pointer

codermomo
Автор

And when does this memory erase? Or we leak memory everytime we access foo function?

erkintek
Автор

@CppNuts won't RVO or NVRO be applies here?
If No, then how come it works for Class variable as shown in you old video on RVO and NVRO

hubtubsub
Автор

Hello sir
Nice short video on static keyword .
2day I had one interview question about far near huge pointers . But as embedded engineer I haven't heard about those pointers in my two years of experience.
Is there any chance to explain it in your way
Thank you so much in advance .

ramakrishna
Автор

this is a stylistic choice, however, do not put pointers inbetween types and identifiers, it makes the code harder to read.

notaredBox