My program deals with accessing attributes of a structure which is defined inside a class. When I use a pointer of type struct to show the structure's attributes, it is throwing a segmentation fault and stops the execution. Though it is printing the structure's attributes fine if I just use a variable of type struct. I tried debugging the code with gdb and it is showing the segmentation fault occurs at line 27 which is poly.params->a = 1;. Why can't we use pointers in this case or am I making a stupid mistake? Here is the sample of the code:
#include <iostream>
using namespace std;
class QuadraticFunc
{
public:
QuadraticFunc(){};
struct Coeff
{
double a;
double b;
double c;
} * params;
void ParamShow(const Coeff *params)
{
cout << "a: " << params->a << endl;
cout << "b: " << params->b << endl;
cout << "c: " << params->c << endl;
}
~QuadraticFunc(){};
};
int main()
{
QuadraticFunc poly;
poly.params->a = 1;
poly.params->b = 2;
poly.params->c = 1;
QuadraticFunc *polyPtr;
polyPtr = &poly;
cout << "The parameters for the first object: " << endl;
polyPtr->ParamShow(poly.params);
}
poly.params->a = 1;
params has not been initialized.
Replace
struct Coeff
{
double a;
double b;
double c;
} * params;
with
struct Coeff
{
double a;
double b;
double c;
} params;
And then replace each params-> with params.
Why can't we use pointers in this case or am I making a stupid mistake?
Yes, you are.
QuadraticFunc poly; // Your constructor leaves poly.params uninitialized
poly.params->a = 1; // Dereferencing uninitialized pointer invokes undefined behavior.
How should I fix this?
The best solution is to avoid the pointer here:
class QuadraticFunc
{
public:
QuadraticFunc(){};
struct Coeff
{
double a;
double b;
double c;
} params;
...
};
int main()
{
QuadraticFunc poly;
poly.params.a = 1;
...
polyPtr->ParamShow(&poly.params);
}
You may have a legitimate reason to have params as a pointer, but you have not yet shown what that reason may be.
Related
I am new to C++ and thus new to smart pointers concept and usage as well. I want to allocate memory dynamically for a struct in a function and then once the receiver is done using that memory. I want the unique (not shared) receiver to safely release the memory. Something like the following:
typedef struct {
int x;
int y;
} myStruct;
myStruct* initMem(void)
{
myStruct* result = new myStruct();
result->x = 12;
result->y = 14;
return result;
}
int main()
{
cout << ">>>>> Main | STARTED <<<<<" << endl;
myStruct* w = initMem();
cout << w->x << endl;
cout << w->y << endl;
delete w;
return 1;
}
Note: Above is just a sample example of what I want to achieve. The structs are far more complicated than that and I have to use dynamic memory allocations only.
I read that playing with raw pointers in C++ for dynamic memory management is not good as C++ has the concept of smart pointers especially for this. Can you please help me in converting the above logic to using smart pointers.
Thanks in advance.
There is no reason to use pointers and dynamically allocated memory. Use automatic storage duration:
myStruct initMem()
{
myStruct result{};
result.x = 12;
result.y = 14;
return result;
}
int main()
{
cout << ">>>>> Main | STARTED <<<<<" << endl;
myStruct w = initMem();
cout << w.x << endl;
cout << w.y << endl;
}
If you have a good reason for using dynamically allocated memory then you must adhere to the RAII principle. Smart pointers in the standard library do just that:
std::unique_ptr<myStruct> initMem(void)
{
auto result = std::make_unique<myStruct>();
result->x = 12;
result->y = 14;
return result;
}
int main()
{
std::cout << ">>>>> Main | STARTED <<<<<" << std::endl;
std::unique_ptr<myStruct> w = initMem();
std::cout << w->x << std::endl;
std::cout << w->y << std::endl;
}
Also in C++ you don't need the typedef. Actually it is idiomatic to not use it:
struct myStruct {
int x;
int y;
};
Use a unique pointer std::unique_ptr. If coding with c++14 and later, then you can benefit from the std::make_unique which creates a myStruct object and wraps it around a unique pointer.
But even if you don't use c++14 or later, then you can simply create the make_unique function yourself and use it accordingly.
template<typename T, typename... Args>
std::unique_ptr<T> make_unique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
So the following example in c++11 would use make_unique instead of std::make_unique.
#include <iostream>
#include <memory>
struct myStruct
{
int x;
int y;
myStruct(int x_, int y_) : x(x_), y(y_)
{
std::cout<< "Calling user-def constructor..." <<std::endl;
}
~myStruct()
{
std::cout<< "Calling default destructor..." <<std::endl;
}
};
int main()
{
std::cout << ">>>>> Main | STARTED <<<<<" << std::endl;
std::unique_ptr<myStruct> ptr = std::make_unique<myStruct>(2,3);
std::cout<< ptr->x << "," << ptr->y <<std::endl;
}
Example online: https://rextester.com/TLIPO27824
This question already has answers here:
Can a local variable's memory be accessed outside its scope?
(20 answers)
Closed 7 years ago.
I have been attempting to create a function getLocation() that utilizes a pointer to return the value of the struct Location declared in the Character class. I was curious as to the problem with my syntax (or my structure). Knowing that the asterisk * should refer to the value, why is it that my function using an ampersand string& Character::getInventory is able to return the value of that particular index (its return does not need to be converted)?
Trying Location& Character::getLocation() {return position; }
when run results in error C2679: binary '<<': no operator found
Nor
Location*
Which cannot be run as there is no conversion.
I read that the following is likely the most proper because it specifies the scope in which the structure resides, but still results in needing and returning a temporary.
Character::Location* const & Character::getLocation() {return &position; }
Any advice or input would be greatly appreciated, thanks in advance.
Below is my main.cpp, which of course will show the hexadecimal address for Location.
#include <iostream>
#include <string>
using std::cerr;
using std::cin;
using std::cout;
using std::endl;
using std::string;
class Character {
private:
string name;
string inventory[4];
public:
struct Location {
int x; int y;
};
Location position;
public:
void Character::setName(string x) { name = x; }
string Character::getName() { return name; }
void Character::setLocation(int x, int y) {
position.x = x; position.y = y;
}
Location* Character::getLocation() {return &position; }
void Character::setInventory(string(&x)[4]) { for (int i = 0; i < 4; ++i) { inventory[i] = x[i]; } }
string& Character::getInventory(int itemNumber) { return inventory[itemNumber]; }
};
void showUser(Character Character);
int main() {
try {
string items[4] = { "Sword", "Shield", "Potion", "Cloak" };
Character CharacterI;
CharacterI.setName("Some Character");
CharacterI.setInventory(items);
CharacterI.setLocation(1, 30);
cout << "\n" << "Retrieving Character Info..." << "\n" << endl;
showUser(CharacterI);
}
catch (std::exception & e) {
cerr << "\nError : " << e.what() << '\n';
}
system("pause");
return 0;
}
void showUser(Character character) {
cout << "Name : " << character.getName() << endl;
cout << "Location : " << character.getLocation() << endl;
for (int i = 0; i < 4; ++i) {
cout << "Inventory " << i + 1 << " : " << character.getInventory(i) << endl;
}
}
Ok, I think I understand the question better now. The reason why getInventory can successfully return a reference while getLocation does not is because getLocation returns a reference to a temporary variable, which is not good. See the link in #NathanOliver's comment for details. Additionally, to paraphrase a previous comment by #Peter Schneider, an * in an expression dereferences a pointer to return a value, while in a declaration it signifies that a variable will be of pointer type. The two usages are more or less opposites of each other. Example:
int* p = new int; //Declares a pointer to int
int x = *p; //Dereferences a pointer and returns an int
What you need to do is create a member variable to hold the Character's location, then set/get from that variable instead of creating temporaries. You did this already for name and inventory, just keep using that same pattern.
Additionally, whenever you use the Location struct outside of the Character class scope, you need to fully-qualify it with Character::Location.
Example:
#include <iostream>
using namespace std;
class Character {
public:
struct Location {
int x;
int y;
};
Location loc;
void SetLocation(int x, int y) {loc.x = x; loc.y = y;}
Location& GetLocation() {return loc;}
};
int main ()
{
Character c;
c.SetLocation(1,42);
Character::Location l = c.GetLocation();
cout << l.x << endl << l.y << endl;
return 0;
}
Output:
1
42
I'm new to C++ and could not figure out how can I define a variable that holds 3 values,
e.g. coordinates hold 2 values, as (x,y).
I tried:
typedef int U_K(int a,int b,int c);
but that doesn't seem to work.
I'd really appreciate a quick simple answer :)
Thanks!
edit:
So i did this :
struct U_K{
float a,b,c;
};
U_K Uk; //this line
is this wrong? because i get "unknown type name U_K" for that line... i first though its because i needed to declare it under the function i am going to use the struct for, but turns out there is the error for both cases.
the shortest way is to use a struct
struct U_K
{
int a,b,c;
};
usage:
U_K tmp;
tmp.a = 0;
tmp.b = 1;
tmp.c = 2;
You can add complexity to that type by adding member function/constructors to make the usage of U_K easier:
struct U_K
{
int a,b,c;
U_K() //default constructor
:a(0)
,b(0)
,c(0)
{}
U_K(int _a_value,int _b_value, int _c_value) //constructor with custom values
:a(_a_value)
,b(_b_value)
,c(_c_value)
{}
};
//usage:
int main()
{
U_K tmp(0,1,2);
std::cout << "a = " << tmp.a << std::endl;//print a
std::cout << "b = " << tmp.b << std::endl;//print b
std::cout << "c = " << tmp.c << std::endl;//print c
}
Alternatively you can use std::tuple to obtain the same result. Using it is different:
std::tuple<int,int,int> t = std::make_tuple(0,1,2);
std::cout << "a = " << std::get<0>(t) << std::endl;//print first member
std::cout << "b = " << std::get<1>(t) << std::endl;//print second member
std::cout << "c = " << std::get<2>(t) << std::endl;//print third member
If you are learning c++ now you should know that the implementation std::tuple is much more complex than a trivial struct and to understand it you need to learn about templates and variadic templates.
struct TypeWith3Ints
{
public:
int a;
int b;
int c;
};
Use std::array<int, 3> which is to be preferred over tuple in this case as a homogenous container can be used.
If you want to typedef:
#include <array>
typedef std::array<int, 3> X;
Use std::tuple, then you don't have to make your own structure. Just write
std::tuple<int, int, int> your_tuple(a,b,c);
std::cout << std::get<1>(your_tuple) << ' '; // b
your_tuple = std::make_tuple(c,d,e);
std::cout << std::get<0>(your_tuple) << ' '; // c
If you want your own name, use alias, like:
typedef std::tuple<int, int, int> your_name;
your_name your_object(a,b,c); //your_tuple
std::cout << std::get<2>(your_tuple) << ' '; // c
If you want, your own structure, and if you want to write object(x,y,z), than you should make constructor or even overload operator (). It's more complicated. I suggest not to do it. Unless that has a deeper meaning. If you really want it, it could look like:
struct coordinate
{
int x,y,z;
coordinate(int a, int b, int c) : x(a), y(b), z(c)
{}
void operator()(int a, int b, int c)
{
x = a;
y = b;
z = c;
}
} my_object(10,20,30);
int main()
{
std::cout<<my_object.x<<' '<<my_object.y<<' '<<my_object.z<<'\n';
my_object(30,40,50);
std::cout<<my_object.x<<' '<<my_object.y<<' '<<my_object.z<<'\n';
return 0;
}
You can also do not make constructor and use {} notation.
I have a Spieler class and a Verein class with a vector of Spieler members.
Now if I change something of the Players like the Staerke(german for strength) by using a function of this class in the player class it does not automatically change the value for this player.
Here is the code:
#include <vector>
#include<iostream>
#include <string>
using namespace std;
class Spieler
{
public:
void setinformation(int a, string b, string c, int d)
{
ID = a;
Vorname = b;
Nachname = c;
Staerke = d;
}
void getinformation()
{
cout << "ID: " << ID << endl;
cout << "Vorname: " << Vorname << endl;
cout << "Nachname: " << Nachname << endl;
cout << "Staerke: " << Staerke << endl << endl;
}
void setStaerke(int x)
{
Staerke = x;
}
int getStaerke()
{
return Staerke;
}
private:
string Vorname, Nachname;
int Staerke, ID;
};
class Verein
{
public:
void setSpielerListe(vector<Spieler> x)
{
Spielerliste = x;
}
vector<Spieler> getSpielerListe()
{
return Spielerliste;
}
string getVereinsName()
{
return VereinsName;
}
int getVereinsID() const
{
return VereinsID;
}
void setVereinsID(int x)
{
VereinsID = x;
}
int getGesamtstaerke()
{
Gesamtstaerke = 0;
vector<Spieler> b;
b = getSpielerListe();
for (size_t i = 0; i < b.size(); i++)
{
Gesamtstaerke = Gesamtstaerke + b[i].getStaerke();
}
return Gesamtstaerke;
}
void Vereinsinformationen()
{
vector<Spieler> list;
int id;
string vereinsname;
int gesamtstaerke;
id = getVereinsID();
vereinsname = getVereinsName();
gesamtstaerke = getGesamtstaerke();
list = getSpielerListe();
cout << "VereinsID: " << id << endl;
cout << "Vereinsname: " << vereinsname << endl;
cout << "Gesamstaerke: " << gesamtstaerke << endl << endl;
cout << "Spieler: " << endl;
for (size_t i = 0; i < list.size(); i++)
list[i].getinformation();
}
private:
vector<Spieler> Spielerliste;
int VereinsID, Gesamtstaerke;
string VereinsName;
};
vector<Spieler> spieler;
int main()
{
Spieler Spieler1;
Spieler1.setinformation(0, "Peter", "Pan", 10);
spieler.emplace_back(Spieler1);
Verein Team1;
Team1.setSpielerListe(spieler);
Spieler1.setStaerke(20);
Team1.Vereinsinformationen();
cin.get();
return 0;
}
I'm really new into c++ and programming so the code might be terrible.
Guess it has to do with pointers, I'm really not into the concept of storing data in c++, try to get it by trial & error; So how to change the Staerke in a way that it is changed in the Teams Playerlist too?
The problem is you are storing full object in the vector and not pointers. When you run this line:
spieler.emplace_back(Spieler1);
a copy of Spieler1 is made and put in the vector. So modifying it in the main will have no effect in the vector. Also not that you are copying the vector when setting in Verein class.
You should use pointer if this is what you are after or better yet have a function to modify strength from Verein class taking its id and new strength as parameters might be a good idea. Something like this:
void setStaerke(int id, int x)
{
vector<Spieler>::iterator it = Spielerliste.begin();
while (it != Spielerliste.end())
{
if ((*it).GetId() == id)
{
(*it).setStaerke(x);
break;
}
}
}
If you have access to C++11, it could be made more elegantly.
Hereby you pass and store a copy from the vector into the object:
Team1.setSpielerListe(spieler);
Therefore changes to the original vector and the contained objects will not affect the member.
Further, I don't have much experience with emplace_back, but the more usual way to append an object to a std::vector would also append a copy:
spieler.push_back(Spieler1);
Therefore changes to the original object would not affect the object you've appended to the container.
Make sure you better understand when objects are copied.
For reference:
http://en.cppreference.com/w/cpp/container/vector/emplace_back
http://en.cppreference.com/w/cpp/container/vector/push_back
How to pass objects to functions in C++?
I defined struct in the global scope, but when I try to use it, I get error: ‘co’ does not name a type, but when I do the same in a function, everything works fine
typedef struct {
int x;
int y;
char t;
} MyStruct;
MyStruct co;
co.x = 1;
co.y = 2;
co.t = 'a'; //compile error
void f() {
MyStruct co;
co.x = 1;
co.y = 2;
co.t = 'a';
cout << co.x << '\t' << co.y << '\t' << co.t << endl;
} //everything appears to work fine, no compile errors
Am I doing something wrong, or structures just cannot be used in global scope?
It's not that you "can't use structures in global scope". There is nothing special here about structures.
You simply cannot write procedural code such as assignments outside of a function body. This is the case with any object:
int x = 0;
x = 5; // ERROR!
int main() {}
Also, that backwards typedef nonsense is so last century (and not required in C++).
If you're trying to initialise your object, do this:
#include <iostream>
struct MyStruct
{
int x;
int y;
char t;
};
MyStruct co = { 1, 2, 'a' };
int main()
{
std::cout << co.x << '\t' << co.y << '\t' << co.t << std::endl;
}
Structure can be "used" as in "you can create a global variable of it".
The remainder of code, co.x = 1; and the rest can appear only inside functions.