C++ :: Virtual Table Pointer - Size Of A Class?
Apr 22, 2015
Here is an example,
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?
View 2 Replies
ADVERTISEMENT
Jul 29, 2014
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
[Code] .....
View 4 Replies
View Related
Jan 21, 2014
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.
#include<iostream>
class A{
public:
[Code].....
View 1 Replies
View Related
Mar 16, 2013
Let's look at this simplified code, it gives compilation error
#include <iostream>
using namespace std;
class A {
public:
void showInfo() { cout << " This is an instance of A" << endl; }
[Code] ....
Without using pointer, how to make this works?
View 8 Replies
View Related
Nov 19, 2013
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?
#include "/home/onyuksel/courses/340/progs/13f/p9/util9.h"
#include "/home/onyuksel/courses/340/progs/13f/p9/hTable.h"
#ifndef H_TABLE1
#define H_TABLE1
void ptr_sort ( );
[Code] ....
View 1 Replies
View Related
Dec 27, 2012
class Parent{
public:
virtual int width();
virtual int height();
int area(){return width()*height();};
[Code] ....
View 10 Replies
View Related
May 10, 2014
I would like to create a table but I dont know how big. Thats why I want to use variable to describe its size. Something like that:
int x = 5; //I will use a function to set x
int Table[x];
What should I do to make it work?
View 4 Replies
View Related
Sep 30, 2014
I've got the following code which demonstrates a problem :
Code:
struct A {
double x[2];
double i;
};
struct B : virtual public A {
double y[2];
[code]....
I'm wondering if this is a compiler bug. Why doesn't the pointer-to-(derived)-member work for the array if it works for the non-array?
View 4 Replies
View Related
May 28, 2014
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.
75, 98, 43,1,-56,93,101,34,23
View 2 Replies
View Related
Mar 27, 2015
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'"
#ifndef ENTITY_H
#define ENTITY_H
#include <SFMLGraphics.hpp>
class Entity {
public:
Entity();
Entity(sf::Vector2f position, sf::Vector2f size, sf::Color fillColor, sf::Color outlineColor);
[Code] ....
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.
View 1 Replies
View Related
Feb 17, 2013
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;
}
What Could be causing this error?
View 8 Replies
View Related
May 4, 2013
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
[code]....
View 7 Replies
View Related
Feb 6, 2015
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.//
#include "stdafx.h"
#include <iostream>
class B
{
public:
B(double x, double y) : x_(x), y_(y) {}
double x() const { return x_; }
[Code] ....
View 14 Replies
View Related
May 29, 2013
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.
Code:
class Base {
public:
class BaseIterator {
};
virtual const BaseIterator& begin();
virtual const BaseIterator& end();
[Code] .....
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.
View 7 Replies
View Related
Mar 20, 2013
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?
View 12 Replies
View Related
Oct 27, 2014
In short, this is what I have
class A{
A(){}
virtual void pure() = 0;
}
[Code] .....
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?
View 5 Replies
View Related
Jul 11, 2012
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:
Code:
class IPanelCCallback {
public:
virtual IPanelCCallback* QueryInterface (PCSTRINGZ pszInterface) = 0;
virtual bool ConvertStringToProperty (PCSTRINGZ keyword, SINT32* pID) = 0;
virtual bool ConvertPropertyToString (SINT32 id, PPCSTRINGZ pKeyword) = 0;
};
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.
View 6 Replies
View Related
Aug 26, 2013
Assume this class:
class GenericTrafficLight {
public:
virtual enum LightState;
void setLightState(LightState newState) {
currentState = newState;
}
private:
LightState currentState;
};
And this deriving class:
class FuturisticTrafficLight : public GenericTrafficLight {
public:
enum LightState {
LIGHT_STATE_RED = 0,
LIGHT_STATE_YELLOW = 1,
LIGHT_STATE_CYAN = 2,
LIGHT_STATE_GREEN = 3
};
};
This yields this error: "C2911 (...) cannot be declared or defined in the current scope" in the deriving class's enum definition.
View 5 Replies
View Related
Sep 2, 2014
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.
View 1 Replies
View Related
Aug 19, 2014
I am attempting to implement function pointers and I am having a bit of a problem.
See the code example below; what I want to be able to do is call a function pointer from another pointer.
I'll admit that I may not be explaining this 100% correct but I am trying to implement the code inside the main function below.
class MainObject;
class SecondaryObject;
class SecondaryObject {
public:
[Code]....
View 10 Replies
View Related
Oct 15, 2014
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.
View 1 Replies
View Related
Dec 14, 2013
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));
[Code] ....
View 7 Replies
View Related
Jan 23, 2014
int hash = 0;
char *strings[100];
if((int)strings[i] != 0)
if((int) strings[hash] != 0)
while((int) strings[hash] != 0)
if((int)strings[hash] != 0)
if((int)strings[hash] != 0)
View 12 Replies
View Related
Jul 13, 2013
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 :
bool Tribe::RemoveSurvavior(char *H_Name) {
const char *p;
p=SurpointArr[i]->GetSurvivor_Name();
}
I need to compare if p is same as H_Name.
Mine is do it with for on each element but when i use sizeof it gives me size of char and not real size of the name.
View 6 Replies
View Related
Feb 6, 2012
I want to allocate dynamically classes from a table. There is a different number of class derived from the same father class. Actually I do:
Code:
typedef struct sSysFBList {
TCHAR name[64];
int dim;
SystemFB *fbExec;
}systemFBCalls;
[Code] ....
and so on. NOTE: SystemFB is the virtual father class, all the other are derived from it.
I would like to know if there is the possibility to define the type of the class in the systemFBCalls struct to avoid all these if - else if - else if
Code:
typedef struct sSysFBList {
TCHAR name[64];
int dim;
????? name_of_class;
SystemFB *fbExec;
[Code] .....
View 4 Replies
View Related
Jan 8, 2014
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.
View 4 Replies
View Related