Click here to Skip to main content
15,905,144 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I have been observed separate data sections in code of various c projects.
I have never created data section in my code.

I am curious to know in what circumstances we should think about creating separate data section for the data?

What advantages can be achieved by creating separate data sections?

Thanks in advance for your reply
Posted
Comments
Sergey Alexandrovich Kryukov 21-Sep-11 10:31am    
Do you mean this: http://msdn.microsoft.com/en-us/library/50bewfwa%28v=vs.80%29.aspx?
--SA
Legor 21-Sep-11 10:43am    
What do you mean by "data section". Can you show an example of what you mean?
wizardzz 21-Sep-11 15:20pm    
Eh, sounds like HW.

More robust programs.
By separating the data and code sections, the OS can mark the code section as read-only, while still letting you modify data in the data section.

With this system in place, an attempt to write to the code section merely results in an exception, often causing your program to be chewed up and spat out.

If you have full write-access to the code section as well, any mistake you make (writing to it) can lead to all sorts of nasty behaviour.

It also helps prevent the execution of self-modifying code - a common feature of polymorphic computer virii (at the expense of greatly increasing the complexity of some super-optimized code that would also modify itself to cut down on execution time) Though to be clear, even a read-only code section can be turned into read/write at run-time.


It also allows for data to be located in physical memory at a place that's deemed appropriate, based on current operating conditions.
 
Share this answer
 
In addition to enhzflep answer, some systems feature (e.g. the PIC microcontrollers) Harvard architecture[^] where code and data are physically separated.
 
Share this answer
 
Comments
enhzflep 22-Sep-11 3:58am    
Good point. I'd forgotten about that.
Simon

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900