Credit…Joao Silva/The New York Times

JOHANNESBURG — The floods in Durban are one of South Africa’s deadliest natural disasters, but part of the reason so many people died is man-made: the country’s failure to deal with a longstanding housing crisis.

Millions of South Africans — in a nation where the unemployment rate is more than 35 percent — cannot afford stable, permanent homes. So many end up building tin shacks wherever they can find land, often in the least-desirable locations, creating what are known here as informal settlements.

In the case of Durban and the surrounding area, those locations are often in low-lying valleys next to rivers or on steep, slippery slopes — among the most dangerous places to be when severe rain storms strike, as was the case a week ago.

After a week of punishing rain, mudslides and flooding flattened hundreds of those shacks in Durban. South Africa’s President, Cyril Ramaphosa, said informal settlements were particularly affected by the extreme weather. Nearly 4,000 homes were destroyed, many of them in informal settlements.

Informal settlements are in many ways a legacy of apartheid. During that time, South Africa’s Black majority was relegated to live in certain far-off places. Once the racist system ended, Black residents could at last move freely around their country’s cities.

Yet many struggled to find places to settle in cities that were built to deliberately keep them out. So in the years after apartheid, as millions of people across South Africa left impoverished rural areas to live and work in cities, they were unable to find suitable housing. Instead, they settled in tin shacks, which mushroomed in many of the country’s cities.

To try to account for the lack of affordable housing, South Africa’s government has built more than three million free houses since the end of apartheid, according to a government report. But even that has not kept up with demand. Over the years, even more shacks have sprung up in more cities, creating a housing crisis with a backlog of more than two million households who seek shelter.

“There was a change in terms of law that people can live wherever they want to live, but the difficulty is that there wasn’t an economic policy to match that,” said Edward Molopi, a researcher at the Socio-Economic Rights Institute of South Africa.

And the free matchbox houses built as part of South Africa’s national housing scheme are also beset by the same challenges of apartheid-era spatial planning and limited budgets. These homes are built far from city centers, where land is cheaper but jobs are scarce. Hospitals and schools are built years later.

Many who have been able to obtain free housing choose instead to return to shack homes because they are closer to cities and jobs, trading improved living conditions for economic opportunity, Mr. Molopi said.

“The idea was basically the very same apartheid pattern of thinking that poor Black South Africans do not deserve to be nearby cities,” said Sibusiso Zikode, one of the leaders of Abahlali baseMjondolo, a shack dwellers movement.

The shack homes in Durban and other cities were built on an open piece of land, often near a river bank or on a sloping hillside, and cobbled together from corrugated iron, wood and plastic. Newspaper stuffed into gaps kept out a cold wind or rain.

These informal settlements rarely have running water, and the relatively more fortunate quarters have portable chemical toilets shared by dozens of residents. Many more are reliant on pit latrines. Electricity is siphoned from illegal connections, and deadly fires are common.

Across the country, 11.8 percent of South African households live in these informal dwellings, according to Statistics South Africa, a government agency.

President Ramaphosa, in addressing the nation on Monday night, acknowledged that the government needed to be more deliberate about where it placed housing.

Rebuilding from the floods, he said, “will also involve the construction of houses in suitably-located areas and measures to protect the residents of these areas from such adverse weather events in the future.”