C++ :: Memory Usage Of A Program With CLASS And Without CLASS?

Nov 6, 2014

I developing a C++ Program to deal with huge data computation. I am using Pointers to hold the data during computation. Current program i developed without using CLASS.

I would like to ask that will the memory usage will be reduced and computational time will be shortened if I modified my program implementing the CLASS into it ?

View 1 Replies


ADVERTISEMENT

C++ :: Simple Class Usage Compiler Error

May 12, 2013

Full disclosure: this is an exercise from "Sams Teach Yourself C++ in 24 Hours" by Jesse Liberty and Rogers Candenhead. This refers to Chapter 9 (Hour 9 Activity 1)

I created a class called Point, in Point.h

I created a class called Rectangle in Rectangle.h and Rectangle.cpp

If I create an int main() function in Rectangle.cpp (that includes Rectangle.h), I can compile Rectangle.cpp and run the resulting program. Fine.

Question:

I create a separate file called main.cpp. I include Rectangle.h. But now the compiler complains.

Code:
$ g++ main.cpp -o main
/tmp/cc38JIph.o: In function `main':
main.cpp:(.text+0x26): undefined reference to `Rectangle::Rectangle(int, int, int, int)'
main.cpp:(.text+0x32): undefined reference to `Rectangle::getArea() const'
collect2: ld returned 1 exit status If I can create a class in Point.h and use it in Rectangle.h, why can I not just use Rectangle in main.cpp?

And the files, of course:

file: main.cpp
Code:
#include <iostream>
#include "Rectangle.h"
using std::cout;
using std::endl;

[Code] .....

View 3 Replies View Related

C++ :: Pointer Usage To Base Class Object In Vector Int Main Function

Aug 12, 2013

I know what are pointer's and how to use them but there is one point i am not able to understand. Below is the example code

I understand everything in the below code except 1 thing why i am using pointer to base class object in vector int the main() Function?

Code:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
// base class

[Code] ...

Here is the lines of code i want to understand.

Code:
vector<Employee*> employees;
employees.push_back(&emp1);
employees.push_back(&mgr1);

I know if i will not use the pointer base class function "virtual double grossPay" will be called for both base class object and derived class object and when i will use pointer with reference to the object because base class function is virtual it will look for same function in derived class and if available it will execute it.

View 3 Replies View Related

C++ :: Memory Usage And Algorithm Efficiency?

Jan 27, 2013

How can I check how much memory my program is using? I have an assignment which asks me to make sure I'm not exceeding some amount of memory but I don't know how I'd do that on Windows 7.

Also, my program parses through a text file and puts each word in a vector so that I can later go through the vector and "locate" where the nth occurrence of a word is by keeping track of a counter and incrementing the counter each time I see the word in the vector. Is this a vector a bad data structure for this? I'm aiming at keeping the efficiency at less than O(n^2).

View 9 Replies View Related

Visual C++ :: Get Maximum Memory Usage Of App?

Apr 21, 2013

I use Visual C++ to write a native C++ application. How to know the maximum memory it will use during its execution?

View 5 Replies View Related

C++ :: Dynamic Memory Allocation In A Class

Apr 17, 2014

I'm having problems with this code:

#include <iostream>
using namespace std;
class Foo {
public:
Foo( int n );// Constructor
~Foo();// Destructor
int *ptr;
int N;

[Code] ....

I'm using Visual C++ 2008 version. The problem arises at the end, after the sentence 'system("pause")' is reached, which makes me think that the problem happens when calling the destructor. The destructor is called twice, the first time it's called is in the function print. The problem seems to be that the destructor can only be called once.

I know I can avoid this situation by defining the function print like this:

void print ( const Foo &f )
...

but I would like to know if there is some way I can do this keeping the definition that I've provided.

View 2 Replies View Related

C++ :: How Class Objects Stored In Memory

Aug 23, 2014

For example when I have:

Class A{
B objectB;
};

Now when I instantiate the object of class A like:

main(){
A objectA;//option 1
A* pObjectA = new A();// option2
}

How is objectB stored in memory (stack heap etc.) for both options??

View 1 Replies View Related

C/C++ :: Allocating Memory For 2D Array In Other Class?

Jun 10, 2014

I'm making some multi-threaded program, but thats not my problem as i've done that already. I have a class with user-functions containing a structure which then contains a two dimensional array for each user with 25 elements. So I dont want to limit the user and make the array for example with just 10 rows, but allocate the needed memory to match the amount of 'users' a potential user of my program would want. The problem is, that i know how i should allocate it using 'new int' but it just doesnt work ! It gives an error:

Error: no operator "=" matches these operands

UserStuff.h:
struct userDataStruct {
bool* isAdmin;

[Code]...

Then, in some completely other class function inside the file mentioned above: (I know i could do a function in CUsers class which could allocate the memory, but I have this function which is used for some other things and it already has the amount of max users

void OtherClass::somefunction(maxusers)
{
// This gives an error: Error: no operator "=" matches these operands
curUsers->uData.userNumbers = new int*[maxusers]; //maxusers is the int variable of max users specified by the client
// However this doesn't
for( int i = 0 ; i < maxusers ; i++ )
curUsers->uData.userNumbers[i] = new int[25]; // 25 columns, this doesnt give any error
}

I'm not really sure what I'm doing wrong. Doing this in some function from CUsers class works (without curUsers-> or with, doesn't give any error) but doing it from some other class's function doesnt.

View 11 Replies View Related

C++ :: Memory Address Of Class Member Variables?

Jun 22, 2013

Suppose I have two classes, MyClassX and MyClassY, each with two member variables, as defined below. I create an object instance of each class, and then create a pointer to each member variable for each object:

Code:
class MyClassX
{
public:
int a;
double b;
MyClassX(int _a, double _b)

[code]....

After converting the hexadecimal to decimal, it appears that with MyClassX, pxb is 8 bytes from pxa, whereas for MyClassY, pya is only 4 bytes from pyb. This makes sense for MyClassY, because the first member variable to be stored is an int, and so will occupy 4 bytes. However, why should this be any different for MyClassX, which also has an int as the first member variable, so shouldn't this also occupy 4bytes?

The reason I have come across this problem is that I am looking into streaming objects to memory and then loading them again. (I know boost can do this, but I am trying it out myself from scratch.) Therefore, this is causing an issue, because I cannot just assume that the size of memory occupied by an object is just the sum of the sizes of its member variables. MyClassX is 8 bytes larger than MyClassY, even though the intuition is that it should only occupy 4 bytes more due to a double being replaced by an int.

View 4 Replies View Related

C++ :: Variable Inside A Class - How To Manage Memory Allocation

Jul 30, 2014

recently I developed a class header only in C++ to deal with byte arrays. Since I developed programs in other languages that had some libraries to dial with byte arrays, I developed one that the syntax is very similar to other languages.

Since I'm not very familiar with memory management, I was concerned about somethings I've read about (ex: memory fragmentation).

Here is a very simple example of my practice:

class ByteArray {
public:
ByteArray(size_t size) {
buffer = (int8_t*)malloc(size);
}

[Code].....

The class is intended to be used as part of comunication protocol in a webserver, byte arrays are created and destroyed a lot. Should I use pools? Is there a better practice? Am I doing everything wrong (laugh)?

For those who wants to see the entire class: [URL]

View 9 Replies View Related

C++ :: Class Memory Allocation - Store Number Of Elements In Table

Mar 31, 2013

Say I have a class with a few member functions, and only two data members: an int* Table; and an int Size;, to store the number of elements in Table.

I'm using a copy constructor that takes in two parameters: int* table, int size. In this case, is the address that table points to the same address as the object that table is part of? And furthermore, is it possible to say table.Size? I want to compare the passed array's size to the passed size.

View 2 Replies View Related

C++ :: Vector Of Int Class - How To Delete Pointer X In Destructor To Free Memory

Feb 25, 2015

An attempt to create a class which is basically a mimic of vector<int> i don't seem to know how to delete pointer x in a destructor to free memory, also on pushback and pushfront methods, i can't free y when i implement delete[] y; y=NULL; i get some NULL out put when cout 'ing the object in main, why is that happening and how do i free memory y.

#include<iostream>
using namespace std;
class vectorOfint{
int* x;
int size;
public:
vectorOfint();

[Code] .....

View 6 Replies View Related

C++ :: Instance Of Class A Vs Class B Derived From Class A

Jul 3, 2014

There are two ways to access the members of class A inside class B:

1) Making an instance of class A in class B
2) Deriving class B from class A

So what is the basic difference in both ways as we can do same kind of work with both ways?

View 1 Replies View Related

C++ :: How To Get Object Of Original Class From Function Of Other Class Where Other Class Object Is Member Of Original Class

Jan 21, 2013

The case is like

class B{
public:
somedata;
somefunction();
}
class A{
public:
data;
function();
}

in somefunction i want a pointer to current object of class A m new to c++

View 2 Replies View Related

C++ :: Calling Derived Class Functions In A Function With Parameter Of Base Class

Mar 30, 2013

Say I have 3 classes:

class Player {
public:
virtual func1();

[code]....

Say in my main class, I have a function fight(Player p1, Player p2) and I would like to do something like this in the fight function, given that p1 is the human and p2 is the computer:

//function fight()
fight(Player p1, Player p2) {
p1.func2();
}
//using function fight()
fight(human, computer);

When I compile the program, I got this: error: ‘class Player’ has no member named 'func2()' What can I do to allow p1 to call func2 inside fight()? I'm not allowed to use pointers as the parameter for fight() and have to use the signature fight(Player p1, Player p2).

View 6 Replies View Related

C++ :: Size Of Derived Class With Overriding Virtual Functions From Base Class?

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

C++ :: Transfer Values Set In Privates Of Base Class By Object Of One Derived Class To Another

Apr 26, 2014

I have my main.cpp like this:

#include <iostream>
#include "curve1.h"
#include "curve2.h"
using namespace std;
int main() {
Curve1 curve1Obj;
Curve2 curve2Obj;

[Code]...

Base class Score has two derived classes Curve1 and Curve2. There are two curve() functions, one is in Curve1 and other in Curve2 classes. getSize() returns the value of iSize.

My base class header score.h looks like this:

#ifndef SCORE_H
#define SCORE_H
class Score {
private:
int *ipScore;
float fAverage;
int iSize;

[Code]...

You can see that I have used curve1Obj to enter scores, calculate average and output. So if I call getSize() function with cuve1Obj, it gives the right size that I took from user in enterScores() function. Also the result is same if I call getSize() in score.cpp definition file in any of the functions (obviously).
.....

The problem is when I call curve() function of Curve2 class in main (line 23) with the object curve2Obj, it creates a new set of ipScore, fAverage and iSize (i think?) with garbage values. So when I call getSize() in curve() definition in curve2.cpp, it outputs the garbage. .....

How can I cause it to return the old values that are set in curve1.cpp?

Here is my curve2.cpp

#include <iostream>
#include "curve2.h"
using namespace std;
void Curve2::curve() {
cout << "getSize() returns: " << getSize() << endl; // out comes the garbage
}

Can I use a function to simply put values from old to new variables? If yes then how?

View 3 Replies View Related

C++ :: How To Initialize Static Member Of Class With Template And Type Of Nested Class

Oct 7, 2014

How to initialize a static member of a class with template, which type is related to a nested class?

This code works (without nested class):

#include<iostream>
using namespace std;
struct B{
B(){cout<<"here"<<endl;}
};
template<typename Z>

[Code] ,....

View 1 Replies View Related

Visual C++ :: Reach Top Class Inherits From Goal Class - Linker Error

Dec 10, 2012

Linker error.

First off the error

Code:
Error1error LNK2019: unresolved external symbol "public: __thiscall ReachTop<class Character>::ReachTop<class Character>(class Character *)" (??0?$ReachTop@VCharacter@@@@QAE@PAVCharacter@@@Z) referenced in function "void __cdecl `dynamic initializer for 'gReachTop''(void)" (??__EgReachTop@@YAXXZ)Main.objDecisionTest

Reach Top class inherits from Goal Class

Goal Class

Code:
#ifndef _GOAL_H
#define _GOAL_H
#include "Action.h"
#include <list>
template <class T>
class Goal

[Code] ....

Code to create

Code:
Character* gCharacter = new Character(1, gWorld);
Goal<Character>* gReachTop = new ReachTop<Character>(gCharacter);

I can provide the character class and its inheritance aswell if you like.

View 4 Replies View Related

C++ :: Deleting Array Of Derived Class Objects Through Base Class Pointer

Mar 21, 2015

In this book, item 3 is about never treat arrays polymorphically. In the latter part of this item, the author talks about the result of deleting an array of derived class objects through a base class pointer is undefined. What does it mean? I have an example here,

Code:
class B
{
public:
B():_y(1){}
virtual ~B() {
cout<<"~B()"<<endl;

[Code] ....

This sample code does exactly what I want. So does the author mean the way I did is undefined?

View 1 Replies View Related

C++ :: Creating Array Of Pointers To Base Class To Point To Derived Class Objects Dynamically

Jan 16, 2013

Please consider the following code :

#include <iostream>
using namespace std;
class superclass;
class subclass1;
class subclass2;

[Code] ....

As you can see I want to create a dynamically allocated storage of references to a parent class each of which can then point to a child class, how ever I do not know how to extract the child class out again from that array so i may access its variable b.

View 2 Replies View Related

C++ :: Initializing Inner-objects Of Base Class From Driven-class Constructor

Jan 6, 2015

Let's say I have a Car object , and it contains inner Engine object.

Code:
struct Car{
Engine mEngine;
};

In order to initialize the engine object NOT by the default constructor (if it has any) , we use initialization semantics:

Code:
Car::Car:
mEngin(arg1,arg2,...)
{
other stuff here
}

Now it gets tricky: Let's say a Car objects has 10 inner objects, each object has about 5 variables in it . Car is a base class for , e.g. , Toyota class. you don't want the Car class to have a constructor with 50 arguments. Can the inner objects of Car be initialized from the base class , e.g. Toyota?

Code:
class Toyota:
Car(...),
mEngine(...),
mGear(..)
{
...
};

The other options are:
1) like said , create a Car constructor which gets 50 arguments, then initialize Car as whole from Toyota - the code becomes less readable and less intuitive
2) Car constructor which get built-objects as arguments and initialize the inner objects with copy constructor . the code gets more readable but then you create many excess objects .

