Understanding the Dangling Pointers

Поділитися
Вставка
  • Опубліковано 14 жов 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 Рік тому +16

    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.

  • @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 роки тому +8

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

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

  • @anshuraj9610
    @anshuraj9610 3 роки тому +6

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

  • @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 🤩

  • @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 🇧🇩

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

  • @kingdey9136
    @kingdey9136 4 роки тому +5

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

  • @tahaanass3097
    @tahaanass3097 2 місяці тому

    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 💖

  • @amiral3187
    @amiral3187 2 роки тому +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

  • @nishyu9101
    @nishyu9101 2 роки тому +1

    great explanation!

  • @gulabchandgupta5486
    @gulabchandgupta5486 Рік тому

    Wow great explanation thanks man 👏👏🙏

  • @kunalsoni7681
    @kunalsoni7681 4 роки тому +1

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

  • @sallaklamhayyen9876
    @sallaklamhayyen9876 2 роки тому

    great explanation thank you so much

  • @HelloWorld40408
    @HelloWorld40408 Рік тому

    Thank You Sir

  • @sureshsingh9880
    @sureshsingh9880 2 роки тому

    it helps me so much

  • @emkcoryo8742
    @emkcoryo8742 4 роки тому +6

    Faster than the notification! Haha!

  • @abhishekm1903
    @abhishekm1903 4 роки тому +1

    Excellent work...!!

  • @akshadabhapkar8944
    @akshadabhapkar8944 3 роки тому

    Awesome explanation yarrrr

  • @oggy107
    @oggy107 3 роки тому +2

    Use static int to avoid the problem.

    • @prog2597
      @prog2597 2 роки тому

      Yes because static variables remains till the program execution

  • @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 6 місяців тому +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".

  • @mdkalimullahzakir
    @mdkalimullahzakir 6 місяців тому

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

  • @vivelafootball4835
    @vivelafootball4835 Рік тому

    Best explaination

  • @nithyashreenaidu8357
    @nithyashreenaidu8357 4 роки тому

    This was very new..thanks sir

  • @sameeruj
    @sameeruj 3 роки тому

    Thank you!

  • @ihabmed1672
    @ihabmed1672 2 роки тому

    Thank u

  • @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???

  • @KUNALSINGH-zk8su
    @KUNALSINGH-zk8su 4 роки тому

    Thanks sir

  • @원형석-k3f
    @원형석-k3f 3 роки тому +1

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

  • @47-6F-64
    @47-6F-64 4 роки тому

    You're amazing !

  • @pawansahu5265
    @pawansahu5265 4 роки тому

    Thanks

  • @tarunpatel1457
    @tarunpatel1457 4 роки тому

    Really helpful 👏

  • @sumainakyasar8857
    @sumainakyasar8857 2 роки тому

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

  • @md.nahidulalamchowdhury9568
    @md.nahidulalamchowdhury9568 2 роки тому

    Awesome!!!

  • @usmanhaider4653
    @usmanhaider4653 Рік тому

    How to use a function pointer in struct?

  • @dileepcheekala2617
    @dileepcheekala2617 Рік тому +1

    In example2 output is 10

  • @shourjyajitmaiti2490
    @shourjyajitmaiti2490 4 роки тому

    when will control system videos come?

  • @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 4 роки тому

      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.

  • @zeeshansadiq9069
    @zeeshansadiq9069 3 роки тому +1

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

  • @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 4 роки тому +3

      scope of the static variable is throughout the program.

  • @letsroastbro7545
    @letsroastbro7545 3 роки тому

    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 3 роки тому

    Is this for java?

  • @srikarrepaka5023
    @srikarrepaka5023 3 роки тому

    2:13
    2:48
    4:50

  • @LearnwithAnas
    @LearnwithAnas 4 роки тому

    Goood bro

  • @arghya.7098
    @arghya.7098 3 роки тому

    2:11

  • @48_prasenjitmondal6
    @48_prasenjitmondal6 4 роки тому

    🥰🥰😍😍😍🤩😘

  • @Sakthibalankk
    @Sakthibalankk Рік тому

    love you bro

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

  • @techxplorerxxx
    @techxplorerxxx 3 роки тому

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