When using member-function-pointers I noticed that they have a size of 8Bytes each. However when I ask the compiler it says it is only 4Bytes. Having a couple of objects in you program which have member-function-pointers and are created dynamically with new will allways crash your program.
Please refer to the following source-code and documentation:
#include <StdIO.h> #include <Reg164ci.h> /* A simple external object */ class cCalculator { public: int calculate(); }; int cCalculator::calculate(){ return 27; } /* The description of the member-function-pointer */ typedef int (cCalculator::*fptr)(); /* An object with an array of such member-function-pointers which is able to call these functions by an index. */ class cAnObject { public: cAnObject(); ~cAnObject(); fptr fptr_array[2]; // 2 x 8 bytes = 16 bytes (see watchwindow) int member1; // 2 bytes int member2; // 2 bytes // -------------------------------------- // total: 20bytes = 0x14bytes }; /* The constructor */ cAnObject::cAnObject() { member1 = 1; member1 = 2; } /* The destructor */ cAnObject::~cAnObject() { member1 = 0; member1 = 0; } int main (void) { // now we have the external object cCalculator calc1; cCalculator calc2; // and a couple of objects containing some function pointes cAnObject test1; cAnObject test2; cAnObject test3; fptr memberfunction = &(cCalculator::calculate); test1.fptr_array[0] = memberfunction; // here we get the size of our objects (see watch window) volatile unsigned long object_addr1 = (unsigned long) &test1; volatile unsigned long object_addr2 = (unsigned long) &test2; volatile unsigned long object_sizeA = object_addr2 - object_addr1; volatile unsigned long object_sizeB = sizeof( test1 ); //... // please notice that the static objects have a distance // which is different from its size ? why? // count the bytes and you see the sizeof() is wrong !! // now we simply create the dynamic object (with the wrong size), why ? compiler ? // which here leads to an -access violation-, due to // the wrong calculated size of the function pointers cAnObject * p_test4; p_test4 = new cAnObject; // and set the function p_test4->fptr_array[0] = memberfunction; // calc the size volatile unsigned long objectSize4 = sizeof( (*p_test4) ); // ---- // now the function call itself cCalculator * p_calc; // here we select our target object p_calc = &calc1; // do the static object call by an index == 0 fptr p_fct1 = test1.fptr_array[0]; int x = ( (p_calc)->*( p_fct1 ))( ); // now use the other object p_calc = &calc2; // do the dynamic object call by an index == 0 fptr p_fct4 = p_test4->fptr_array[0]; int y = ( (p_calc)->*( p_fct4 ))( ); // if there is any time please notice that there is a second bug: // try to remove the line "fptr p_fct4 = ..." // and put it into one line like " int y = ( (p_calc)->*( p_test4->fptr_array[0] ))( ); " // and your whole program will crash ! // just print the result printf("%d = %d \r", x, y ); while (1); }
is that a typo?
>> p1 = 0x10fac
should it be
>> p1 = 0x10fcc
testresults1:
// test from Per Westermark cAnObject obj1; cAnObject array[2]; volatile unsigned long size_obj1 = sizeof(obj1); >> 0xC volatile unsigned long size_array = sizeof(array); >> 0x18
testresults2:
cAnObject c; cAnObject *p0 = &c; cAnObject *p1 = p0+1; >> p0 = 0x10fc0 >> p1 = 0x10fac
You are exactly right this is may problem!
Please report back the results of the test cases I described.
... If malloc gave me to much space due to some padding-stuff ,... who cares? But it gives me not enough memory!
Allright. But this is not the point. The point is that malloc gives me a pointer to an object and whenever I write to its member-variables it writes to an address which is beyond the allowed space. What only could be a reason for a wrong sizeof()! (see -access violation- )
Again this usually works, but not if any member is a member-function-pointer or an array of it.
What is the sizeof for obj1, and array below?
cAnObject obj1; cAnObject array[2];
or possibly:
cAnObject c; cAnObject *p0 = &c; cAnObject *p1 = p0+1;
What is the value of (char*)p1 - (char*)p0?
Whatever padding the compiler does, n*sizeof(obj) must be the amount of memory to allocate for an n-sized array, and incrementing an object-pointer must move the pointer forward exactly sizeof() bytes.
Note that your assumptions that an allocated memory block is always 6 bytes larger is not true. That depends completely on the memory manager. All you know is that the pointer you receive has the requested number of usable bytes, and has the best alignment needed for any data type. If for example the processor requires that double-precision floating point numbers are aligned on an n*8 address, then malloc() will return a pointer that is _at least_ aligned to that requirement.
... what from my point of view proofs that the internal sizeof() function calculates the wrong size for my objects with member-function-pointers.
I understand the preceeding comments from Hans-Bernhard Broeker and Tamir Michael as question of how can I be sure that the linker puts both objects into the memory as I assume it does.
(1) As mentioned, you can count both objects byte-size and you figure out its distance is the correct size. But this is not the point!
(2) You can create two objects dynamically with new at once, where you get two pointers which actually point to the correct starting of the two objects (note that there is an addtional space for the mem-manager of 6bytes -> Kernighan & Ritchie). Then write to the second-object member-variables "member1" and "member2" and the "fptr_array" and watch the contents of the first object. You will see your written variable-data in the first object not in the seconed as you did!! Coincidence? And there are rules of how dynamic memory is allocated and manged -> Kernighan & Ritchie. There are no rules that allow modifying one object which automatically manipulates another. (As far as I know).
(3) This behavior shows up only if you use member-function-pointers or arrays of it in objects which you create dynamically. (note the -access violation- while creating the object.)
As I noticed, the normal function pointer has a size of 4bytes however the member-function-pointer has a size of 8bytes. When you rise your array size count from nr=2 (fptr fptr_array[2]) to anything else you will see there is allways a difference of 4bytes x nr not 8bytes x n.
"compiler tend to patch structues/objects to have a size that is a power of 2, so that iterations offsets can be easily calculated using shifts."
Thrue - but not just any old "power of 2", and not (directly) for computing offsets:
Processors with a Word size larger than one tend to have specific preferences or even requirements on the alignment of objects - especially multi-byte objects.
Therefore, the compiler will tend to produce code that follows those preferences - which may involve "padding" in structures and between variables.
Sven, Some of your assumptions are unfounded. As Hans-Bernhard Broeker noted, you cannot assume anything about the placement of objects - it depends of quite some factors, including the optimination level. In addition to that, do remember that compiler tend to patch structues/objects to have a size that is a power of 2, so that iterations offsets can be easily calculated using shifts. This and other factor may contribute to the observations you made. Please refer to your map file for complete details.
// please notice that the static objects have a distance // which is different from its size ? why?
Why not?
// count the bytes and you see the sizeof() is wrong !!
You have nowhere near enough proof to back such an outrageous claim. There's no rule forcing the tools to locate individual variables in memory like you assume it must.
View all questions in Keil forum