class function gives garbage value - c++

I am novice in c++. I have made class named Cache and made an object L1Cache using the constructor. I am passing parameters to the constructor and printing calculated data correctly. But when I am calling a different function within the same class and same object, data that I generated in constructor gives garbage value. Here, I would like to mention that my all functions and variables are public.
My code looks something like this:
main.cc:
#include <stdio.h>
#include <fstream>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "cache.h"
main()
{
unsigned long long Address= 5555555555;
unsigned long a=5,b=7;
Cache L1Cache(a, b);
L1Cache.Calculate( Address);
}
cache.cc:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include "cache.h"
using namespace std;
Cache::Cache(unsigned long c,unsigned long d)
{
e= c+d;
printf("Value Of e:%lu",e);
}
void Cache::Calculate(unsigned long long A)
{
printf("Value of e:%lu",e);
}
cache.h:
class Cache
{
public:
unsigned long e;
Cache(unsigned long c,unsigned long d)
void Calculate(unsigned long long A);
}
output:
Value of e: =12;
Value of e: = garbage

There's a semicolon missing in and after your class declaration. And main should be int. After fixing that the code runs. Have you actually run the code you think you ran?

Related

c++ unable to read memory with custom class when calling a function

I am having problems running functions within a custom class. Here is my code:
ROBOTSTRUCTURE.H
#pragma once
#include "math.h"
#include <fstream>
#include <string>
#include <thread>
#include <chrono>
#include <ctime>
#include <vector>
#include <sstream>
#include <fstream>
using namespace std;
using namespace std::chrono;
class RobotStructure
{
bool faceTrackingEnabled;
public:
RobotStructure();
~RobotStructure();
bool testFunction(string input);
ROBOTSTRUCTURE.CPP
#include "RobotStructure.h"
RobotStructure::RobotStructure()
{
faceTrackingEnabled = true;
}
RobotStructure::~RobotStructure()
{
}
bool RobotStructure::testFunction(string input)
{
cout << input << endl; //THIS DOES NOT WORK, When using debugger shows that the entire class "Robot Structure" as unable to read memory
return true;
}
MAIN
#include <Windows.h>
#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>
#include <thread>
#include <string>
#include <sstream>
#include <vector>
#include <math.h>
#include <fstream>
#include "RobotStructure.h"
using namespace std;
int main()
{
RobotStructure *mybot= new RobotStructure();
mybot->testFunction("test");
return 0;
}
If a set a breakpoint in main, my class is initialized correctly and everything is fine. As soon as a call the "test function" the class falls out of memory within the test function.
When it goes back to main just before return 0; the class is also out of memory. As if the pointer is deleted somehow. Can someone please explain what is happening and what did I do that is wrong?
You declared the function as bool testFunction(string input) but the function is not returning anything. This leads to undefined behavior in C++.
In addition your example wouldn't compile (you declared an argument input but you are using intput).
Your testFunction does not use any class members. If you compile this code with optimization enabled ("Release"-Configuration in VisualStudio, -O2 on gcc), the compiler might render some variables (like this) "undebuggable".

c++ Class Members Returning Odd values

I will start this out by saying c++ is my first programming language and I am a beginner at best. And I am sure this has some sort of obvious answer.
But for some reason this simple program with a single custom class is returning very odd values from the Get functions that are calling the private members of the one custom class.
The program is separated into three file as follows.
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include "Shapes.h"
using namespace std;
int main ()
{
double test=20.0;
Cube D(test);
cout<< D.GetSA()<<endl<<D.GetSide();
return 0;
}
then as header files for the one Classes called Shapes.h
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
using namespace std;
class Cube
{
public:
Cube();
Cube(double);
double GetSA() const;
double GetSide() const;
private:
double SA;
double V;
double Side;
};
And another file Called Shapes.cpp that contains the Constructor.
#include <iostream>
#include <cmath>
#include <string>
#include <cctype>
#include "Shapes.h"
Cube::Cube()
{
V=0.0;
SA=0.0;
Side=0.0
}
Cube::Cube(double Side2)
{
Side=Side2;
}
double Cube::GetSA() const
{
return SA;
}
double Cube::GetSide() const
{
return Side;
}
for some reason when this program is run it returns a value of 6.95293e-310 for the GetSA accessor function and returns a value of 200 for the side function.
Any ideas on why this is happening and how to fix it?
Try invoking the default constructor. It seems to initialize the data members correctly. Your parameterized constructor is only initializing the Side data member, and not any other data members.

Invalid type of argument using pointer to struct array

I have a problem with the next code, I get the error: invalid type argument of unary '*' (have 'int'). If I write int *content that allows the code to run, but I have to write int=content and change the code *((ptab->content)+pC1+17), I tried but I can't fix the error.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <Windows.h>
using namespace std;
struct box{
int content;
};
struct box *ptab;
int pC1=5;
int main (){
ptab=new struct box[64];
if (*((ptab->content)+pC1+17)==0) {
pC1=pC1+17;
}
cout<<pC1<<endl;
}
I have to pass from pointers to poninters to struct arrays, this code is an example, because the original code has 23000 lines.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <Windows.h>
using namespace std;
int *box;
int pC1=5;
int main (){
box=new int[64];
if (*(box+pC1+17)==0){
pC1=pC1+17;
}
cout<<pC1<<endl;
}
With *((ptab->content)+pC1+17), an easier way to say it is ptab[pC1+17].content [the latter compiles and produces 22 as output]. Did you mean that or ptab->content + pC1 + 17 [which produces 5]?

Query over initialisation of global variables

Not able to figure out why this code gives error.
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
unsigned long long A[102];
A[0]=2;
int main()
{
cout<<"OK";
}
Error while compiling:
prog.cpp:6:1: error: ‘A’ does not name a type
But this works fine.
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
unsigned long long A[102];
int main()
{
A[0]=2;
cout<<"OK";
}
What could be the reason ?
This
A[0]=2;
is not an initialization, it is an assignment to the first element of A. You cannot do that outside of a function.
This is initialization, and is legal:
#include<iostream>
unsigned long long A[102] = {2};
int main()
{
std::cout<<"OK\n";
}
It will set the first element to 2 and all the rest to 0.

pthread_create() "class not declared in the scope?

I have a class called pos... I am trying to poll a method from this class. I used pthread_create(pthread_t thread, pos::Pirnt_data,this);
I get an error that pos is not declared in the scope... I included the h file of pos but I don't understand. I think I am using a wrong format can somebody help me
#include "position.h"
#include "pthread.h"
#include "pos.h"
void position::tick(schedflags_t flags)
{
if(pthread_create(&thread,NULL,pos::Print_data,this)!=0) {
stringstream bad;
bad << "OPIMex: Could not create listener thread: "
}
this class position has method tick that runs every 1 second with the data. I am trying to poll a method Print data from the class pos but it gave me that error any ideas why ?
this is class pos.h
#ifndef POS_H_
#define POS_H_
#include <math.h>
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <gps.h>
#include <string.h>
#include <pthread.h>
#include <string>
#include <vector>
#include <strings.h>
#include <math.h>
using namespace std;
namespace herpderp {
namespace modules {
int UBX_step =0;
long data;
int UBX_class;
int UBX_id=0;
int UBX_payload_length_hi;
int UBX_payload_length_lo;
int UBX_payload_counter =0;
int ck_a;
int ck_b;
int GPS_timer;
int fd;
unsigned int UBX_buffer[35];
int payload_data;
long lat=0;
long lon=0;
long alt_MSL=0;
long iTOW=0;
long alt=0;
unsigned long LastMS;
int UBX_Read;
vector <float> v;
fstream myfile;
int Open_port(void);
int read_tofile();
long join_4_bytes( unsigned int Buffer[]);
void parse_ubx_gps(void);
void checksum(char ubx_data);
void Print_data();
int push_data_into_vector();
int decode_gps();
int Configure_gps();
int test();
int Close_NEMA();
int Open_UBX();
}
}
#endif //POS_H_
pthread_kill is not on pthread.. It is on signal.h
#include <signal.h>
1) you can provide some code snippet/additional information to help you better.
2) If you are getting linkage error, check if you have linked with -lpthread library.
Form the pos.h it seems that there is no class called pos and you just need to call the function name:
if(pthread_create(&thread,NULL,Print_data,this)!=0) {