• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Help me find why these

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Can someone tell me why switching the order creates the error? I believe it is due to dangling pointer, but just want to be sure.
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your foo1() function returns a pointer to a local variable. Unfortunately, that local variable is not in scope upon return, so it is pointing to a location on the stack that will be reused with other function calls.

Henry
 
Jay Schwartzman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but then why is it switching val and ptr to be the same in the if/else will not create that error
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Schwartzman wrote:Yes, but then why is it switching val and ptr to be the same in the if/else will not create that error



First of all, this should be compiler dependent -- so what you are seeing may not be with another compiler.

Anyway, if the two blocks are returning different results, I would speculate that it is the "else" block that is wrong. The reason is that the foo2() function reused the stack that was previously used by the foo1() function. Technically, the "if" block also has issues, but it has not yet been corrupted when the pointer is used in the later expression (since the foo2() function was used before the foo1() function).

Henry
 
Jay Schwartzman
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That may be 1/2 right, when a do a line by line tracking, the else runs first since 0 is even and gives val0=108 then i is 1, and val 1 gives 10. Then these values never change. However if you switch in the else statement for val to be done first then ptr, both val 0, and val 1 stay 10 throughout the program. I don't understand why
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Well, like I mentioned, this is a corruption (that is likely compiler dependent); so, if you want to understand how your compiler generated / optimized the code differently, so that the result is corrupted differently (or not corrupted at all), I think that you may be alone in your research.

Sorry,
Henry
 
You totally ruined the moon. You're gonna hafta pay for that you know. This tiny ad agrees:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic