Thread: Questions on Memory

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    61

    Questions on Memory

    Keep in mind that I am relatively new to programming in general, so these questions may be a little foolish.

    1) How much memory does a normal variable use?
    2) What is the difference memory-wise between a normal variable and a pointer?
    3) How much memory does a pointer use?
    4) What is the difference between a normal variable or pointer and dynamically allocated memory?
    5) Why do you have to free dynamically allocated memory, but not regular variables or pointers?

    Like I said before, I am new to programming. I have read tutorials on C++, but I don't quite follow certain parts, especially those parts dealing with memory, hence the questions. Thanks in advance for any help!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    For your first three questions start with this link and for the rest see this.

    Jim

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Thanks for the links. So, from what I understand:

    A variable always takes a certain amount of memory based on what type it is (int, char, float, short int, etc.). If you try to give a variable a value outside of its range, it will return an error.

    A pointer always uses the same amount of memory, regardless of the type, because it does not actually contain a value, simply the adress of a value.

    Dynamically allocated memory allows you to declare a pointer or array of a size which is not previously determined.

    A couple more questions:

    1) How does the memory of a normal variable or pointer get freed?
    2) Because the identifier of an array is equivalent to the adress of its first element, is an array in essence a pointer? How much memory would it then use?

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by LyTning94 View Post
    If you try to give a variable a value outside of its range, it will return an error.
    It will give a warning, if the value is known at compile time. Overflow of a signed integer results in undefined behaviour.

    Quote Originally Posted by LyTning94 View Post
    Dynamically allocated memory allows you to declare a pointer or array of a size which is not previously determined.
    Not "declare a pointer" - allocate memory and return address to it.

    Quote Originally Posted by LyTning94 View Post
    1) How does the memory of a normal variable or pointer get freed?
    2) Because the identifier of an array is equivalent to the adress of its first element, is an array in essence a pointer? How much memory would it then use?
    Variables are destroyed when they go out of scope (global variables are destroyed when the program ends).

    Arrays are not pointers and occupy N * sizeof(T) bytes. Expression of an array type returns pointer to its first element.
    Last edited by kmdv; 03-23-2011 at 09:58 AM.

  5. #5
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Try sizeof() and you will come to know...
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Okay, a few more questions. Hopefully the last!

    Say for example, I create the following class:

    Code:
    class Arithmetic
    {
    public:
    	int AddTo(int Num);
    	int AddToTen();
    };
    and then do something like this:

    Code:
    #define ten     10
    
    int Arithmetic::addToTen()
    {
    	Arithmetic number;
    	Arithmetic sum;
    	cout<<"Please Input A Number: ";
    	cin>>number;
    	sum = number.AddTo(ten);
    	return sum;
    }
    1) how much memory does number or sum take up (because I didn't give it a type)?
    2) how much memory does ten take up (because, again, I defined it, but didn't give it a type)
    3) how much memory does a function use? (I'm assuming it's varied?)

  7. #7
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    1. No Size.
    2. Depends upon your machine register size.
    3. Yeah, it varies, and i believe a function doesn't contain the whole memory at once.

    I may be wrong, but i think what i told is right, according to my knowledge...
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Quote Originally Posted by Mr.777 View Post
    1. No Size.
    How can it not take up any memory? If I give it a value, it has to record that value somewhere in memory, right?

  9. #9
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by LyTning94 View Post
    How can it not take up any memory? If I give it a value, it has to record that value somewhere in memory, right?
    How may you even assign it any value???
    You have no variables in your class, nor any constructor....
    So?
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Quote Originally Posted by Mr.777 View Post
    How may you even assign it any value???
    You have no variables in your class, nor any constructor....
    So?
    I apologize. I am still learning the language, as you can see, and my knowledge of classes is limited. If I understand correctly, creating an object of a class (number and sum in my previous example) allows you to assign different values to each variable based on which class object you are currently referencing. Which means you can't assign a value to an object of the class itself, right? So object1.x != object2.x, even though you have only declared x once, correct?

    I guess my question then is, if you have only declared x once yet it holds different values, it has to occupy two different locations in memory (one for object1.x and one for object2.x), correct?

  11. #11
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by Mr.777 View Post
    1. No Size.
    2. Depends upon your machine register size.
    3. Yeah, it varies, and i believe a function doesn't contain the whole memory at once.

    I may be wrong, but i think what i told is right, according to my knowledge...
    1. Size does not need to equal 0
    2. Implementation-defined
    3. Function, if virtual, will force compiler to generate v-table, which is usually a pointer. Class size will grow then.

    How can it not take up any memory? If I give it a value, it has to record that value somewhere in memory, right?
    You can still perform assignments on empty classes, it's semantically valid. It will just have no effect, if you don't declare your own copy operations.
    Last edited by kmdv; 03-24-2011 at 08:15 AM.

  12. #12
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Yeah, I'd already seen that. Thanks for all the help!

  14. #14
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    just to be pedantic, I think I should clear a few things up.

    * if I recall correctly, the C++ standard does not mandate that pointers be implemented as memory addresses, but every implementation I've seen implements them as such.

    * an instance of a class need not take up any memory if it has no data members and no virtual functions. I suspect that most implementations implement it as 1 byte - GCC does, but I haven't had an opportunity to test others.

    * regarding your question "how much memory does ten take up", it takes up no memory at all, because it is a preprocessor macro. the preprocessor replaces references to 'ten' with the literal value 10, which then takes up memory space - as much space as an int takes, although with optimizations turned on, it's likely that the compiler will put that value in a register or put it in the assembly code as a literal, where it will take no memory at all. you wouldn't be to refer to it by a pointer. for example:
    Code:
    #define ten 10
    int* ptrToTen = &ten;
    is not legal code, because 'ten' is not a variable.

  15. #15
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    I think it will be more clear to say first that this is token replacement:
    Code:
    #define ten 10
    int* ptr = &ten;
    is equivalent to:
    Code:
    int* ptr = &10; // !
    You should avoid such short names for macros, because they are 'global', and you will not be able to use 'ten' as identifier anymore.
    Code:
    static const int ten = 10;
    Much better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  2. Replies: 7
    Last Post: 02-06-2009, 12:27 PM
  3. Random memory questions
    By Blackroot in forum Windows Programming
    Replies: 3
    Last Post: 04-04-2007, 09:33 PM
  4. Memory handler
    By Dr. Bebop in forum C Programming
    Replies: 7
    Last Post: 09-15-2002, 04:14 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM