Pointers as function returns in C/C++
Vložit
- čas přidán 4. 07. 2024
- See complete series on pointers here:
• Pointers in C/C++
In this lesson, we have explained the concept of pointers as function returns. We have described through code example why we need to be careful about returning pointers from functions.
Feel free to drop your comments, feedback and suggestions.
For practice problems and more, visit: www.mycodeschool.com
Like us on Facebook: / mycodeschool
Follow us on twitter: / mycodeschool
Hi Mustafa,
Please watch the complete video. That's the whole point that has been explained in the video that you cannot return pointer to a local variable. I think because you knew the concept already and you have this opinion that one cannot learn programming on CZcams, you got angry and skipped at 9:00. :)
You are awesome . Make more videos and bless us please.
Today everyone learns on CZcams and not only programing but everything. MyCodeSchool took initiative of making this highly valuable content during a time when people were using 2G. Then also your content quality is way better.
Yes, printf is a library function. So, a stack-frame will be allocated for printf also. Sorry, i did not show it in my simulation. stack frame is allocated corresponding to each call of a function. So, if there are multiple calls to printf in the same function. stack frame will be allocated and de-allocated multiple times corresponding to each call of printf.
This man is lit. U teach so well. I have been in touch with c since 4 years, still didn't know these things. U r awesome!
This is an excellent series. In my view, your extended explanation/demonstration of pointers is especially good. Thank you so much for doing this.
Wow, I don't know how you did that, but you made me understand. That's super hard to do lol 😅 You're amazing man, thank you!
all I can say "Thank You for this pointer tutorial".(Awesome explanation)
Excellent explanation of pointers... No words to tell...now got clear idea of all concepts in pointers ...watch all videos on pointer
Simply brilliant. The explanation of using the heap as a way to pass values down the calling stack is brilliant.
This guy is seriously amazing, no words to describe his ingenuity !!!
you are awesome... great tutorial.. its really helped me a lot. you are doing a great job.
Every video has a ton of information. Very humbling. Thanks a ton!!
I never understood cs this well before. Thank you!
1st, I know this video was posted 6.5 years ago but good content is good content and is still valid today.
What I'd like to add is that "delete ptr" should be used when we're done using it. After that, "ptr = nullptr" should also be used so we don't end up with a dangling pointer.
Hiiii I am from 2021 😂
thank you very much. i read some books about c, but your explanation is the best. because you explain step by step.
@kewlpint: Yes.printf() is considered as another function call and separate stack frame is allocated for it. Nice question.
@mycodeschool: You r doing gr8 job boss.Go ahead..
Bro.... thank you so...much.. your teaching is awesome😊 , no one actually touch such concepts but..you did...
I don't have words to describe how excellent you are.
You are just amazing. I have no words to say.Excellent teaching skills.
Great tutorial sir. I look forward to learning a lot from you.
Since your playlist tells almost everything a beginner needs to know about pointers . Kindly rename it with "All you need to know about pointers" . I couldn't find your video easily although it was much better than all videos I saw on pointers.
This holy and sacred video cleared all the concepts...............
wow cooooooooool i was serching this for days u r the only one who cleared my doubt (my doubt was about array returning)
you r god
I've been watching at least one of your videos each day
Thank you for your content
Great explanation! This video shows why we should return a pointer from the function using dynamic memory allocation.
Very nice brother, nice explanation. Thanks to you from bottom of my heart. Keep up the good work, best wishes
I just paused the video in the middle to say that you my brother you are awesommmeeeee!!!!! Take love
Excellent video demonstration about usage of pointers. Please release the memory which is dynamically allocated by Malloc in the heap.
What a master I was struggling with a linked list, and I thought it was something due to some of this, and I was right. Thank u.
This was very easy to follow along with. Thanks!
Amazing tutorial! Clear as always...
Excellent explanation man ! Thank you very very much !
best possible explained, THANK YOU!
Simply brilliant tutorial. I like the way you deliver your lecture. Would like to connect with you
Thank you very much, my mind was crashed because I didn't know the root cause of this behaviour
You have a great! channel to learn from
Man! Thanks a ton. These videos are so easy to understand
Rocking Dude :), The concept regarding Top to bottom and bottom to top of the stack is cristal clear... Thank a lot Bro :)
Excellent Tutorial, Great Teacher, Thank you very much !
u explain very nicely!thanx for the lesson
Simply terrific explanation!!
This video basically explains why there is error when using pointers on the stack as data can be deallocated , however in when using pointers in the heap data has to be explicitly deallocated or overwritten using free() which is handled by the programmer, so there will be no accidental deallocation of data unlike in the stack
Really nice videos..simple explanation to complex concepts
I watched from the first video in this playlist upto here in one watch..This playlist made me relise and gave a realaly good uunderstanding also cleared lotta doubts on a project i did using dynamic memory allocation.Thanks a lot
Awesome Explanation ! Thanks !
Really really good explanation
From where did you study these tehings? It would be really helpful if you could give the materials or the name of the books from where you studied the 'application memory'
Amazing! great refresher and tutorial
Thanks for your tutorial. It is helping me alot. Please start posting more videos.
Explained pretty well!! thanks :)
Actually it seems very fast to learn the concepts.Although i listen slowly with pause and play again and again thinking all about the int* add(int *a,int *b).Now i finally understand the underlying concepts .Thanks to mycodeschool for such valuable lectures .
Can you kindly explain why they used int *add? Don't we write the name of function at that place?
This cleared my concept. Thanks a lot.🤗🤗🤗🤗
Thanks Bharath !! yeah, may be i should always write a free. If you see program will anyway finish execution after print where we are using the pointer to access memory. But anyway, its a good practice to write free explicitly.
Could you please explain where should free() be placed? Writing free on main will create a segmentation fault.
IMO, this video is just amazing.
Illuminating explanation
Your videos helped me so much! Thank You !:D
very nice explanation. Thank you.
You are too amazing! you are extraordinary amazing! Google is lucky to have you. Wish you the best in the world! Thank you.
that's amazing, I can not believe it. Thank you Coach
Excellent demonstration on pointers and memory..
Thank you for Blowing my mind.....
Subscribed because of this video. Thanks!
Best explanation ever! Thanks
Amazing. Superb. Subtle. Love you.
Very valuable lesson......Thank you very much!!!!!!!
Cool! Thanks shedding light on this topic. Bless you! :)
this was great sir , a learned a new thing
Amazing Explanation !! 💛
really great work sir...
amazing as always!
Exellent video,ive learnt alot my friend
so you allocated memory inside a function, is it ok to free the memory allocated outside a function? is it safe to do so? thanks. good explanation.
Thanks a lot :)
You are simply genius..
So clear to understand, thank u @mycodeschool
Lucas Dantas
.
bulu
Should callee(add) stacks be at the bottom of caller(main), if you assume stack grows top down(from high address to low address)? It's kind of strange you draw a diagram such a way
Well explained, thanks a lot!
Just awesome dude, thanks!
Great Explanation!
Hey, Your tutorial is awesome.
can you tell me which compiler you use?
your compiler found an error during typing the code that a good thing and attractive.
Use Visual Studio 2019
nice! I really love this video
Worth of Time. kudos..!!!
Explained so well
Is that possible that my compiler can output "Sum = 6" in both occassions (6:39 and 7:11)?
excellent demo
Thank you Harsha Suryanarayana AKA humblefool. RIP Legend.
Your videos are sooo awesome
Massive respect !!
If I use consecutive command to add another two numbers and store in another pointer variable , say ptr1, the answer in the first ptr will be overwritten with the answer stored in ptr1. Can you help so i can use the function multiple times to add different numbers and getting the accurate answer for each?
Thanks.
Very clear. Thank you
excellent playlist
you are a....LEGEND!
Never thought about that...thanks
huhhh,,,,i got tired of writing positive feedback for this man in every video...but still i feel these appreciations are not much for his skills ^_^
Good, better, the best!
hi sir i have one doubt in called function c is the local variable the scope of the local variable is within the block but how can u return the address of c. i am tired in linux platform so iam getting runtime error(segmention fault) can u help me
The PrintHelloWorld() function overwrittens the address of *c in Add() is aweful. Good to understand the behavior of Call Stack
Firstly, this is the best video ever on this topic for sure...But I have a question : Why the function ADD returning the "c" instead of "&c"??? It is returning "c" but we are writing the address of "c" in to the *ptr in the stack??? This confused me a bit...
excellent video, thanks!!!!!!
Thanks Dev !
Great course
Thanks for your explanation, I have a question and I would appreciate if anyone can help, by defining pointer c in add function can we deallocate the memory in heap? if we call the add function several times, is that allocate several addresses for pointer c in Heap?
Each time you call add function, as is declared, it will allocate a new block of memory. It is your responsibility as a programmer to free that new block when you finish using it. You can free it because you have the address. Sorry that I'm 8 years late 😂