In the case of a random blog, the code likely does not have an associated license, meaning it is copyrighted and any derivative use of it (i.e. copying it) is a violation of their copyright.
In the case of stackoverflow, their ToS require people who post code to share it under a Creative Commons license [0]. Unfortunately, if the author of the post didn't realize that, it's possible they did not have the original copyright and couldn't make that assignment, so you still may have to do your own due diligence.
Where things work as intended on stackoverflow, you are required to provide attribution.
Your assumptions are backwards, and it sounds like that results in you violating copyright.