|
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...
|
|
|
|
|
This is not a programming problem, more one of mathematics. You first need to work out the algorithm to calculate the numbers of each product that you can buy for €100. Once you have that then turning it into code should be fairly trivial.
|
|
|
|
|
A brute force approach would work, e.g.
for ( x = 1; x <= 98; x++)
for ( y = 1; y <= (99 - x); ++y)
{
z = 100 - x - y;
}
|
|
|
|
|
Thanks. I'm finally getting the hang of it. I'll try something tomorrow and I will let you peoples know if I succeed or not.
|
|
|
|
|
|
Thanks again, I make improve in the code, but it ain't working as supposed.
I'm really wondering where the code is wrong, I get no output or a wrong output (e.g. 100 100 100 gives as output only 100 a, 0 b, 0 c.)
I tried different things and I guess I'm almost there, but can't fix it.
So please, help me with this code.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
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);
for(x = 0; x <= 100; x++)
{
for(y = 0; y <= (100 - x); ++y)
{
z = 100 - x - y;
if(x*a + y*b + z*c == 10000)
{
printf("%d a, %d b, %d c",x ,y ,z);
}
}
}
return 0;
}
|
|
|
|
|
You don't use correctly scanf . Try
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
int a, b, c, x, y, z;
int done = 0;
while ( done == 0)
{
printf("please enter the price of a:\n");
if ( scanf("%d", &a) != 1) continue;
printf("please enter the price of b:\n");
if ( scanf("%d", &b) != 1) continue;
printf("please enter the price of c:\n");
if ( scanf("%d", &c) != 1) continue;
done = 1;
}
for(x = 0; x <= 100; x++)
{
for(y = 0; y <= (100 - x); ++y)
{
z = 100 - x - y;
if(x*a + y*b + z*c == 10000)
{
printf("%d a, %d b, %d c\n",x ,y ,z);
}
}
}
return 0;
}
|
|
|
|
|
Thank you so much! It works great. I had to change the code a bit to pass all testcases, but finally I made it .
However, I'd like to know how that piece of code between the While { ... } works, I don't really get it.
done = 0, so while ( done == 0) is true. Then it runs the code and you can enter the values. But why does it need the done == 0 and why the != 1? I understand the done = 1, otherwise it will run the while statement again, right?
|
|
|
|
|
Well, I don't know why it did not work the first time, but I tried to simplify the code and ended up with:
printf("price of a: price of b: price of c: ");
scanf("%d %d %d", &a, &b, &c);
instead of that thing with while { ... }
And this time, it did work. I do not really see the difference with the code I used before that did not work.. :p
|
|
|
|
|
Even the code you used before would have work with a suitable input, that is if the user would have entered, for instance
100,100,100
that is, with the commas.
However, the code handling input from the user should be more robust.
|
|
|
|
|
Perhaps your problem can be broken up into two smaller problems: 1) Find all combinations of A+B+C that equal (quantity) 100. I *think* that gives you a set of about 4851 combinations. 2) For each of those, multiply A by 0.88, b by 0.99, and C by 1.02, and if that product equals 100 euros, you have a match.
"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
|
|
|
|
|
That sounds like a faster way to calculate, because of the smaller set of combinations. I'd like to try that another time, but I don't have the time to do that now before the deadline. Thanks for the tip, nice to see how there are multiple solutions to work it out.
|
|
|
|
|
Hi all,
I need callback functions between different classes. I had already a solution based on stl, which was compilable with VS2010. Unfortunately I was not able to find a way to compile it with VS2015. I got weird error messages.
Now I found a solution with lamda C++11 lambda function pointers. Actually it works, but I have one open issue.
For having a save source code, I want to initialize the function pointer with NULL. But compiler is producing weird error messages again!
My Code:
typedef std::function<bool(int, int, CString*)> _tcedit_cb_;
class MyClass
{
public:
MyClass ()
{
m_cbGetItemData = NULL;
}
_tcedit_cb_ m_cbGetItemData;
...
}
How I can initialize the function callback pointer? I need to initialize in order to avoid running into illegal code!
e.g. I tried to cast ... = (_tcedit_cb_) NULL;
error messages:
c:\program files(x86)\microsoft visual studio 14.0\vc\include\type_traits(1501) : error C2893 : Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&...)'
1> c:\program files(x86)\microsoft visual studio 14.0\vc\include\type_traits(1501) : note : With the following template arguments :
1> c:\program files(x86)\microsoft visual studio 14.0\vc\include\type_traits(1501) : note : '_Callable=_Decayed &'
1> c:\program files(x86)\microsoft visual studio 14.0\vc\include\type_traits(1501) : note : '_Types={int, int, ATL::CStringT<wchar_t,StrTraitMFC<wchar_t,ATL::ChTraitsCRT<wchar_t>>> }'
1> c:\program files(x86)\microsoft visual studio 14.0\vc\include\functional(210) : note : see reference to function template instantiation '_Rx std::_Invoke_ret<_Rx,_Callable&,_Ty,_Ty,ATL::CStringT<wchar_t,StrTraitMFC<wchar_t,ATL::ChTraitsCRT<wchar_t>>>>(std::_Forced<_Rx,false>,_Callable &,_Ty &&,_Ty &&,ATL::CStringT<wchar_t,StrTraitMFC<wchar_t,ATL::ChTraitsCRT<wchar_t>>> &&)' being compiled
1> with
1>[
1> _Rx = bool,
1> _Callable = _Decayed,
1> _Ty = int
1>]
|
|
|
|
|