Pointers as function returns in C/C++

Sdílet
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

Komentáře • 236

  • @mycodeschool
    @mycodeschool  Před 10 lety +64

    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. :)

    • @prankita107
      @prankita107 Před 5 lety +7

      You are awesome . Make more videos and bless us please.

    • @adityaojha2701
      @adityaojha2701 Před 2 lety +4

      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.

  • @mycodeschool
    @mycodeschool  Před 11 lety +49

    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.

  • @everything9118
    @everything9118 Před 5 lety +30

    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!

  • @user-yg2gw4je8d
    @user-yg2gw4je8d Před 7 lety +14

    This is an excellent series. In my view, your extended explanation/demonstration of pointers is especially good. Thank you so much for doing this.

  • @ozzyfromspace
    @ozzyfromspace Před 5 lety +15

    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!

  • @rajdeepkhandelwal1653
    @rajdeepkhandelwal1653 Před 5 lety +4

    all I can say "Thank You for this pointer tutorial".(Awesome explanation)

  • @vasanthidevipaneerselvam48

    Excellent explanation of pointers... No words to tell...now got clear idea of all concepts in pointers ...watch all videos on pointer

  • @dogdutyascetic
    @dogdutyascetic Před 11 lety

    Simply brilliant. The explanation of using the heap as a way to pass values down the calling stack is brilliant.

  • @PrayingForYourWellBeing

    This guy is seriously amazing, no words to describe his ingenuity !!!

  • @amandeepsingh2314
    @amandeepsingh2314 Před 8 lety +31

    you are awesome... great tutorial.. its really helped me a lot. you are doing a great job.

  • @samvaidya1987
    @samvaidya1987 Před 4 lety +1

    Every video has a ton of information. Very humbling. Thanks a ton!!

  • @anondoggo
    @anondoggo Před 5 lety +3

    I never understood cs this well before. Thank you!

  • @gijimbo1337
    @gijimbo1337 Před 4 lety +4

    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.

    • @soham2732
      @soham2732 Před 3 lety +1

      Hiiii I am from 2021 😂

  • @1990idris
    @1990idris Před 8 lety +1

    thank you very much. i read some books about c, but your explanation is the best. because you explain step by step.

  • @satwaghole
    @satwaghole Před 10 lety +3

    @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..

  • @maheshrokade5
    @maheshrokade5 Před 5 lety +3

    Bro.... thank you so...much.. your teaching is awesome😊 , no one actually touch such concepts but..you did...

  • @ankukumar7874
    @ankukumar7874 Před 3 lety +2

    I don't have words to describe how excellent you are.

  • @rohithbhandari7836
    @rohithbhandari7836 Před 5 lety

    You are just amazing. I have no words to say.Excellent teaching skills.

  • @ShubhamSharma-qy8ti
    @ShubhamSharma-qy8ti Před 7 lety +3

    Great tutorial sir. I look forward to learning a lot from you.

  • @hamzaahmad9489
    @hamzaahmad9489 Před 6 lety +2

    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.

  • @shivamverma9447
    @shivamverma9447 Před 3 lety +2

    This holy and sacred video cleared all the concepts...............

  • @sankarnampoothirikm3930

    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

  • @ZeriAi
    @ZeriAi Před 3 lety

    I've been watching at least one of your videos each day
    Thank you for your content

  • @cankocak1068
    @cankocak1068 Před 10 měsíci

    Great explanation! This video shows why we should return a pointer from the function using dynamic memory allocation.

  • @Protick1994
    @Protick1994 Před 6 lety +1

    Very nice brother, nice explanation. Thanks to you from bottom of my heart. Keep up the good work, best wishes

  • @zarifahmed1024
    @zarifahmed1024 Před 5 lety

    I just paused the video in the middle to say that you my brother you are awesommmeeeee!!!!! Take love

  • @bharathslip9
    @bharathslip9 Před 11 lety

    Excellent video demonstration about usage of pointers. Please release the memory which is dynamically allocated by Malloc in the heap.

  • @joecort9890
    @joecort9890 Před 4 lety +1

    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.

  • @jdschlichting
    @jdschlichting Před 5 lety +1

    This was very easy to follow along with. Thanks!

  • @garryharry1024
    @garryharry1024 Před 8 lety

    Amazing tutorial! Clear as always...

  • @SushrutKanetkar
    @SushrutKanetkar Před 10 lety +1

    Excellent explanation man ! Thank you very very much !

  • @alexanderschmidt8976
    @alexanderschmidt8976 Před 7 lety +1

    best possible explained, THANK YOU!

  • @dwivedys
    @dwivedys Před 6 lety

    Simply brilliant tutorial. I like the way you deliver your lecture. Would like to connect with you

  • @cajhdz
    @cajhdz Před rokem +1

    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

  • @rashigupta1813
    @rashigupta1813 Před 4 lety

    Man! Thanks a ton. These videos are so easy to understand

  • @devesh5955it
    @devesh5955it Před 11 lety

    Rocking Dude :), The concept regarding Top to bottom and bottom to top of the stack is cristal clear... Thank a lot Bro :)

  • @mistersz
    @mistersz Před 9 lety

    Excellent Tutorial, Great Teacher, Thank you very much !

  • @arafamahmood
    @arafamahmood Před 10 lety +1

    u explain very nicely!thanx for the lesson

  • @sairajdas6692
    @sairajdas6692 Před 4 lety

    Simply terrific explanation!!

  • @aloysiustan6809
    @aloysiustan6809 Před rokem +1

    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

  • @vidyadherreddy2511
    @vidyadherreddy2511 Před 9 lety

    Really nice videos..simple explanation to complex concepts

  • @conquerorcj26
    @conquerorcj26 Před 10 dny

    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

  • @shezijalal1774
    @shezijalal1774 Před 9 lety +1

    Awesome Explanation ! Thanks !

  • @thenottakenone
    @thenottakenone Před 10 lety +2

    Really really good explanation

  • @hardikraj9469
    @hardikraj9469 Před 4 lety +13

    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'

  • @rmarinero
    @rmarinero Před 5 lety

    Amazing! great refresher and tutorial

  • @sameerabanu329
    @sameerabanu329 Před 3 lety

    Thanks for your tutorial. It is helping me alot. Please start posting more videos.

  • @aleeibrahim8672
    @aleeibrahim8672 Před 7 lety +1

    Explained pretty well!! thanks :)

  • @clintonahong
    @clintonahong Před 10 lety +1

    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 .

    • @sunandachowdhury1455
      @sunandachowdhury1455 Před rokem

      Can you kindly explain why they used int *add? Don't we write the name of function at that place?

  • @pronoob4890
    @pronoob4890 Před 2 lety +1

    This cleared my concept. Thanks a lot.🤗🤗🤗🤗

  • @mycodeschool
    @mycodeschool  Před 11 lety +4

    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.

    • @joshidikshya6183
      @joshidikshya6183 Před 3 lety

      Could you please explain where should free() be placed? Writing free on main will create a segmentation fault.

  • @zekininadresi
    @zekininadresi Před 6 lety

    IMO, this video is just amazing.

  • @drnovice612ita
    @drnovice612ita Před 8 lety +3

    Illuminating explanation

  • @nalia20111
    @nalia20111 Před 9 lety

    Your videos helped me so much! Thank You !:D

  • @farhantanvir3073
    @farhantanvir3073 Před 10 lety +1

    very nice explanation. Thank you.

  • @thestarinthesky_
    @thestarinthesky_ Před 4 lety

    You are too amazing! you are extraordinary amazing! Google is lucky to have you. Wish you the best in the world! Thank you.

  • @MMABeijing
    @MMABeijing Před 2 lety

    that's amazing, I can not believe it. Thank you Coach

  • @muhammadnaveed7953
    @muhammadnaveed7953 Před 2 lety

    Excellent demonstration on pointers and memory..

  • @kunalpanchal7751
    @kunalpanchal7751 Před 5 lety

    Thank you for Blowing my mind.....

  • @timoteisatmarean7391
    @timoteisatmarean7391 Před 7 lety

    Subscribed because of this video. Thanks!

  • @rugri
    @rugri Před 3 lety +1

    Best explanation ever! Thanks

  • @viithalbhat5640
    @viithalbhat5640 Před 2 lety

    Amazing. Superb. Subtle. Love you.

  • @mayankverma5490
    @mayankverma5490 Před 4 lety

    Very valuable lesson......Thank you very much!!!!!!!

  • @user-gb7cp5vz4q
    @user-gb7cp5vz4q Před 4 lety

    Cool! Thanks shedding light on this topic. Bless you! :)

  • @overclockinggames2419
    @overclockinggames2419 Před 7 lety

    this was great sir , a learned a new thing

  • @aptitudepointiit-bhu5358

    Amazing Explanation !! 💛

  • @crazytech3519
    @crazytech3519 Před 5 lety

    really great work sir...

  • @rashmikiranpandit8962
    @rashmikiranpandit8962 Před 5 lety +1

    amazing as always!

  • @Anddosdd
    @Anddosdd Před 11 lety

    Exellent video,ive learnt alot my friend

  • @dandelion0331
    @dandelion0331 Před 9 lety

    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.

  • @mycodeschool
    @mycodeschool  Před 11 lety +3

    Thanks a lot :)

  • @abhimanyumishra9455
    @abhimanyumishra9455 Před 9 lety

    You are simply genius..

  • @lukthi
    @lukthi Před 8 lety

    So clear to understand, thank u @mycodeschool

  • @touching-fish-technologist

    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

  • @martingruscher7173
    @martingruscher7173 Před 10 lety

    Well explained, thanks a lot!

  • @guledalprashanth
    @guledalprashanth Před 7 lety

    Just awesome dude, thanks!

  • @manishthakur9797
    @manishthakur9797 Před 4 lety

    Great Explanation!

  • @ramjanraeen4263
    @ramjanraeen4263 Před 5 lety

    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.

  • @nearriver8174
    @nearriver8174 Před 10 lety +1

    nice! I really love this video

  • @Naveen-ef2dg
    @Naveen-ef2dg Před 6 lety

    Worth of Time. kudos..!!!

  • @EngWorld-nr2ww
    @EngWorld-nr2ww Před 3 lety

    Explained so well

  • @karelponcar5313
    @karelponcar5313 Před 5 lety

    Is that possible that my compiler can output "Sum = 6" in both occassions (6:39 and 7:11)?

  • @viewdeep100
    @viewdeep100 Před 6 lety

    excellent demo

  • @ramfattah211
    @ramfattah211 Před 9 měsíci

    Thank you Harsha Suryanarayana AKA humblefool. RIP Legend.

  • @pranaysamrit8944
    @pranaysamrit8944 Před 6 lety

    Your videos are sooo awesome

  • @saiyaswanthreddyleagala725

    Massive respect !!

  • @rahulaggarwal288
    @rahulaggarwal288 Před 3 lety

    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.

  • @someinteresting5629
    @someinteresting5629 Před 3 lety

    Very clear. Thank you

  • @rakshitks7850
    @rakshitks7850 Před 5 lety

    excellent playlist

  • @zacian4941
    @zacian4941 Před 4 lety

    you are a....LEGEND!

  • @Schnapsbrennor
    @Schnapsbrennor Před 8 lety

    Never thought about that...thanks

  • @sandeepkumawat4982
    @sandeepkumawat4982 Před 4 lety

    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 ^_^

  • @youyiliu7556
    @youyiliu7556 Před 6 lety

    Good, better, the best!

  • @sampathkumar2780
    @sampathkumar2780 Před 6 lety

    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

  • @ranjithks7
    @ranjithks7 Před 9 lety +2

    The PrintHelloWorld() function overwrittens the address of *c in Add() is aweful. Good to understand the behavior of Call Stack

  • @yunustaliperol6912
    @yunustaliperol6912 Před 4 lety

    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...

  • @LimeriuxasLT
    @LimeriuxasLT Před rokem

    excellent video, thanks!!!!!!

  • @mycodeschool
    @mycodeschool  Před 11 lety +1

    Thanks Dev !

  • @takijeden3419
    @takijeden3419 Před 7 lety

    Great course

  • @sepidkh84
    @sepidkh84 Před 10 lety +1

    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?

    • @Ferproko
      @Ferproko Před 2 lety

      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 😂