This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 11 years ago.
Somebody told me that this piece of code has some serious issues but I have not been able to get my head around such issues. Can you guys please educate me on this?
static char BASED_CODE szFilter[] = "HTML Files (*.xls)|*.xls|All Files (*.*)|*.*||";
const char* filter = "HTML Files (*.xlsx)|*.xlsx|All Files (*.*)|*.*||";
size_t length = strlen(filter);
strcpy_s(szFilter, length + 1, filter);
Well, the buffer overrun leaps out at me – szFilter isn't big enough to receive filter.
Why not use std::string since you are using C++? That makes these issues vanish.
The second parameter of strcpy_s() should be the size of the destination buffer; you've given it the size of the input string.
But as you're working in C++, you should avoid strcpy() (etc.) entirely, and use std::string.
szFilter is shorter than filter, so there is not enough place to copy filter to szFilter. You current code has undefined behaviour.
Related
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I was wondering if casting here is the best solution:
This is the function prototype:
void function(unsigned char * data)
This is how I intend to use it (nSize is read from):
unsigned int nSize = 15;
function( (unsigned char*) &nSize);
Assuming the function prototype is set in stone and nSize has to be an int, yes that looks right to me.
Yes, in your case, cast seems necessary.
Note that using C++-style named cast is preferred than the C-style cast. In your case, reinterpret_cast is the right choice. Note that this is a dangerous behavior, see here for detail.
function(reinterpret_cast<unsigned char*>&nSize);
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am modifying some C++ code that has a wchar_t myArray[MAX_PATH] in the header file.
My modifications mean that I cannot know the length of this array until runtime. How can I store an array of dynamic length instead? Maybe I just keep a wchar_t* in the header file and another int to hold its length?
Use std::wstring instead. It's a dynamic string containing wchar_t.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
int foo(char *p)
{
static int i = 0;
if (*p == '\0') return i;
i++;
return foo(p+1);
}
What would be the problem if this function was placed in a generic library. How can I modify the code to sidestep the problem?
If this code were to be placed in a generic library will it return the correct string length?
One problem would be that the state persists between calls:
foo("hello");
foo("world");
The second call would return the incorrect result because i is not reset.
If you call it twice in succession, i won't be what you expect. That's because it's initialised at startup and retains its previous value across calls.
In addition, it won't work well with threads, and the idea of recursion is best used when the "search space" reduces quickly, like a binary search halving the space at each recursion level.
Processing a string character by character with recursion is a bone-headed idea, since you only reduce the search space by one character at a time.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
if((kulax>=schodki[i][0][0] && kulax<=schodki[i][1]][0]) && (kulay==schodki[i][2][0]+10))
spoczywa=true;
Hi guys, I have an array of integers which name is schodki and it is declared as int schodki[5][3][1] and the global variables : int kulax and int kulay.
What's wrong in the line of code which is above ?
EDIT : Of course. "i" is the value from current state of loop.
You have an extra ] in
kulax<=schodki[i][1]][0]
which probably screws up parsing and results in a confusing error message. The compiler probably sees it as
kulax<=schodki[i][1]
which is indeed an attempt to compare an integer to a pointer. Try to pay attention to your own code and make sure it is free from primitive syntax errors before asking questions here.
Other than that, there's nothing wrong with your code (assuming that the variables are really declared the way you say they are)].
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I've seen a pretty strange (for me) usage of this method:
strncpy(somePointer,"%d",someInt);
What does this actually do? The integer specifier "%d" as the source is troublesome for me to understand.
It does what it says on the tin: It copies the literal string "%d" into a char buffer pointed to by somePointer, or at least the first someInt bytes of it (up to three).
Don't be upset by a percentage sign, it's just another character...