The title is a bit wrong, but that's the best I can do with the English I know at the moment.
What I mean is:
I am currently creating my "toy" OS.
I am able to create a string like this in C:
int main() {
char* s = "blah";
}
That means I am creating a pointer to a memory chunk, right?
I would think that, without an operating system, I would have to do the following:
int main() {
char* s = 0x/*A random address here*/;
s[0] = 'b';
s[1] = 'l';
s[2] = 'a';
s[3] = 'h';
s[4] = 0;
I haven't really tried the second, but I feel like both of these methods are undefined behavior, because I haven't registered the length of the string somewhere.
Can someone help me with this?
- Where is the first
"blah"
stored in RAM since I haven't specified an address? - Is the second the correct way, if not, what is the correct way?
- Is any of the methods I mentioned undefined behavior?
Thanks in advance.
CodePudding user response:
Generally a compiler produces an output file that contains the code and data it generated and information about how they should be laid out in memory. A linker reads one or more of these files and produces a similar output file. When the program is executed, a program loader reads the executable file produced by the linker and puts the code and data in memory at appropriate places.
Some linkers also have options to produce a “raw” binary file that could be loaded into memory directly, if it has been appropriately prepared.
To make an operating system, you must have some way of loading your operating system. That could be a raw binary file that the hardware loads into memory, or it could be some file format that the firmware knows how to load. Modern general-purpose computers contain hardware and firmware that perform extensive operations to support loading the operating system. To create an operating system for such a computer, you will need to learn about the file format that its firmware uses.