It depends on SO's terms of use. Without any terms to the contrary, which I am confident exist, you would be violating copyright by copying that example, regardless of attribution.
Assuming the example is the original work of the author of the post (and again, assuming no other terms), then that post author would hold copyright to the work. You would have no license to copy or distribute it.
I have great confidence that SO's terms include a section wherein contributors grant license or grant copyright to SO. At that point (again, assuming no other terms), you would still have no right or license to copy or distribute the work.
Assuming SO has a very permissive license to the work, including the ability to re-license it, they can grant you a license to copy or distribute the work. At that point you are bound by the terms of the license SO grants you.
Assuming the example is the original work of the author of the post (and again, assuming no other terms), then that post author would hold copyright to the work. You would have no license to copy or distribute it.
I have great confidence that SO's terms include a section wherein contributors grant license or grant copyright to SO. At that point (again, assuming no other terms), you would still have no right or license to copy or distribute the work.
Assuming SO has a very permissive license to the work, including the ability to re-license it, they can grant you a license to copy or distribute the work. At that point you are bound by the terms of the license SO grants you.
Read the terms to get specifics.