Jump to content
  • Advertisement

Some question in memory [ Data Structure ]

This topic is 461 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi i am actually having some problems in understand what happens in the memory ...
for example i have 2 int variables [ int x  and y ]

x = 15 and y = 30
in memory for example the address of x = 0x123456 and the address of y = 0x654321

now if i did this x = y
isn't that means in memory the address of y will be the same for x or what 

Now here in java in creating objects ...
say that i have a class called a and the attributes is name and age so when i create an object :
a objectreference = new a();

now i have allocated the memory for the object of class a and a reference called objectrefernece reference to object of class a 

now how this objectreference reference to the object ? how that's done in memory 
also what happens in memory when i create an object and what happen to the attributes i wish if u can answer me :)

Edited by User134

Share this post

Link to post
Share on other sites

You can think of memory as a grid of boxes. Each box is at a specific address and in each box there is a value.

So if you say, int x, you've told the compiler to reserve a box and call it x. You say, x = 30, then the value 30 is put into that x box.

So with pointers, you say, ptr = address of x. What happens is that the compiler reserves a box and calls it, ptr, and then the address of the box x is put into the ptr box. Later when you use the ptr, the compiler *dereferences* its value ( address of x ) and retrieves the value in the x box.


Share this post

Link to post
Share on other sites

so when i say x = y that means the value of y equals x not means the address of y = x right ?
in java there's no pointers linke the * in c++ so how for example can i make the address of x equals the address of y

and what about the objects :D

Share this post

Link to post
Share on other sites

most people think of memory as boxes that can hold something.  If you picture a 32 bit integer (4 bytes) as a long skinny box, big enough to write a big number in, that support dry erase markers.  so write now you have 2 long skinny dry erase boards on them, the first has 15 written on it, and the second one has 30 written on it.  the "compiler" knows the first one (called x) is the one on the left (also know as address 0x123456) and the one called y is the one on the right (at address 0x654321).

Now a pointer, is also just another number, so in a language like C++ I could have another variable, we'll call it "current pointer" that i write the number 0x123456 into ... this means, the current pointer has a number in it, which happens to be the same number as the address of the box on the left.  The better analogy for this is a phone number.  Get 2 friends to be those numbers, tell your first friend (mary) to remember the number 15, and your second friend (tom) to remember the number 30.  Then write the phone number of mary on your board.  If your mom (the program) tries to "look up" the value pointed to by current pointer (look in the box at address 0x123456 or in this case, call mary and ask her for the number she's remembering) then she/it will get the number 15.  If the program is supposed to multiple that number by 3 and save it back into the same variable it will store 45 back into the box (tell mary to remember 45 instead of 15),

Now an "object" is more like a custom designed box, with more than 1 slot in it, each made to hold a certain thing.  So a 2D point object is a box with 2 slots in it, each of which can hold a number.  In real computer terms this is just a "map" of how to interpret a set of contiguous memory.  So in your example of a class with a Name and Age, it is a map that says, if the object starts at a certain address, then the first thing at that address will be a pointer to a string that shall be referenced as the Name property, and the second thing after that shall be an integer that shall be referenced as the Age property.  

