I have the following code:
main.cpp
#include "Test.h"
int main() {
Create();
}
Test.h
#pragma once
#include <iostream>
#include "Function.h"
class Test {
public:
Test();
};
extern Test* g_pTest;
inline void Create() {
g_pTest = new Test;
std::cout << "On Test.h: " << std::endl;
PrintAddr();
}
Test.cpp
#include "Test.h"
Test* g_pTest = nullptr;
Test::Test() {
std::cout << "On Test.cpp:" << std::endl;
PrintAddr();
}
Function.h
#pragma once
void PrintAddr();
Function.cpp
#include "Function.h"
#include "Test.h"
void PrintAddr() {
std::cout << "g_pTest address is " << g_pTest << std::endl;
}
When I run it, I get the following output:
On Test.cpp:
g_pTest address is 0000000000000000
On Test.h:
g_pTest address is 000002008A5EAE40
I thought that a extern variable was supposed to have the same value anywhere on the code, so why doesn't it? I've tried to run the same code, but instead of the Test class, I just have a function:
Test.h
#pragma once
#include <iostream>
#include "Function.h"
void Test();
extern int* g_pTest;
inline void Create() {
g_pTest = new int;
Test();
std::cout << "On Test.h: " << std::endl;
PrintAddr();
}
Test.cpp
#include "Test.h"
int* g_pTest = nullptr;
void Test() {
std::cout << "On Test.cpp:" << std::endl;
PrintAddr();
}
And it somehow works if it is done this way, I don't understand the logic...
What am I doing wrong here? What can I do in order to use a class and have access to the same extern variable in any code file?
int main() {
Create();
}
main calls Create(), which does this:
g_pTest = new Test;
Test gets newed first. g_pTest gets assigned after Test's constructor finishes and the object is constructed.
Test's constructor calls PrintAddr which prints the value of g_pTest because it is still nullptr:
Test* g_pTest = nullptr;
This remains the case until after Test finishes constructing.
Only after the constructor wraps up its business the pointer to the new object gets assigned to g_pTest, and then the 2nd call to printAddr (from Create) prints the value of the pointer, which is now pointing to the new object.
Related
I'm currently learning c++ for a week and here's my problem:
run.cpp
#include <iostream>
#include "Abc.h"
int main(){
int a;
std::cout << "Enter a : ";
std::cin >> a;
// Object Initialization
Abc AbcObj();
}
the header, Abc.h :
#ifndef ABC_H
#define ABC_H
class Abc
{
public:
Abc();
protected:
private:
};
#endif // ABC_H
and finally my cpp file for implementation, Abc.cpp:
#include "Abc.h"
#include <iostream>
Abc::Abc()
{
std::cout << std::endl << "Object created ";
}
Why don't I get output on my console? I'm expecting "object created" should be on the console. These files are in the same directory.
You error doesn't come up because you've used different files, so I have used one in this example
struct Foo
{
int a;
Foo()
{
std::cout << "Constructor called!";
}
};
int main()
{
Foo obj();
}
Why don't you see the message? You can read this thread
The problem here is, Foo obj() is taken as a function declaration. To fix this you need to remove the ()
int main()
{
Foo obj;
}
Constructor called!
I am currently learning C++ and found that there are at least two ways using variables defined in other files. However I do not understand really, when to use what method.
For example:
I have writte in "h1.h":
extern int k;
and "a2.cpp" writes:
#include "a2.h"
#include "h1.h"
int k = 42;
int k2 = 43;
Then I can reference k in my main.cpp with:
#include "main.h"
#include "a1.h"
#include "h1.h"
#include <iostream>
Main::Main() {}
int main() {
std::cout << k << std::endl;
}
However if I want to use k2 in main.cpp I could simply write a getter/setter method, thereby I would avoid having to use extern in a common included header file.
I would like to know: What are other ways to access variables from other files? When do you use which method (and why )?
You expose k as a function, or not at all, not as a variable.
"h1.h":
int k();
void k2(int &);
"h1.cpp":
int k() { return 42; }
void k2(int & value) { value = 43; }
"main.cpp"
#include "h1.h"
#include <iostream>
int main () {
std::cout << k() << std::endl;
int any_name;
k2(any_name);
std::cout << any_name << std::endl;
}
I have a small solution in visual studio 2012. The solution consists of two projects (Scanner and TestApp), Scanner is a dll and TestApp is a small application using the dll.
I would like a function in the dll to run in a thread and to report its result back via a queue to the TestApp.
To test this I wrote a minimal application, but depending on how I launch the thread I get different results and I would like to understand why.
The Scanner.h file looks like this:
#pragma once
#include <iostream>
#include <string>
#include <stdint.h>
#include <atomic>
#include <future>
#include <thread>
#include "version.h"
#include "threadsafe_queue.h"
#include "capture_data.h"
#include "process_data.h"
#ifdef SCANNER_EXPORTS
#define SCANNER_API __declspec(dllexport)
#else
#define SCANNER_API __declspec(dllimport)
#endif
class Scanner
{
public:
static SCANNER_API void run();
static SCANNER_API void stop();
};
Scanner.cpp:
#include "stdafx.h"
#include "Scanner.h"
std::vector<std::future<int>> my_futures;
void Scanner::run()
{
CaptureData capture_data(1234);
auto t = std::async(std::launch::async, &CaptureData::get_data, capture_data);
my_futures.push_back(std::move(t));
}
void Scanner::stop()
{
for(int n=0; n<my_futures.size(); n++) {
auto e = std::move(my_futures.back());
e.get();
my_futures.pop_back();
}
}
The class CaptureData is defined in capture_data.h and capture_data.cpp.
capture_data.h:
#pragma once
#include <atomic>
#include <thread>
#include "iq_data.h"
#include "threadsafe_queue.h"
class CaptureData
{
public:
CaptureData(double freq_start);
void configure();
void get_data();
private:
double m_test;
};
capture_data.cpp
#include "stdafx.h"
#include "capture_data.h"
#include "Scanner.h"
ThreadsafeQueue<int> g_queue_1;
SCANNER_API ThreadsafeQueue<int> g_queue_2;
CaptureData::CaptureData(double test)
: m_test(test) {}
void CaptureData::get_data()
{
cout << "1: " << m_test << std::endl;
Sleep(5000);
cout << "2: " << m_test << std::endl;
g_queue_2.push(3);
cout << "Capture has now pushed data" << std::endl;
}
And finally the TestApp.cpp:
#include "stdafx.h"
#include "tchar.h"
#include <stdint.h>
#include <string>
#include "Scanner.h"
SCANNER_API extern ThreadsafeQueue<int> g_queue_2;
int _tmain(int argc, _TCHAR* argv[])
{
Scanner scanner;
scanner.run();
cout << "TestApp waiting for data..." << std::endl;
int data;
g_queue_2.wait_and_pop(data);
cout << "TestApp got data: " << data << std::endl;
scanner.stop();
return 0;
}
In Scanner.cpp I have tried to launch the thread in two different ways, the first way:
auto t = std::async(std::launch::async, &CaptureData::get_data, capture_data);
Second way is with a reference to the object "capture_data":
auto t = std::async(std::launch::async, &CaptureData::get_data, &capture_data);
The first way seems to work as I intended the application to work and I get the following printouts in my terminal:
TestApp waiting for data...
1: 1234
2: 1234
Capture has now pushed data
TestApp got data: 3
Press any key to continue...
If I use the second way I get:
TestApp waiting for data...
1: 6.95166e-310
2: 6.95166e-310
Capture has now pushed data
TestApp got data: 3
Press any key to continue...
So, what I do not understand is why the variable "m_test" get messed up in the second case.
I would very much appreciate if anyone could shed a light on this.
/M
In the following code:
void Scanner::run()
{
CaptureData capture_data(1234);
auto t = std::async(std::launch::async, &CaptureData::get_data, capture_data);
my_futures.push_back(std::move(t));
}
capture_data is a local variable that goes out of scope and gets destroyed when the function returns. If you pass a pointer to that variable into async that pointer becomes a dangling pointer causing undefined behaviour. That does not happen if you pass it by value, as you do in the above snippet.
You are trying to pass a pointer to a stack allocated object. This object is destructed at the end of the Scanner::run() method. Thus, the pointer is now pointing to invalid memory when the async function runs.
The first method works, because the capture_data variable is move constructed when it is passed to the function, therefore it still retains it's structure.
I recommend using lambda functions than passing the raw member function:
void Scanner::run()
{
CaptureData capture_data(1234);
auto t = std::async(std::launch::async, [capture=std::move(capture_data)]() { capture.get_data(); });
my_futures.emplace_back(t);
}
Even better is to construct the object inside the lambda function:
void Scanner::run()
{
auto t = std::async(std::launch::async, []() {
CaptureData capture_data(1234);
capture_data.get_data();
});
my_futures.emplace_back(t);
}
I don't know exactly why this is happening but it seems that the code is "losing" the value of an attribute. I got a class defined as (in Foo.h):
#ifndef FOO_H
#define FOO_H
using namespace std;
#include <string>
class Foo{
public:
Foo(short int id);
void Foo::DoSomething(std::string someMsg);
private:
short int id;
};
#endif /* FOO_H */
in the class implementation (Foo.cpp):
#include "Foo.h"
#include <iostream>
Foo::Foo(short int id) {
this->id = id;
cout << "value now: " << this->id << "\n"; // I print to be sure it was set correctly and it prints the right value
}
void Foo::DoSomething(std::string someMsg) {
cout << "Foo number: " << std::to_string(this->id) << someMsg;
// when it runs this code called by another class, it always prints 0 to this->id for all objects instantiated
}
Then, I got another class, Bar. In Bar.h I have:
#ifndef BAR_H
#define BAR_H
#include <iostream>
using namespace std;
#include <list>
#include "Foo.h"
class Bar {
public:
Bar();
void setValor(std::string valor);
void notifyAllFoos();
void Bar::registerFoo(Foo *h);
private:
std::list<Foo> foos;
};
#endif /* BAR_H */
In Bar.cpp I have:
#include "Bar.h"
void Bar::setValor(std::string valor){
// do more stuff
this->notifyAllFoos();
}
void Bar::registerFoo(Foo *h){
this->foos.push_back(*h);
}
void Bar::notifyAllFoos(){
for(std::list<Foo>::iterator it=this->foos.begin() ; it!=this->foos.end() ; it++){
it->DoSomething("myMsg");
}
}
Finally, in main.cpp:
#include <cstdlib>
#include "Bar.h"
#include "Foo.h"
using namespace std;
int main(int argc, char** argv) {
Bar *b = new Bar();
b->registerFoo(new Foo(1));
b->registerFoo(new Foo(2));
b->registerFoo(new Foo(3));
b->setValor("Value");
delete b;
return 0;
}
Basically, Bar must notify all Foos of it's list that an update of an Value occured and print a Msg. To identify each Foo I put this id but it keeps printing 0 for all.
Why is this happening? It must be something very simple I guess but I'm not used to c++. Thanks in advance.
That:
std::list<Foo> foos;
will store copies of objects from class Foo.
You need to store pointers, like this:
std::list<Foo*> foos;
PS: Never forget to delete for every new, when you are done.
I did take your code and put it into one file, added includes and using at the top and commented out declaration of Bar constructor.
#include <string>
#include <iostream>
using namespace std;
class Foo{
public:
Foo(short int id);
void Foo::DoSomething(std::string someMsg);
private:
short int id;
};
Foo::Foo(short int id) {
this->id = id;
cout << "value now: " << this->id << "\n"; // I print to be sure it was set correctly and it prints the right value
}
void Foo::DoSomething(std::string someMsg) {
cout << "Foo number: " << std::to_string(this->id) << someMsg;
// when it runs this code called by another class, it always prints 0 to this->id for all objects instantiated
}
#include <list>
class Bar {
public:
//Bar();
void setValor(std::string valor);
void notifyAllFoos();
void Bar::registerFoo(Foo *h);
private:
std::list<Foo> foos;
};
void Bar::setValor(std::string valor){
// do more stuff
this->notifyAllFoos();
}
void Bar::registerFoo(Foo *h){
this->foos.push_back(*h);
}
void Bar::notifyAllFoos(){
for(std::list<Foo>::iterator it=this->foos.begin() ; it!=this->foos.end() ; it++){
it->DoSomething("myMsg");
}
}
int main () {
Bar *b = new Bar();
b->registerFoo(new Foo(1));
b->registerFoo(new Foo(2));
b->registerFoo(new Foo(3));
b->setValor("Value");
}
It gives me following output
value now: 1
value now: 2
value now: 3
Foo number: 1myMsgFoo number: 2myMsgFoo number: 3myMsg
Is it what you are looking for?
I have a weird problem, this is my code :
test.h
#ifndef _test_
#define _test_
#include <iostream>
#include <string>
class Test {
public:
Test();
~Test();
void addName(std::string _Name);
private:
std::string Name;
};
#endif // _test_
test.cpp
#include "test.h"
Test::Test() {}
Test::~Test() {}
void Test::addName(std::string _Name) {
std::cout << _Name << std::endl;
Name = _Name;
std::cout << _Name << std::endl;
}
main.cpp
#include "test.h"
int main(int argc, char* argv[]) {
Test* project;
project->addName("abc");
return 0;
}
Results :
abc
The program has unexpectedly finished.
That's because you have a pointer to a Test object, but it doesn't actually point anywhere. That leads to undefined behavior when you try to dereference the pointer.
Declare the object as an actual object, not a pointer:
Test project;
project.addName("abc");
The pointer project is default-initialized and has indeterminate value, so dereferencing it has a big chance to cause abnromal termination of the program.
Try creating an object and assigning it before dereferencing like this:
#include "test.h"
int main(int argc, char* argv[]) {
Test* project = new Test;
project->addName("abc");
delete project;
return 0;
}