|
This is probably referring to values being 'hard coded'.
So if you have explicitly defined a numeric or string in your code, then it is termed as being 'hard-coded'.
The flip side of this is to construct your coding so that your values are user-defined, or can be obtained from an external source.
#define PI 3.142
float calc(int a, int b)
{
return (a * b * PI);
}
</code>
So in the above example, I have hard-coded the value of pi. In this case the accuracy of my calculations are fixed. If I needed to increase the accuracy of pi at a future date, I need to edit and recompile my code. This may not be a problem now, but can cause some problems in certain circumstances.
Hard coding string values are also widely mentioned and can be extremely difficult for multi-lingual software. In cases where your program messages, logs, reports and UI text may change (due to language changes for example), you do not want to hard-code text strings when writing your code. One way around this is to use the string resource files that de-couple this process.
I Dream of Absolute Zero
|
|
|
|
|
Hardwired[^] is often used to convey the concept of logic (i.e. control flow) that's "unchangeable" or "difficult to change".
/ravi
|
|
|
|
|
Adding to what other have posted, here are some examples of hardwired code:
void main(void) {
double cylinder_volume=5.5*3.1415*3.1415*7.9;
printf("The total cylinder area is %.15g\n", cylinder_volume*4.0);
long total_people_to_invite=10.0;
printf("The total invitations is %d\n", total_people_to_invite);
}
This code is hardwired to the extreme. Compare to the next code example:
#define PI 3.1415912
void main(void) {
double cylinder_radius=5.5;
double cylinder_height=7.9;
double cylinder_top_area=cylinder_radius*PI*PI;
double cylinder_volume=cylinder_top_area*cylinder_height;
double total_cylinders=4.0;
printf("The total cylinder area is %.15g\n", cylinder_volume*total_cylinders);
long singers=1;
long bartenders=3;
long dancers=5;
long cooks=1;
long total_people_to_invite=singers+bartenders+dancers+cooks;
printf("The total invitations is %d\n", total_people_to_invite);
}
This code is not as hardwired. Still, it could be coded to be even more flexible and reusable. For example, the area computation of a circle (used for the cylinder_top_area) could be implemented as a function which takes the radius.
In general, the sense of "hardwired" code is related simultaneously to how clear it is to read it, understand it, change it, and re-use it. The easier it is to acomplish those things *SIMULSTANEOUSLY* the less hardwired the code is.
In the above example all the predefined quantities could even come from some external configuration file, making changes easy even without recompilation. For the above example, this could be too much. So "hardwired" is not a clearly defined concept, but you see that if for such a simple example as the cylinder volume and invitation count external files had to be opened to read configuration parameters then, clearly, reading and understanding would be harder.
I hope this helps,
Rilhas
|
|
|
|
|
Hi,
I am writing a console application when I came across a problem where C++ would allow me to divide by zero when using doubles, but would not through out an exception. Is there anyway I can get the program to throw the divide by zero exception. Here is a sample program I made:
#include <iostream>
int main()
{
double x = 5.0;
double y = 0.0;
double z = x / y;
return 0;
}
The value of z ends up being infinity, but there is no exeception thrown and the program runs to completion.
|
|
|
|
|
You won't get an exception when dealing with floats or doubles. If you want to check for divide-by-zero problems, either check your denominator for values close to 0 or cast it to an int.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
|
Semantics changed since Visual C++ 7.1; structured exceptions (SEH) are no longer caught
When compiling with /EHs, a catch(...) block will not catch a structured exception (divide by zero, null pointer, for example); a catch(...) block will only catch explicitly-thrown, C++ exceptions.
Check MSDN for more information[^]
SaRath.
"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
My Blog | Understanding State Pattern
Last modified: Friday, August 04, 2006 12:11:47 PM --
|
|
|
|
|
kpeng1 wrote: Is there anyway I can get the program to throw the divide by zero exception.
Here is a MFC example.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I guess I kind of phrased things wrong. I actually want the debugger to catch the divide by zero. I tried to enable the break into the debugger option in for floating points divide by zero and it does not break into the debugger. Any ideas?
|
|
|
|
|
Your best solution would be to do something like:
double x = 5.0;
double y = 0.0;
const double epsilon = 0.0001;
if (abs(y) < epsilon)
{
throw "Divide by 0!";
}
double z = x / y;
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Exceptions are expensive. Checking for zero before doing a division operation where zero might be in the denominator is cheap and the preferred method.
(I also add ASSERTS to check for these conditions, which will cause a catchable exception in the debugger.)
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
strToken is retaining an invalid value.
+ strToken 0xcccccccc <Bad Ptr> char *
unsigned int CSentenceAr::search(const char* strSearch)
{
// initialize local variables
unsigned long iWordCount = 0;
char buffer[MAX_SENTENCE_LENGTH] = {0};
/* Algorithm:
strToken shall initially contain the sentence string.
When strWord is found from the string, this is removed
from the string so that this function checks the rest of the
string for other instances of the word.
*/
char* strToken = {0};
for (int iCurrent = 0; iCurrent < Size; iCurrent++)
// copy to buffer, used as parameter for strstr.
{ memcpy(buffer, Sentences.strSentence[iCurrent], MAX_SENTENCE_LENGTH);
strToken = strstr(buffer,strSearch);
while (strToken !=0 && *strToken != 0)
{
if (strlen(strToken) > 0)
{ iWordCount++;
strToken += strlen(strSearch);
}
if (strToken >= buffer + MAX_SENTENCE_LENGTH)
{break;}
strToken = strstr(strToken, strSearch);
}
}
return (int) iWordCount;
}
Is it declared incorrectly?
Jon
|
|
|
|
|
jon_80 wrote: strToken is retaining an invalid value.
At what point?
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Since it is declared (see the code), that's why I said retained. The problem is that since it has an invalid value the program is not going through the while loop.
Jon
|
|
|
|
|
strstr() will return NULL if strSearch is not found in buffer . That would be why the while loop is not entered. You should be able to set a breakpoint on the strstr() statement to confirm this.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
I meant to copy Sentences.strSentences to the buffer, look for the occurrence of strSearch within buffer and the pointer returned by strstr written to strToken.
memcpy(buffer, Sentences.strSentence[iCurrent], MAX_SENTENCE_LENGTH);
strToken = strstr(buffer,strSearch);
while (strToken !=0 && *strToken != 0)
{
if (strlen(strToken) > 0)
{ iWordCount++;
// The program looks for strSearch from the position where it was found onwards.
strToken += strlen(strSearch);
}
if (strToken >= buffer + MAX_SENTENCE_LENGTH)
{break;}
strToken = strstr(strToken, strSearch);
}
I hope it is clear now. What I don't get is what is wrong with strToken?
Jon
|
|
|
|
|
jon_80 wrote: strToken = strstr(buffer,strSearch);
What is the value of buffer and strSearch at this point?
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
Break up the scope of your function:
Function 1 will take a single sentence and count how many times a substring appears in it.
Function 2 will take a collection of sentences and call Function 1 on each of them and sum the results.
const unsigned long MAX_SENTENCE_LENGTH = 100;
unsigned long HowManyInSentence(const char* sentence, const char* substring)
{
char buffer[MAX_SENTENCE_LENGTH + 1] = {0};
memcpy(buffer, sentence, MAX_WORD_LENGTH);
char* token = 0;
unsigned long wordCount = 0;
token = strstr(buffer, substring);
while (token != 0 && *token != 0)
{
if (strlen(token) > 0)
{
++wordCount;
token += strlen(substring);
if (token >= buffer + MAX_WORD_LENGTH)
{
break;
}
}
token = strstr(token, substring);
}
return wordCount;
}
unsigned long HowMany(const char* substring)
{
unsigned long count = 0;
for (int i = 0; i < MAX_SENTENCES; ++i)
{
count += HowManInSentence(g_sentences[i], substring);
}
return count;
}
Make sure that your buffer is null-terminated otherwise token will end up off in the weeds.
Oh, and token doesn't need to be assigned with scope (that is, you don't need char* token = {0}; ). char* token = 0; is fine and more readable.
-- modified at 12:58 Friday 4th August, 2006
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Thanks, but what do you mean by null-terminated? Also why this line of code?
if (token >= buffer + MAX_WORD_LENGTH) { break; }
Jon
|
|
|
|
|
jon_80 wrote: ...what do you mean by null-terminated?
Strings (not to be confused with an array of characters) in C must be nul-terminated in order to use string-related functions (e.g., strcpy() , strlen() , strstr() , strchr() ).
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
jon_80 wrote: but what do you mean by null-terminated?
Lets say I write the following:
char* myString = "Hello, World!";
There are 13 characters in that string, but in memory, the size allocated for it would be 14. The reason is that the last character will be a 0, so the string actually looks like this in memory: Hello, World!\0 . C-style strings require this, as do many C++ string wrappers.
if (token >= buffer + MAX_WORD_LENGTH) {
break;
}
The above section of code checks to make sure that the token (which is nothing more than a character pointer) is not pointing to something that is outside the buffer. buffer is an array of characters (with a size of MAX_WORD_LENGTH -- which actually should have been renamed to MAX_SENTENCE_LENGTH). Basically, if token has moved passed the end of the string, we stop.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
>>memcpy(buffer, Sentences.strSentence[iCurrent], MAX_SENTENCE_LENGTH);
how is Sentences.strSentence declared? is it a char**? has memory been allocated or is your program just scribbling all over memory with that statement?
|
|
|
|
|
it's a char...
struct Sentence
{
char strSentence [MAX_LINES] [MAX_SENTENCE_LENGTH];
};
Jon
|
|
|
|
|
When I select menu item Help-->Search, enter a search string, the IDE displays a URL in the middle of the screen and the only way I can get rid of it is to shut down and restart the entire IDE. This is really starting to pi$$ me off!
Any way to change that "feature"?
|
|
|
|
|
Anyway I could not search from the net using MSDN installed with Visual Studio. it's saying that proxy authentication failed. even Im simply browsing using Internet explorer. what a heck is this
SaRath.
"Where I am from, there is no plan B. So, take advantage of today becuase tomorrow is not promised. - 50 Cent"
My Blog | Understanding State Pattern
|
|
|
|