Lets assume that all objects in Java use 4 bytes for their "vtable" pointer (i'm not going into that right now), and then they use additional bytes for every property they have.  In this case then, when you call "a objectreference = new a();", you have allocated 12 bytes of memory, the first 4 bytes (offset 0-3) are a vtable pointer, (offest 4-7) are a string pointer, and (offset 8-11) are an integer.  If you allocate an array of 100 objects of type a, then you will have created 1200 bytes of objects in memory, the pattern would repeat every 12 bytes.  So if the array started at address 0x800, then at 0x808 would be the first object's age, and at 0x814, 0x820, 0x82C, etc ... would be the age property of the second, third and 4th object.

A type is kinda like a stencil, that tells the compiler how to look at/interpret a section of memory.  The computer doesn't know much special about what things in memory mean, so if they are told wrong, they just blindly try to do the wrong thing.  So if someone the compiler allocated the array of 100 objects are 0x800, but say it got corrupted and accidentally thought the array started at 0x804.  Then when it went trying to read the Age property of the first object (which is actually at 0x808) it would erroneously get whatever is in the vtable of the second object (0x804 + 8 byte offset = 0x80C which really the start of the second object) ... so the computer would just start doing stupid stuff, and eventually crash.

Share this post

Link to post
Share on other sites

Objects are just groupings ultimately of fundamental types such as int. A collection of boxes. 

References are just pointers with different restrictions on how they can be used. Both work with addresses as their value instead of  data such as, x= 30.

you can't change the address of a box, they are fixed. However using references, two can each refer to the same address or box. So for instance, you have three variables where two are references. Each of the references can refer to the same variable, and in that case, x==y and each x and y refer to a variable, z= 30.


Share this post

Link to post
Share on other sites

So if i have a class of 2 attributes 
class A{
int age = 15,code = 30;
new A(); 
now this code allocates 12 bytes in memory for the object which for example :
say that the address of the object is 0x100 
so the age will be at 0x104 and the code will be at 0x108 (4 bytes (int) ) 
is that right ?
now when i do that A obj = new A();
obj is a pointer which points to the object and the age and the code 
is that right and i really wanted any videos about that to understand better because i stopped coding just to understand how things work

Edited by User134

Share this post

Link to post
Share on other sites

So if i have 2 boxes one for x and the second one for *ptrx 
Say that the box of x is in location 0x1000 
and the ptrx is in the location 0x2000
int x = 15;
int *ptrx;
now if i made this ptr points to x how the boxes will be at the same location ???!
for example how we will have a box of x in the location 0x1000 and the box of pts in the location 0x1000 :(

Share this post

Link to post
Share on other sites

ok, importantly, you never move boxes (aka memory), you copy values into them, read values from them, etc.

So let me give you a script for a comptuer:

1. create a pointer to an integer on the stack (aka declare a local variable for type int*), call this variable "current ptr".  We'll assume this is at address 0x200, and the current value stored in it is "undefined" ...

int *current_ptr;

2. create a new array of 10 integers, and store the address of the start of this array in the variable called "current ptr".  We'll assume the array was allocated on the heap starting at memory location 0x300 and running through 0x327.  So now the box at 0x200, has the value 0x300 stored in it.  So if you read the value of current_ptr, it is 0x300.  But if you read the value POINTED TO by current_ptr, you would get an undefined value.

current_ptr = new int[10];

3. initialize the values of each array element to its index.  So now the values at 0x300, 0x304, 0x308 ... are 0, 1, 2 respectively.  But the value stored in 0x200 is still 0x300

for(int i=0; i<10; i++)

  current_ptr = i;

4.  Change current pointer to point to the item whose value is 5.  So now the value/address 0x314 would be stored in current pointer (aka in the box at 0x200.

while(*current_ptr < 5)


NOTE - now that we changes current_ptr and didn't remember the start of the address anywhere, we have a little problem .. we've "lost" the first 5 elements of the array.  They are still there at 0x300-0x313 ... but our program no longer knows that.  It just has an integer pointer pointed to 0x314 (the middle of the array) ... if we write the following:

for(int i=0; i<10; i++)

  printf("%i", current_ptr);

it would print:

5 6 7 8 9

and then it would crash because current_ptr[5] is the address 0x328, 1 byte past the end of the original array of 10 integers we allocated.  Basically, we've gone too far - opps!

So "numeric variables" are just boxes of memory.  "pointers" are also just boxes of memory.  The value in a numeric variable is just a number stored in memory, the value in a pointer variable is just a number stored in memory.  The difference is in how we USE the value.  If the number 20 is stored in an integer, the computer will support operations like x+x which would be 20+20 => 40.  If the number 20 were stored in an integer pointer, it is still the same bit pattern 10100, but the computer will support operations like *x which means use the number stored in x (20) as the address of ANOTHER number ... go to that address and get the value there - so *x + *x means ... go get the number value pointed to by x .. and add it to itself ... so whatever number is stored in the bytes in memory at address 20-23 would be read and used (or if we didn't allocate that memory, we would get a memory access violation exception and our program would crash)


Share this post

Link to post
Share on other sites

Suppose i have this code :

class B{
    int age;
    int code;
    cout << "Age | " << &age << endl;
    cout << "Code | " << &code << endl;


int main()
   B obj;
   cout << "Object | " << &obj << endl;
   return 0;

Output :
Age | 0x6afef8
Code | 0x6afefc
Object | 0x6afef8

here why the object address is the same for the age address ?

Share this post

Link to post
Share on other sites

I guess it is some reason similar to an array example, where if you do

	int myArr[5];

	*myArr = 200;
	cout << &myArr << endl;
	cout << &myArr[0] << endl;




then you notice that the name of the array is actually a pointer to the first element.
So maybe your Object is actually just a pointer to the first variable inside of it(even though is not a pointer? now I'm confused xD).

I wait for the experts to answer, mine was only a wild guess :)


Edited by MarcusAseth

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!