|
if i'm not mistaken ctrl c(yes the copy shortcut) should break an infinite loop
|
|
|
|
|
How can i define the enter key to stop a working loop in my program ?
for advance ..
2-programs here which enter key defined both with different ways !
==============
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int x=1;
r:
printf("Allah %d\n", x);
x++;
while ( getch() != '\r')
{
goto r;
}
return 0;
}
and the other one ..
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int nm = 0, ch = 0, wr = 1,i=0;
char x;
while ((x=getchar()) != '\n')
{
if (isdigit(x))
{
nm++;
}
else if (isalpha(x))
{
ch++;
}
else if (x == ' ')
{
wr++;
}
i++;
}
printf(" \n");
printf("Numbers entered = %d , Characters entered = %d , Words entered = %d", nm, ch, wr);
printf(" \n");
system("pause");
return 0;
}
The first one enter was defined as '\r' while the second was '\n' ! which is the right way ?!
|
|
|
|
|
What happens when you try it?
|
|
|
|
|
Both programs debug well ! :/
|
|
|
|
|
I have the following code and when running it stays blocked on the join(). So, is it possible to call std::thread::join() in the destructor of a global variable?
void MyTimerFunction();
class MyGlobal
{
public:
std::atomic<bool> m_Flag;
std::thread m_GlobalThread;
MyGlobal() {
m_Flag = true;
m_GlobalThread = std::thread(MyTimerFunction);
}
~MyGlobal()
{
m_Flag = false;
if (m_GlobalThread.joinable())
m_GlobalThread.join();
std::cout << "MyGlobal destroyed" << std::endl;
}
};
MyGlobal Test;
void MyTimerFunction()
{
do
{
std::this_thread::sleep_for(std::chrono::seconds{10});
if (!Test.m_Flag)
break;
std::cout << "New tick" << std::endl;
} while (true);
std::cout << "Exit MyTimerFunction" << std::endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Main thread sleeping..." << std::endl;
std::this_thread::sleep_for(std::chrono::seconds{15});
std::cout << "Main thread awaking..." << std::endl;
std::cout << "Main thread returning..." << std::endl;
return 0;
}
|
|
|
|
|
Quote: it stays blocked on the join()
That's the correct behaviour: a never-ending thread won't join.
|
|
|
|
|
Not exactly, the destructor of the global variable sets an atomic to tell the thread to exit.
|
|
|
|
|
|
Yeh, it seems to be the same thing. I checked under Linux with GCC and it works. On my point of view, Visual C++ runtime after exiting the main() destroys all the threads then all the global variables, instead of the opposite.
|
|
|
|
|
Looks a sensible hypothesis to me.
|
|
|
|
|
I need a sorted list of strings with some additional information for each entry, so I thought about to use std::map
I am doing just simple things:
Definition:
typedef std::map<std::string, WORD> tTABMNEMO;
tTABMNEMO TabMnemo;
and in constructor of using class I am calling firstly:
TabMnemo.clear();
already this first access (in constructor of using class) causes an access violation.
any idea, what could be wrong?
Do I need to initialize anyhow?
maybe it is because of my program structure? this map is defined globally outside of the class and all the member functions of the using class are static functions!
|
|
|
|
|
Member 8534035 wrote:
TabMnemo.clear(); Have you tried:
try
{
TabMnemo.clear();
}
catch(exception &e)
{
cout << e.what();
}
Member 8534035 wrote: ...and all the member functions of the using class are static functions! So have you tried using it in a class with non-static functions?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
In my opinion that shouldn't happen. Are you sure the access violation is triggered by the clear method invocation?
Could you opst the exact code?
|
|
|
|
|
I am rebuilding my application and would like NOT to make local copies of parameters passed to the class constructor.
I have few methods which used to access application global variables , they are actually hardware "constants", and do not want to do that anymore.
I can rebuild these methods to include constructor passed parameters. no issue there.
But I was wondering if there is an another way to access the constructor parameters without adding them to the methods parameters or making local copies.
I hope I have explained my dilemma , if not , sorry.
Thanks Vaclav
( C/C++ and gcc)
|
|
|
|
|
Could you please post sample code?
|
|
|
|
|
It is normal practice to add parameters that remain constant in the object to the constructor like:
class Foo
{
private:
int paramThatDoesNotChangeOften;
public:
Foo(int someParam)
{
paramThatDoesNotChangeOften = someParam);
}
};
You could then add a getter method to return the value to the user if it's value is needed outside the class.
Is that what you are querying?
|
|
|
|
|
Thanks for replies.
It may be stupid, but that is what I am trying to avoid.
Now I would have "someParam" - actually global array and a class copy of the same so it can be accessed by class methods.
I am trying to save some memory of embedded processor.
Kinda of wishful thinking ahead.
I won't get to upset if I need to make a local copies.
Would using "this" pointer work making local copy?
I just tryied "standard" this.name = name but could not make it work on pointers.
I guess I am still struggling with using pointers.
Addendum
I have decided to KISS and I'll be making copies of the parameters using "this" pointer.
One question remains - since all of the parameaters passed are de facto constants - harwdware pins, LCD size etc. and won't be modified by the class, why can't I just use "this>LCD_x_size" as variable instead of "this->LCD_x_size = LCD_x_size" Now the class local varaible is "LCD_x_size".
And yes, I read somewhere that using same symbols for "name" and "variable" is OK.
Confusing, but OK.
-- modified 19-Sep-15 17:56pm.
|
|
|
|
|
If the parameters are actually constants, how about instantiating a template class?
int const PROCESSOR_COUNT = 42;
template <int processor_count>
scheduler
{
private:
processor_data data[processor_count];
};
scheduler<PROCESSOR_COUNT> sched;
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
Hello there,
I am looking for the best Way/Strategy to "Continuously read data from the HW in real-time and draw some GUI-diagram in Parallel". Performance would be a big matter here since the graph has to be drawn in real-time.
Its just coming to my mind about 1) creating 1 worker-thread for reading process, and 2) another for GUI drawing.
But I am looking for perfection and suggestions here.
1) Is there any specific Design-Pattern that would fit this?. If so, please suggest.
2) Whats the best way(container) to read data by the worker-thread and keep it for the GUI-thread to process. FIFO? or some other class?
Thanks in advance for your suggestion and advice.
|
|
|
|
|
FrankStar89 wrote: Performance would be a big matter here since the graph has to be drawn in real-time.
How many points per second?
Presumably there is no data munging at all?
|
|
|
|
|
Thanks Jschell.
I am not sure yet how many points per sec. ( still in the initial stage.) I think there would be data munging for sure.
Anyway, what I was looking here mostly any "usual Design Pattern" for this kind of problem in general. (1 thread is reading and another is drawing simultaneously).
Thanks again.
|
|
|
|
|
FrankStar89 wrote: I am not sure yet how many points per sec.
That of course would be the first requirement before proceeding. It impacts everything.
|
|
|
|
|
For worker threads, read here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Thanks David,
That would surely be helpful.
|
|
|
|
|
Hey guys,
I have to write a small program in C, I'll explain what it has to do.
You have product a, b and c
The input is the prices of a, b and c in cents.
You have to spend exactly 100 euros on the purchase of exactly 100 products.
As output I need to get a list of all possible combinations.
Lets give an example. Lets say the input is "88 99 102":
then the output has to be exactly like this:
1 a, 62 b, 37 c
4 a, 48 b, 48 c
7 a, 34 b, 59 c
10 a, 20 b, 70 c
13 a, 6 b, 81 c
I'm relatively new in progamming in C, so all help is appreciated, the deadline is short for this one.
int main(int argc, char *argv[]) {
int a, b, c, x, y, z
printf("price of a:\n");
printf("price of b:\n");
printf("price of c:\n");
scanf("%d, %d, %d" ,&a, &b, &c);
while (x+y+z=100){
x*a+y*b+z*c=10000
As you can see, I tried writing some code, but im stuck right here...
|
|
|
|