This is a very valid point. There are a lot of mentally unstable homeless on the streets and you do have to be careful with certain interactions. When I walk the streets, I make eye contact first and smile. Generally, their reaction helps me gauge what kind of interaction I might have with this person.
If they yell at me, or start talking to themselves, I leave it at a smile and keep walking. Even acknowledging they are a human is something they really need.
If they engage with me, or respond to me, I'll sometimes stop and talk with them for a minute.
More often than not, when I go out to eat, I can't finish my food. Depending on where I am, I generally look for someone as I walk home to give it to. It's always risky because I would never want to offend someone by offering them food, but if I see someone digging through a trashcan, I always ask "Hey Man, are you hungry?" and offer my leftovers. I've never been turned down and I've gotten a few hugs out of it.