View 5 Replies View Related

C++ :: Derived Class Not Overwriting Base Class Function - Using Vectors

Feb 4, 2014

So I have a base class, lets call it base. In base I have a virtual function called update(), update just couts "base" then I have a class derived from base called derived;

it has a function called update(), update just couts "derived" then I create a vector called Vec it's initialised like this:

std::vector<base> Vec;

then I add an element into it like this

Derived DerElement;
Vec.push_back(DerElement);

then when I type:

for (int i=0; i<Vec.size(); i++) {
Vec.at(i).Update();
}

It outputs:

Derived DerElement2;
DerElement2.Update();

and it outputs this:

#include <iostream>
#include <vector>
class Base {
public:
virtual void Update() {

[Code] .....

and this is it's output:

Base
Derived
Press any key to continue . . .

View 1 Replies View Related

C++ :: Friendship From Derived Class Method To Base Class Members

Jul 15, 2014

I would like to know if there's a way to make a method from a derived class a friend of its base class. Something like:

class Derived;
class Base {
int i, j;
friend void Derived::f();
protected:
Base();

[Code] ......

View 3 Replies View Related

C++ :: Variable Belonging To Base Class - Tell Compiler Consider This To Be Derived Class?

Oct 12, 2013

I have an example where I have a variable belonging to a base class, but I would like to tell the compiler that it actually belongs to a derived class. How can I do this?

// base class: R0
// derived class: R1
// see function SetR1 for the problem
class R0 {
public:
int a;

[Code] .....

View 5 Replies View Related

C++ :: Define A Class That Only Inherits From Parent Class And Takes One Argument

Jan 5, 2015

In the project I'm currently working on I define a class that only inherits from a parent class and takes one argument.

Does this class need to be defined in the header or source file? I read different answers around the internet.

Or is it better to always split definition and logica, even for something like an operator?

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved