Hello,
Attached are two codes.
Please compile them and check the output.
The question I have is that I am unable to understand why is it that the memory location allocated to tempmemcheck are 8bytes + the last memory location and not 4 bytes plus the last memory location.
Similarly, why is the memory location of ver2 on heap 16+ memory location of ver1->tempmemcheck and why not 4 + memory location of ver1->tempmemcheck.
I am using g++ of gcc version 4.1.1 20061011 (Red Hat 4.1.1-30)
In anticipation of an enlightening reply..


main.cpp:

#include <iostream>
#include "graph.h"
using namespace std;

int main()
{
        int a=5;
        int *b=new int;
        *b=6;

        vertex *ver1=new vertex(5);
        vertex *ver2=new vertex(5);

        printf("Here comes all the address listings:\n");
        printf("variable a: %x\n",&a);
        printf("variable b: %x\n",&b);
        printf("memory location alloted to int b: %x\n",b);

        printf("variable ver1: %x\n",&ver1);
        printf("memory location alloted to struct ver1, known by its members: %x %x %x\n" ,&(ver1->id),&(ver1->status), &(ver1->tempmemcheck));
        printf("memory location alloted to struct ver1, known directly : %x\n", ver1);
        printf("Memory location alloted to member tempmemcheck of struct ver1: %x\n",ver1->tempmemcheck);


        printf("variable ver2: %x\n",&ver2);
        printf("memory location alloted to struct ver2, known by its members: %x %x %x\n" ,&(ver2->id),&(ver2->status), &(ver2->tempmemcheck));
        printf("memory location alloted to struct ver2, known directly : %x\n", ver2);
        printf("Memory location alloted to member tempmemcheck of struct ver2: %x\n",ver2->tempmemcheck);

}

graph.h in the same directory as main.cpp

class vertex
{
        public:
                int id;//ID of the vertex;
                int status;//Status of the vertex;
                int *tempmemcheck;

                vertex(int n)
                {
                        id=n;
                        status=0;
                        tempmemcheck=new int;
                }

};

Here is the output using VC++ 2008 Express on Vista. Is this the same as what you got? I'm sure the answers to your questions is how your compiler aligns data, on 1, 2, 4, 8 or 16 byte boundries.

Here comes all the address listings:
variable a: 26fe00
variable b: 26fdf4
memory location alloted to int b: 943f90
variable ver1: 26fde8
memory location alloted to struct ver1, known by its members: 943fc0 943fc4 943f
c8
memory location alloted to struct ver1, known directly : 943fc0
Memory location alloted to member tempmemcheck of struct ver1: 941258
variable ver2: 26fddc
memory location alloted to struct ver2, known by its members: 941288 94128c 9412
90
memory location alloted to struct ver2, known directly : 941288
Memory location alloted to member tempmemcheck of struct ver2: 9412c0
Press any key to continue . . .

commented: Thanks you. That was very helpful +2

Yest I got similar results though the values keep changing due to memory address randomization but the differences between addresses are all the same.

Here comes all the address listings:
variable a: bfb2ddbc
variable b: bfb2ddb8
memory location alloted to int b: 95d2008
variable ver1: bfb2ddb4
memory location alloted to struct ver1, known by its members: 95d2018 95d201c 95d2020
memory location alloted to struct ver1, known directly : 95d2018
Memory location alloted to member tempmemcheck of struct ver1: 95d2028
variable ver2: bfb2ddb0
memory location alloted to struct ver2, known by its members: 95d2038 95d203c 95d2040
memory location alloted to struct ver2, known directly : 95d2038
Memory location alloted to member tempmemcheck of struct ver2: 95d2048

Thanks for the answer about memory alignment but now a new confusion is that in your answer why is it that : struct ver1 ends at 943fc8 but tempmemcheck starts at 941258..
I dont quite understand the heap allocation here.
Pease help


memory location alloted to struct ver1, known by its members: 943fc0 943fc4 943fc8
Memory location alloted to member tempmemcheck of struct ver1: 941258

we have no way (in most cases that is) of controling where or how new allocates memory. Its purely up to each compiler how it wants to do it. One way is for the compiler to search its linked list of free memory blocks and return the smallest block that is at least as big as the amount of memory requested. Therefore memory blocks returned by new may or may not be adjacent, depending on other allocations/deallocations that took place.

> vertex *ver1=new vertex(5);
> vertex *ver2=new vertex(5);
The results of any two memory allocations are un-comparable. Sure, right at the start you may convince yourself there's a pattern, but run any decent sized program for a while and try it again and the results would be all over the place.

> Similarly, why is the memory location of ver2 on heap 16+ memory location of
Depends how your allocator works. Very few allocators actually allocate just the amount you ask for. Most pad and align the requests in some way to balance performance against memory use. For example, all really small requests could be rounded up to say 16 bytes.

Plus there is the allocator overhead of storing information about each allocated block (like it's size), along with pointers to other memory blocks. Debug versions of the allocator can store additional information to assist with memory related problems such as memory leaks.

commented: Thank you. That was very helpful!! +2

Thanks for your enlightening answers.
@Ancient Dragon: You probably mean memory manager as the compiler has no control over dynamic memory.
Thank you Ancient Dragon and Salem for help!!

>>@Ancient Dragon: You probably mean memory manager as the compiler has no control over dynamic memory.

Depends -- memory management is a multi-terr operation. At the lowest level is the operating system that manages system-wide memory. Then you have the program's memory manager that manages all memory that is requested by new or malloc() functions. The program's memory manager, not the os, requests large chuncks of memory from the os and then diveys it out a little at a time to the program via the new and malloc() functions. When delete or free() is called the memory is kept by the program's memory manager and put into a free list so that it can quickly allocate it again.

Thank you very much for this enlightening information. I didn't know this much of detail about memory allocation.
Do you mean that every single program, however small, lets say just a cout, has a memory manager of its own? If yes, in that case, is it provided by the runtime linked libraries?.

yes and yes. There is a small amount of code that gets executed before main() which sometimes allocates memory too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.