+ 2

Am I being too literal?

For my part of the assignment about tracking memory allocation I have done this: int Integer; int* PI; int main() { PI = & Integer; cout << sizeof(Integer)<<endl; cout<<PI; return 0; } Surely they want more than this. Especially as they don't just want text output?

4th Aug 2017, 1:31 PM
Richard Appleton
Richard Appleton - avatar
14 Respuestas
+ 7
They probably want you to look at new and delete as well http://www.cplusplus.com/doc/tutorial/dynamic/ also, globals are not great to use. see this copy of your code I have modified to make them local https://code.sololearn.com/cpoNYuFs9y3Q/?ref=app
4th Aug 2017, 1:50 PM
jay
jay - avatar
+ 7
https://www.ibm.com/developerworks/aix/tutorials/au-memorymanager/index.html
4th Aug 2017, 3:33 PM
jay
jay - avatar
+ 6
talk about throwing you in the deep end 😆
4th Aug 2017, 3:29 PM
jay
jay - avatar
+ 6
no worries!
4th Aug 2017, 3:43 PM
jay
jay - avatar
+ 4
The address pointers hold are all integers. You specify the type the address points to by proceeding the type with *. So for char it's char*
4th Aug 2017, 1:42 PM
aklex
aklex - avatar
+ 4
I can't see your assignment, but your code works here (keeping SoloLearn's template includes). I verified this is working by setting a value: int Integer=99; and dereferencing the pointer: cout<<*PI;
4th Aug 2017, 1:57 PM
Kirk Schafer
Kirk Schafer - avatar
+ 4
@Richard Appleton ... Yeah, I saw your interface/implementation Q&A, started an analogy to a grocery store / inventory vs shopping, then realized a visual code would be better. In the meantime you had lots of help so that was nice :)
4th Aug 2017, 2:16 PM
Kirk Schafer
Kirk Schafer - avatar
+ 4
Seriously though, this looks like fun. I'll be happy to practice helping without answering if the situation warrants.
4th Aug 2017, 3:31 PM
Kirk Schafer
Kirk Schafer - avatar
+ 2
Thanks Kirk. My full assignment is on my post called "More Newbie Questions" but this part of it is to write a code which tracks and reports memory allocations.
4th Aug 2017, 2:04 PM
Richard Appleton
Richard Appleton - avatar
+ 2
Yes. everybody on here is very patient.
4th Aug 2017, 2:17 PM
Richard Appleton
Richard Appleton - avatar
+ 2
That's brilliant.. Thanks, @Jay! :-)
4th Aug 2017, 3:38 PM
Richard Appleton
Richard Appleton - avatar
+ 1
Ok... So I have clarification of what they actually want: You should be aiming to do the following: · Take control of memory allocation and deallocation via some kind of management class or other implementation Then · Track and report any memory leaks (Any memory which is allocated should be deallocated, if it is not deallocated the memory is still in use and can continue to eat into system resources. This is a memory leak.) · Implement a method to deal with memory fragmentation at runtime. Fragmentation occurs when old memory is deallocated and new memory is allocated. Consider this situation: You allocate three blocks of memory to separate objects. You deallocate the second block of memory as it is no longer useful. Leaving a block of free memory between the other two blocks of memory. When you allocate new memory you reuse this free block of memory, however, the new object you allocate memory to takes up fractionally less memory and there is a small amount of free memory available between the second block and the third block. This block of free memory is too small for anything to be allocated to it. If this memory fragmentation happens continually without any kind of countermeasure, you will eventually run out of free memory while having lots of free memory!
4th Aug 2017, 3:16 PM
Richard Appleton
Richard Appleton - avatar
+ 1
Haha. Yeah. This is going to be a google job when I get home, just to find out what it means!! :-D
4th Aug 2017, 3:31 PM
Richard Appleton
Richard Appleton - avatar
0
Are all pointers not integers? how do I set a pointer to the memory address of a character?
4th Aug 2017, 1:31 PM
Richard Appleton
Richard Appleton - avatar