C++ memory partition questions.
Apart from C# I am also reading about C++ which I found easier is some parts. Anyway I must admit that I have some serious questions about the memory partitioning and other stuff. If someone could give an explanation I would be grateful.
1) In my University we did a lot of Assembly. In there I knew that the memory is in general 4 parts, code segment, data segment and stack segment. I usually used the code seg or cs and the data seg or ds. The first had my code and the second had all my data. Meanwill I knew that all of my registers would be stored in the various reg of the cpu. In case I wanted to use a function then I used the stack seg or ss in order to store the previous address and reg so that I could use them after the jump to the function ends. However the ss could be used as a memory also.
Now in C++ I read that there are 5 memory parts, the stack, the free store or heap, the code space, the registers and the global name space. The book I am reading says that in a C++ program every variable that my functions use are stored in the stack even the member variables of objects. Ok I understand that. I also understand that the free store is the free memory which I can use only if I use the NEW keyword. When I have a statement like "Car *pford = new Car" I know that a pointer to an object of type Car with name pford will be created and take the address on a free store. In other words an object would be created in the free store. So my questions are:
a) If I had the statement "Car *pford" in what memory would the pointer point?
b) Can I use the statement "Car pford = new Car" and what will it mean?
c) Why use the statement "Car *pford = new Car"? Also is pford a pointer to an object or a pointer to a class?
d) If I had the statement "Car *function(Car *ford)" what does it mean?
e)Why use the statement "Car *pford" at all and not "Car pford"?
Could I use references instead of pointers for the above?
You think I should stick with C# only?
IF anyone wants to answer please do.
Anyone correct me if I''m wrong.
A)It could point any where since it hasn''t been inited (I think some compilers my init it to NULL, but I don''t think the standard requires it). A big source of bugs. Generally you type
Car *pford = NULL;
to help avoid problems. Most if not all standard function ignore NULL pointers.
B) Nope the new operator returns a pointer to a car and pford is a car, not a pointer.
C) Why is kind of hard to explain. generally if you don''t know how many objects you''ll need at run time you''ll need to make more. I recommend checking if an STL container would be more useful for your dynamic storage. Other important uses are polymorphism and optimization.
D) function is a function that outputs a pointer to a car, and it''s input is a pointer to a car. And in the input has the name ford.
E) *pford is pointer (the address of a car object, generally a 32bit number). pford is a car (requires all the storage of a car object). One advantage is moving single 32 bit number around between functions is a lot faster than your 3 meg super object (generally references are used in this case).
The other advantage of pointers vs references is the ability to point them at new objects, useful for optimizing and I''m certain there''s something else they help with, I just can''t remmeber it of the top of my head, maybe the concept of iterators.
Now a reference is a pointer. The only differences is that it has to point to something when it''s created and what it points to can never change (there are very evil, hackish ways of changing it for the curious). And the syntax is different, to help remind you that your pointer can change or may point to nothing.
If C# uses only references (like java) then learning how c++ handles none references and pointers may be more helpful (In fact it''s the same way, copying data when calling functions, or assigning values. But copying pointers is a very fast operation). Changes to none pointers only effect the copy, while change to pointers (and references) affect what is pointed to, and there may be several things pointing to it (multiply names in a sense).
I don''t know C# enough to make that suggestion. C++ is supported on more systems (although mono may help with this). And isn''t controlled by a single entity. But C++ is a harder language to understand with it''s idiosincrasitys (most make sense when you understand the language). If you want to understand the language and don''t want to buy a book I suggest you download and read this (at least the first one).
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
I understood c++ MUCH better after reading it.
A piece of warning function, class function, and class member pointers have ugly syntax (and insanely ugly syntax in some cases). But there are very few times you''ll gain from directly using them.
A)It could point any where since it hasn''t been inited (I think some compilers my init it to NULL, but I don''t think the standard requires it). A big source of bugs. Generally you type
Car *pford = NULL;
to help avoid problems. Most if not all standard function ignore NULL pointers.
B) Nope the new operator returns a pointer to a car and pford is a car, not a pointer.
C) Why is kind of hard to explain. generally if you don''t know how many objects you''ll need at run time you''ll need to make more. I recommend checking if an STL container would be more useful for your dynamic storage. Other important uses are polymorphism and optimization.
D) function is a function that outputs a pointer to a car, and it''s input is a pointer to a car. And in the input has the name ford.
E) *pford is pointer (the address of a car object, generally a 32bit number). pford is a car (requires all the storage of a car object). One advantage is moving single 32 bit number around between functions is a lot faster than your 3 meg super object (generally references are used in this case).
The other advantage of pointers vs references is the ability to point them at new objects, useful for optimizing and I''m certain there''s something else they help with, I just can''t remmeber it of the top of my head, maybe the concept of iterators.
Now a reference is a pointer. The only differences is that it has to point to something when it''s created and what it points to can never change (there are very evil, hackish ways of changing it for the curious). And the syntax is different, to help remind you that your pointer can change or may point to nothing.
If C# uses only references (like java) then learning how c++ handles none references and pointers may be more helpful (In fact it''s the same way, copying data when calling functions, or assigning values. But copying pointers is a very fast operation). Changes to none pointers only effect the copy, while change to pointers (and references) affect what is pointed to, and there may be several things pointing to it (multiply names in a sense).
I don''t know C# enough to make that suggestion. C++ is supported on more systems (although mono may help with this). And isn''t controlled by a single entity. But C++ is a harder language to understand with it''s idiosincrasitys (most make sense when you understand the language). If you want to understand the language and don''t want to buy a book I suggest you download and read this (at least the first one).
http://www.mindview.net/Books/TICPP/ThinkingInCPP2e.html
I understood c++ MUCH better after reading it.
A piece of warning function, class function, and class member pointers have ugly syntax (and insanely ugly syntax in some cases). But there are very few times you''ll gain from directly using them.
As for memory storage there are three sections you''ll probally ever care about. stack, heap and static (global). It looks like you understand stack and heap. I pretty sure static space is the same as global space, the difference between static varibles and globals is that static varibles have limited scope.
Also remeber to delete space used on the heap. In my experience this is best handled in the deconstructors, he function that called new, or with a smart pointer. Manual memory management takes practice to get right. I''m still trying to figure out a good way to deal with fragmentation (I think it only matters when creating objects and destroying obejcts of various sizes in strange orders)
Also remeber to delete space used on the heap. In my experience this is best handled in the deconstructors, he function that called new, or with a smart pointer. Manual memory management takes practice to get right. I''m still trying to figure out a good way to deal with fragmentation (I think it only matters when creating objects and destroying obejcts of various sizes in strange orders)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement