Code:
class A {
public:
virtual void foo(){}
virtual void foo2(){}
virtual void foo3(){}
};
int main() {
A a;
int ret = sizeof(A);
return 0;
}
Basically object a contains a virtual table pointer which is of size 4 bytes. Since class A should have a virtual table which contains three pointers pointing to foo, foo2,foo3 separately. So the virtual table should be of size 12 bytes. I wonder where is virtual table located in memory?
I have found that when I dump a C++ object from memory to a file - it seems that there is a misplacement of the last Virtual-Function-Table pointer - in that appears at the beginning. The result is that the gdump information based on this object dump (using green hills) is incorrect. I copied the contents of the gdump information below. The executable is compiled in linux.
Basically MEIO::CameraStatus contains an item that relates to its parent class (line 188). Then it has 18 items that are all Diagnostics::EventsCounter items. Now for each Diagnostics::EventsCounter item there is a Virtual-Function-Table Info Pointer as its last item. All is fine and good except that the last item of MEIO::CameraStatus which is _selfReset is missing its last item of 4 bytes (which is the Virtual-Function-Table Info Pointer ). On the other hand - right before the first Diagnostics::EventsCounter item ("_vidErrors") - there is an extra 4 bytes which happens to be the Virtual-Function-Table Info Pointer. As I said the gdump information file does not see this.
Why the object memory "moves" the last Virtual-Function-Table Info Pointer to the beginning (right before _vidErrors) and is there a way to "fix" this?
/////////////////////////////////////////////////////////////////////////// "MEIO::CameraStatus" val:0x000002f0 ind208,-1) Struct-Begin Info 188: "" offset 0, Parent-Class Private Info C++ Struct ref = 114 189: "_vidErrors" offset 160, Member Info C++ Struct ref = 128 190: "_vdiErrors" offset 480, Member Info C++ Struct ref = 128
The compiler creates virtual table for the base class and also for the derived class whether we override it or not.
That means each class has separate virtual table. when we get the size of the each class with out any data members... the size of base is -- 4 bytes(64 bit) and the size of derived is -- 1
The size of base class 4 is correct since it creates the virtual pointer internally and its size is member data + virtual pointer, but it in this case I have included any data members so it has given 4 byts.
But why in case of derived is 1 byte, since it the derived class has overridden the virtual function from base, this will also contains the virtual pointer which will be pointing to derived class Vtable, it the size of the class suppose to be 4 instead of 1 byte.
I am having an issue with my sort function. This is one part of the Hash table program. The main issue is that I am trying to sort the pointer table after all of the values have been entered. The ptr_sort function is not a class function and so I am therefore unable to use the class variables psize and pTable. Is there another way I should be trying this? it is a Vector should I use the sort() function from the vector class in STL?
How to approach this? Not very clear on how hashing works..
Insert the following numbers into a hash table of size 10 (ie an array with 0…9 index position) using a hash function h(k)=(k*71+94)%10 and using Linear Probing.
75, 98, 43,1,-56,93,101,34,23
Insert the following numbers into a hash table of size 10 (ie an array with 0…9 index position) using a hash function h(k)=(k*71+94)%10 and using Chaining.
I am trying to create a platformer and is stuck on a problem regarding my virtual class Entity. I wish to use it to create stuff like the Player and Enemy class(es). But how to do the parameter for my collision check function. Below is my Entity- and player class.
There might be a better way to check CC with a lot of different objects, this is my first attempt.
This is the error I am getting: "error C2664: 'bool Player::CollisionCheck(Hostile)' : cannot convert argument 1 from 'Player' to 'Hostile'"
and in Hostile I would (I guess) use bool CollisionCheck(Player p);
But if I try for example to use Player in the CC in player.h it will complain that the function doesn't have an overload for that. Hostile is just a example class name right now, it isn't implemented yet. I am trying to use Player, but if possible wish to be able to have a different class depending on what kind of entity it is. The entity will probably also be the players projectiles and so on.
I have this header file called Shape.h containing these function declarations. and a Shape.cpp which contains the body of the function. I am not showing it since it is not needed.
//This is from Shapes.h header file #ifndef SHAPES_H #define SHAPES_H #include <iostream>
[Code]....
I have this unfinished Main.cpp because the third line "JuanSanchez::Circle *pCar = new Circle; " is giving me a compiler error "error C2061: syntax error : identifier 'Circle' "
#include "Shapes.h" int main() { const int arrayIndex = 4; JuanSanchez::Shape *myShape[arrayIndex]; JuanSanchez::Circle *pCar = new Circle; }
How can I access the virtual base class? This is a practice exercise from c++ primer plus 6.
The problem is that the name becomes No Name instead of the name specified when creating the gunslinger, I don't know how I can call the virtual base class explicitly
Output,
#ifndef PERSON_H_ #define PERSON_H_ #include <string> #include <iostream> #include <cstdlib> using std::string; class person
I create an instance of a base class (not derived class) and assign it to base class pointer. Then, I convert it to a pointer to a derived class and call methods on it.
why does it work, if there is a virtual table?
when will it fail?
// TestCastWin.cpp : Defines the entry point for the console application.//
I have a 'Graph' class, which has derived classes for Adjacency Matrix and Adjacency List representations.
How do I provide iterators for traversing vertices and edges, when the iterator classes would have different implementations for the different derived classes ?
The following way is the only one I can think of, but seems quite cumbersome.
Or is there a pattern for doing this that I'm not aware of ? Would composition be a better idea here compared to polymorphism ? I mean, I can think like..a Graph can 'have' several representation 'objects' within it.
All the involved classes are templates,not sure if that makes the situation different.
Below is simplified code consists of two classes, namely Parent and Child.
Child is inherited from Parent.
All member functions of class Parent are declared virtual, and they have been overridden in the class Child.
Code 1:
#include <cstdlib> #include <iostream> using namespace std; #define QUANTITY 5 class Parent {
[Code] ....
The output of the code:
Child::showID() -- ID is 1804289383 Child::showID() -- ID is 846930886 Child::showID() -- ID is 1681692777 Child::showID() -- ID is 1714636915 Child::showID() -- ID is 1957747793
Parent::operator=() invoked.
Child::showID() -- ID is 1804289383 Child::showID() -- ID is 846930886 Child::showID() -- ID is 1714636915 Child::showID() -- ID is 1714636915 Child::showID() -- ID is 1957747793
Question:
Why is Parent::operator= invoked instead of Child::operator= ..?
Isn't it already declared virtual and hence would be overridden..?
I need to invoke Child::operator= instead. How to achieve this?
I need a2 to be a deep copy of a1, but if I understand it correctly, then a2 should just be a pointer copy of a1. How do I make a2 be a different instance of B?
I develop add-ons for MS Flight Simulator and I use the poorly documented SDK for this. The SDK provides a .h file in which an interface class is defined (with pure virtual methods only), something like this:
In my code, I use this interface like this: Code: IPanelCCallback* pCallBack = panel_get_registered_c_callback("fs9gps"); ... SINT32 id; pCallBack->ConvertStringToProperty(propertyName, &id);
Everything works fine, but I don't understand why... I thought the linker would stop with an "undefined symbol" error because the IPanelCCallback methods, such as ConvertStringToProperty, are declared as pure virtual but defined nowhere, and I don't use any library for linking. With such an interface class, I thought I would have to defined a subclass of IPanelCCallback and define the ConvertStringToProperty method.
Is there way to find the size of structure pointer size? When we tried to get the size of structure pointer will get size of address(@ location) which would be 4 bytes long. But I want to get the size of all structure members size using structure pointer.
If I have an array and all I have is an upper limit on how big the array can get, and if the number of elements that get added can be considerably smaller than this limit, is it always the right choice to declare a pointer and just reallocate extra memory whenever the array grows? For instance, instead of declaring int a[max] I can declare a pointer int *a and than just realloc when I add elements.
That code should make the size of the pointer (how many chars it can store) bigger but when i run it it show always 3 char positions while it should show N*M.
Code: #include <stdio.h>#include <stdlib.h> int main(void) { int M, N, P, i; scanf ("%d %d", &M, &N); P = M * N; char *c = malloc(P * sizeof(char));
I have function that looks like this myfoo(char* Name) Now i want to compare this name to another one . But the another name is a pointer . This my code :
I'm trying to keep track of the size of blocks of memory that a pointer points to. No matter what I do, this code below always outputs the integer 8.
If I change 1000 to 5, I still get 8. If I change it to 0, I get 8... If I change it to -1, I get 8. If I change int *a to double *a, I get 8. If I take away the & symbol, I get 8. If I use *& instead, I get 8.
Why? I want it to output 1000. If I change that to 500, I want it to output 500.
int *a; a = malloc(1000 * sizeof(int));
int j = sizeof(&a); printf("%d", j);
I want to build my skills where I can allocate, inspect and change memory sizes.