|
Hi all
This is all plain vanilla C. So no wise ass C++ answers. Thanx.
typedef bool (*HANDLER)(void*,...);
Now is it possible to assign a "templated" function? Eg
bool expect(const char* str);
HANDLER HAHA = expect("HAHA");
What other options can I exercise? IOW I need to pass a pointer to a templated function pointer? But how?
CHeers
leppie::AllocCPArticle(Generic DFA State Machine for .NET);
|
|
|
|
|
|
Could someone EXPLAIN what the : operator is designed to do. The line of code I'm looking at is...
m_nBmihAlloc = ((hGlobal == NULL) ? crtAlloc : heapAlloc);
The specifics of this code shouldn't be important. I just want to know what the : is for.
Thanks to whomever can help.
JD
|
|
|
|
|
The operator is actually ? : , it's the only trinary (three-argument) operator in the C/C++ language. If the test before ? evaluates to true , the value of the statement is the expression on the left of : , if the test is false the value is on the right of : . The example code could also be written as:
if (hGlobal == NULL) {
m_nBmihAlloc = crtAlloc;
} else {
m_nBmihAlloc = heapAlloc;
}
|
|
|
|
|
|
Michael Dunn wrote:
Thank you for using the correct word "trinary".
(instead of the popular but incorrect "ternary")
I take it you remember the debate on here about 5-6 months ago about this?
-Nick Parker
|
|
|
|
|
|
Does anyone know why there would be a problem using this on seemingly ONLY some XP systems? I guess it doesn't allocate the memory, and it returns NULL when I use it in:
LPVOID pData = CoTaskMemAlloc(BufferSize);
TRY
{
RegQueryValueEx(Sub3,"OBJECTS",NULL,&RegistrySize,(BYTE*)pData,&BufferSize);
}
CATCH(CException,e)
{
#ifdef _DEBUG
MessageBoxes(3);//this is an error/exit function
RegCloseKey(Sub3);
#endif
}
END_CATCH
In order to set the right size to BufferSize, I call it with NULL in place of the pData first. Is there a better way to do this, or a different function that works with XP consistently, or am I doing something wrong?
halblonious
|
|
|
|
|
It's either I or the debugger, because it's showing me for the statements:
int diff = len - flxCnt;
int GHCapturePt = len - diff;
=============================
len = 5 (which is true)
flxCnt = 3 (which is true)
diff = 2 (which is very true)
==============================
However, it's showing me that:
GHCapturePt = -858993460 (which is very wrong)
==============================
All the variables are defined as 'int', and the program compiles and links fine (no errors and no warnings). Later on in the program when 'GHCapturePt' is tested for equality with 'flxCnt', the condition failed, which is why I went into debugger to see what's going on.
Can anyone say why the debugger is showing this strange result? Thanks!
William
Fortes in fide et opere!
|
|
|
|
|
Check source code and see if the program assigns GHCapturePt to something different. I suspect that the program assigns GHCapturePt to something that does not exist such as a deleted pointer to the address of some value.
Kuphryn
|
|
|
|
|
Good point!
However, the debugger is showing the value of GHCapturePt right at that point before it does anything, or have anything done to it. IOW, GHCapturePt has not yet gone into action. Right exactly where the statement appears, is where the debugger is showing the value of GHCapturePt.
William
Fortes in fide et opere!
|
|
|
|
|
Add a breakpoint after that line and check the value of GHCapturePt. Another way to test it is to use a temporary CString object and show a message box with the actual value in string.
Kuphryn
|
|
|
|
|
Thanks!
When a breakpoint was added after the line that GHCapturePt appeared, the debugger showed the correct value!! This has been verified using real figures and real values also.
From here on, it's a matter of walking through the logic of the program to find out where the wrong turn was made, or the wrong value was used.
Thanks. I feel I have enough clues to find the source of the error.
William
Fortes in fide et opere!
|
|
|
|
|
sure ur not over-writing memory somewhere and using junk values stored there?
"there is no spoon" biz stuff about me
|
|
|
|
|
Thanks for replying.
No, to answer your question, but right now I believe I have things under control.
I just needed to set a breakpoint after the line where the variable was defined for debugger to show the correct value it contained.
Before, I was setting the breakpoint at the line of definition, thinking the result of the operation would be assigned to the variable, and is what I would be seeing. (Debugger doesn't seem to work that way.)
William
Fortes in fide et opere!
|
|
|
|
|
WREY wrote:
I just needed to set a breakpoint after the line where the variable was defined for debugger to show the correct value it contained.
Isn't that blatantly obvious?
WREY wrote:
at the line of definition
That means you would be breaking BEFORE the line of code is executed. So, obviously the variable would not show the correct value.
The kindest thing you can do for a stupid person, and for the gene pool, is to let him expire of his own dumb choices.
[Roger Wright on stupid people]
|
|
|
|
|
Since you are so logical, wouldn't it be logical to think that:
int GHCapturePt = len - diff;
would yield the result of the operation in GHCapturePt, and therefore setting the breakpoint at that line you'd get to see what GHCapturePt has in it???
Isn't that reasonable to think???
What is so blantantly obvious that setting the breakpoint at the line would not yield the result of the operation?
William
Fortes in fide et opere!
|
|
|
|
|
WREY wrote:
would yield the result of the operation in GHCapturePt, and therefore setting the breakpoint at that line you'd get to see what GHCapturePt has in it???
Take it easy WREY. Normally debuggers (the ones that I have used at least) don't work that way. You get to see the results of a line after that line has been executed. Imagine for a moment if you wanted to set a breakpoin on the very first line of the code. Where would you put the breakpoint? Before the line? There is nothing before that. However, your mistake is very normal and I must admit that I make it too once in a while (but I know the answer when I don't see the right results).
// Afterall, I realized that even my comment lines have bugs
When one cannot invent, one must at least improve (in bed).-My latest fortune cookie
|
|
|
|
|
Since I've already solved the problem which prompted posting the message in the first place, this is just academic "after the fact" stuff.
I guess the whole thing comes down to a matter of interpretation. I interpreted setting a breakpoint as telling the computer, "At this point, stop, and let me see what you have, or what you've done up to this point." I didn't interpret it as, "... excluding this line." I interpreted it as, "up to this point including this line where the breakpoint is being set."
To take your example of setting the breakpoint at the first line of code (even if that first line of code is):
int GHCapturePt = len - diff;
(accepting that 'len' and 'diff' are global variables) I would think that the line will have been executed and the contents of all variables in that line would have legitimate results in them. That was my thinking, and I can see now, it really doesn't matter what someone thinks, what it comes down to, is the way the tool works.
William
Fortes in fide et opere!
|
|
|
|
|
Toni78 said it right: the way you think debuggers should work is not the actual way they work. The breakpoint is set on a line to break BEFORE the line is run. That way you can set the breakpoint, break at that line or code, then step to the next line and see what that line of code did. It seems rather silly to have a breakpoint AFTER the code you want to look at has already been executed.
I bet you and I have different meaning to the old "1,2,3, Go" conundrum. Is it: "1,2,3, THEN Go", or is it: "1,2,3, GO on 3".
The kindest thing you can do for a stupid person, and for the gene pool, is to let him expire of his own dumb choices.
[Roger Wright on stupid people]
|
|
|
|
|
I am reading in a file and trying to put words read in a char[].
My array in within a struct and the notation is:
struct TSimHeader
{
char Name[47];
char Unit[47];
double Min[47];
double Max[47];
int SignalCount;
int SimStatus;
int simdkid;
int modmode;
int write;
int call;
}static TSimHeader_arr[10];
This is where I read in the file and try to put the values in the appropiate containers:
string name, unit,min,max,value;
for(int i=0; i<47; i++){
getline(fin, name, ',');
if(name == "STOP")
break;
getline(fin, unit, ',');
getline(fin, min, ',');
getline(fin, max, ',');
getline(fin, value);
strcpy(TSimHeader_arr[0].Name[i], name.c_str());
strcpy(TSimHeader_arr[0].Unit[i], unit.c_str());
TSimHeader_arr[0].Min[i] = atof(min.c_str());
TSimHeader_arr[0].Max[i] = atof(max.c_str());
TSimSignal_arr[0].Value[i] = atof(value.c_str());
sig_count ++;
}
The error I get is:
:*.cpp(104) : error C2664: 'strcpy' : cannot convert parameter 1 from 'char' to 'char *'<br />
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Thanks for the help,
Steven
|
|
|
|
|
Here's your problem: strcpy() copies the contents from one character-array to another. in the string class, c_str points to the char-array (which is good).
However, you're telling strcpy() to copy the contents of c_str to a single character in the array (Name[i] signifies the i-th character in Name).
Looking at your strcuture, it "LOOKS" like Name and Unit are supposed to be character-arrays (strings) of length 47, and there are 10 elements of type TSimHeader, which equates to 10 character-arrays (string) of length 47.
If this is the case, you must change the loop for 10 entries instead of 47.
string name, unit,min,max,value ;
for(int i = 0 ; i < 10 ; i++){
getline(fin, name, ',') ;
if(name == "STOP")
break ;
getline(fin, unit, ',') ;
getline(fin, min, ',') ;
getline(fin, max, ',') ;
getline(fin, value) ;
// The following assumes that name and unit
// are less than 47 characters long
strcpy(TSimHeader_arr[i].Name, name.c_str()) ;
strcpy(TSimHeader_arr[i].Unit, unit.c_str()) ;
TSimHeader_arr[i].Min[i] = atof(min.c_str()) ;
TSimHeader_arr[i].Max[i] = atof(max.c_str()) ;
TSimSignal_arr[i].Value[i] = atof(value.c_str()) ;
sig_count ++ ;
}
Again, this is assuming I interpretted this correctly.
|
|
|
|
|
You are correct but, There are 47 names, units, min, max. At each (ith) position the data is related.
If I just run through the loop 10 times then I won't get the rest of the data all put in the correct array within the struct.
I have it working with a 2 dimensional array.
I have limited memory so I need to really be sparing on memory.
So, I really need to store the names and units in a single dimension array
I hope all of that made sense.
I have a feeling that it won't work.
thanks,
Steven
|
|
|
|
|
What good is a code snippet if you don't even know C++?
A student knows little about a lot.
A professor knows a lot about little.
I know everything about nothing.
|
|
|
|
|
that's quite an order ... I feel nice today,
FindWindow can do that for ya' but I won't give more, you need to work on your c++ before continuing.
Maximilien Lincourt
"Never underestimate the bandwidth of a station wagon filled with backup tapes." ("Computer Networks" by Andrew S Tannenbaum )
|
|
|
|
|