Understanding the Dangling Pointers

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 ส.ค. 2024
  • Data Structures: Understanding the Dangling Pointers
    Topics discussed:
    1) What is a Dangling pointer?
    2) How to avoid the Dangling pointers?.
    3) Examples of Dangling pointer.
    4) Segmentation fault.
    C Programming Lectures: goo.gl/7Eh2SS
    Follow Neso Academy on Instagram: @nesoacademy(bit.ly/2XP63OE)
    Follow me on Instagram: @jaspreetedu(bit.ly/2YX26E5)
    Contribute: www.nesoacademy...
    Memberships: bit.ly/2U7YSPI
    Books: www.nesoacademy...
    Website ► www.nesoacademy...
    Forum ► forum.nesoacade...
    Facebook ► goo.gl/Nt0PmB
    Twitter ► / nesoacademy
    Music:
    Axol x Alex Skrindo - You [NCS Release]
    #DataStructuresByNeso #DataStructures #DanglingPointer #Pointer

ความคิดเห็น • 69

  • @matousvondrak3461
    @matousvondrak3461 ปีที่แล้ว +14

    This channel is a holy grail of C tutorials, I am so grateful I found you. Your explanations are clear and also show you have deep understanding of what you talk about.

  • @Tarun_Kotagiri
    @Tarun_Kotagiri 4 ปีที่แล้ว +24

    Damn..I don't get why your channel isn't famous yet...I refer your Vedios for almost everything including ADE.....but don't give up...! Keep at it and you'll be shining under the blue sky some day...

  • @edwardmacnab354
    @edwardmacnab354 2 ปีที่แล้ว +7

    Your explanations are pretty good ! They are clear , concise , and thorough . You have all the key information that a lot of other CS channels are leaving out ! Thank you .

  • @bossysmaxx3327
    @bossysmaxx3327 ปีที่แล้ว +3

    dude I used to wat you 5 years ago and really apprecite you man, you're just amazing,
    you taught me computer architecture so well that I got deep level understanding of how computers actually work and also successfully build my own 4 bit computer from a breadboard and also my own seven segment display

  • @kanishkgupta5015
    @kanishkgupta5015 2 ปีที่แล้ว +7

    Thanks bro Dangling pointer is asked in so many interviews. Finally i can answer it with full clarity.

  • @anshuraj9610
    @anshuraj9610 3 ปีที่แล้ว +6

    Best video ever made 🔥🔥,black screen also reduces the eye strain .love u sir

  • @tanviralam9798
    @tanviralam9798 4 ปีที่แล้ว +33

    Thank you, I understand English less which is why I am having a little trouble coding from these videos but I keep trying. I'm from Bangladesh 🇧🇩

  • @aviroxi
    @aviroxi 3 ปีที่แล้ว +10

    Thank you so much, for so many days i was searching internet for segmentation fault but i could not understood it clearly you made it so clear thank you 🤩

  • @kingdey9136
    @kingdey9136 4 ปีที่แล้ว +5

    thank you for all you are doing sir
    sir we want such tutorials on python too...

  • @pk_officl
    @pk_officl 3 ปีที่แล้ว +1

    Thank you so much. Really Neso Academy is best one for learn. You always trying to teach with full of clarity not just for cover the topics.

  • @tahaanass3097
    @tahaanass3097 21 วันที่ผ่านมา

    what i understood is that the variable inside the local function gets dealocated from the stack automatically by the compiler , so returning the pointer of a dealocated memory location makes it a dangling pointer , i hope i userstood it right ? and thank you so much

  • @yutaitadori7318
    @yutaitadori7318 3 ปีที่แล้ว +3

    Beauty in the way you teach 💖

  • @nishyu9101
    @nishyu9101 ปีที่แล้ว +1

    great explanation!

  • @emkcoryo8742
    @emkcoryo8742 4 ปีที่แล้ว +6

    Faster than the notification! Haha!

  • @kunalsoni7681
    @kunalsoni7681 4 ปีที่แล้ว +1

    Thanks for that.. I was never know about dangling pointer 😇. after watching this This is very helpful to me

    • @Dnsx_plus
      @Dnsx_plus 3 ปีที่แล้ว +1

      Yah same

  • @amiral3187
    @amiral3187 ปีที่แล้ว +3

    Thank you for the explaination, but as I try to recompile the same examples(example2) the compiler only gives me a warning for returning a local address but the program runs normally(no segmentaion fault Error),
    does this means that new compilers(GCC) are optimized to avoid dangling pointers or I'm missing something ?
    My code:
    #include
    #include
    int* CreatInt(int var)
    {
    int I = var;
    return &I;
    }
    int main(void)
    {
    int *ptr = NULL;
    ptr = CreatInt(10);
    printf("ptr contains: %d
    ", *ptr);
    return 0;
    }
    The output:
    program.c: In function 'CreatInt':
    program.c:79:2: warning: function returns address of local variable [-Wreturn-local-addr]
    return &I;
    ^
    GCC_EXIT_CODE: 0
    ptr contains: 10

  • @gulabchandgupta5486
    @gulabchandgupta5486 11 หลายเดือนก่อน

    Wow great explanation thanks man 👏👏🙏

  • @mdkalimullahzakir
    @mdkalimullahzakir 4 หลายเดือนก่อน

    I didn't get any warning about fun() returning a address of local variable in Turbo C++ compiler and my program runs successfully. But when I tried to run it in VS code program didn't run at all and throws a warning " warning: function returns address of local variable".

  • @sallaklamhayyen9876
    @sallaklamhayyen9876 2 ปีที่แล้ว

    great explanation thank you so much

  • @HelloWorld40408
    @HelloWorld40408 ปีที่แล้ว

    Thank You Sir

  • @sureshsingh9880
    @sureshsingh9880 2 ปีที่แล้ว

    it helps me so much

  • @oggy107
    @oggy107 3 ปีที่แล้ว +2

    Use static int to avoid the problem.

    • @prog2597
      @prog2597 2 ปีที่แล้ว

      Yes because static variables remains till the program execution

  • @vivelafootball4835
    @vivelafootball4835 ปีที่แล้ว

    Best explaination

  • @akshadabhapkar8944
    @akshadabhapkar8944 3 ปีที่แล้ว

    Awesome explanation yarrrr

  • @nithyashreenaidu8357
    @nithyashreenaidu8357 4 ปีที่แล้ว

    This was very new..thanks sir

  • @sameeruj
    @sameeruj 3 ปีที่แล้ว

    Thank you!

  • @sumainakyasar8857
    @sumainakyasar8857 2 ปีที่แล้ว

    why do we use free() if it causes dangling?

  • @shantanukulkarni8883
    @shantanukulkarni8883 ปีที่แล้ว +1

    When I tried to execute the second example's program for dangling pointer in my pc, it ran without any errors. I also got the correct output as 10. I did get a warning about the fun() function returning a address of local variable but that's it. How is this possible?

    • @mdkalimullahzakir
      @mdkalimullahzakir 4 หลายเดือนก่อน +1

      I have same problem in But I didn't get any warning about fun() returning a address of local variable in Turbo C++ compiler. But when I tried to run it in VS code program didn't run at all and throws a warning " warning: function returns address of local variable".

  • @ihabmed1672
    @ihabmed1672 2 ปีที่แล้ว

    Thank u

  • @techxplorerxxx
    @techxplorerxxx 3 ปีที่แล้ว

    Why int * fun() is used isn't it a function pointer?

  • @KUNALSINGH-zk8su
    @KUNALSINGH-zk8su 3 ปีที่แล้ว

    Thanks sir

  • @dileepcheekala2617
    @dileepcheekala2617 ปีที่แล้ว +1

    In example2 output is 10

  • @usmanhaider4653
    @usmanhaider4653 ปีที่แล้ว

    How to use a function pointer in struct?

  • @tarunpatel1457
    @tarunpatel1457 3 ปีที่แล้ว

    Really helpful 👏

  • @pawansahu5265
    @pawansahu5265 4 ปีที่แล้ว

    Thanks

  • @user-pr4jg3sn2j
    @user-pr4jg3sn2j 3 ปีที่แล้ว +1

    Local variable 의 주소를 반환할 때 댕글링 포인터로 인한 세그멘테이션 폴트 발생

  • @abhishekm1903
    @abhishekm1903 3 ปีที่แล้ว +1

    Excellent work...!!

  • @Sakthibalankk
    @Sakthibalankk 10 หลายเดือนก่อน

    love you bro

  • @ritikashishodia675
    @ritikashishodia675 3 ปีที่แล้ว

    Kya dangling pointer us memory ko point to krra h vo us memory ko use kr skta h na aur value assign krke print kraya to horra h codeblocks pr .... How say 1000 memory is not exist *p still point so we can use it???

  • @md.nahidulalamchowdhury9568
    @md.nahidulalamchowdhury9568 2 ปีที่แล้ว

    Awesome!!!

  • @47-6F-64
    @47-6F-64 4 ปีที่แล้ว

    You're amazing !

  • @prashanthreddy5959
    @prashanthreddy5959 4 ปีที่แล้ว +2

    i am not getting segmentation fault but getting the num value printed as 10 can u help me out

    • @pradhoshss8281
      @pradhoshss8281 4 ปีที่แล้ว +1

      Same 4 me..

    • @ShubhamMishra-mz4zw
      @ShubhamMishra-mz4zw 3 ปีที่แล้ว

      The thing is that you may get segmentation fault when you deal with the return address of local variable.I am sure you might be getting warning messages.It is not advisable to use this method.It might work for some cases while it may not work for other cases...its better to avoid it.

  • @jacklaughter1219
    @jacklaughter1219 4 ปีที่แล้ว +2

    at 4.25, what if we define "num" as static int? then in my compiler, it works without warning.

    • @ayushshreshthi3891
      @ayushshreshthi3891 3 ปีที่แล้ว +3

      scope of the static variable is throughout the program.

  • @zeeshansadiq9069
    @zeeshansadiq9069 3 ปีที่แล้ว +1

    visual studio 2019 could not high light this fault its execute successfully

  • @shourjyajitmaiti2490
    @shourjyajitmaiti2490 4 ปีที่แล้ว

    when will control system videos come?

  • @letsroastbro7545
    @letsroastbro7545 2 ปีที่แล้ว

    2:43 sir what if we skip free(ptr) ????

    • @andistheinforitbutso7513
      @andistheinforitbutso7513 2 ปีที่แล้ว +1

      I think the memory allocated to 'ptr' is still not yet freed.
      Even though 'ptr' will not point to any memory location(i.e NULL pointer) but the chunk of memory allocated using malloc function will be inaccessible due to not being freed.
      There will be no error while compiling or running the program but it is a bug.
      So, free(ptr) is necessary.

  • @seaofknowledge2477
    @seaofknowledge2477 2 ปีที่แล้ว

    Is this for java?

  • @LearnwithAnas
    @LearnwithAnas 4 ปีที่แล้ว

    Goood bro

  • @srikarrepaka5023
    @srikarrepaka5023 3 ปีที่แล้ว

    2:13
    2:48
    4:50

  • @arghya.7098
    @arghya.7098 3 ปีที่แล้ว

    2:11

  • @48_prasenjitmondal6
    @48_prasenjitmondal6 4 ปีที่แล้ว

    🥰🥰😍😍😍🤩😘

  • @bossysmaxx3327
    @bossysmaxx3327 ปีที่แล้ว

    dude when I tried it it gave me waning at compile time but gave the right answer and right memory address...
    ```
    #include
    #include
    int *haha () {
    int num = 554;
    printf("in hah->%d:%u
    ", num, &num);
    return #
    }
    void main () {
    printf("%d: %u
    ", *haha() , haha());
    }
    ```

  • @andistheinforitbutso7513
    @andistheinforitbutso7513 2 ปีที่แล้ว

    don't read from geeksforgeeks just come here.. or use trusted books