|
Dewm Solo wrote:
I am actually surprised that this compiles though...and runs. Shouldn't the compiler complain about this?
Yeah. While it should of course compile, I'd want this warning: http://msdn2.microsoft.com/en-us/library/9x19t380(vs.80).aspx[^]
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
Indeed! Right on the button.
Dewm Solo - Managed C++ Developer
|
|
|
|
|
Dammit, I had to stick this in the IDE and debug it to work out what the hell was wrong with this. I think I've been doing a little too much ASP maintenance today
|
|
|
|
|
I was starting to get mad at this code, thinking it is one of those "inside" jokes where everybody laughs until you start laughing, then they laugh harder cause there is no joke. Nobody said what the problem was. Then after reading all the comments, I went back and reviewed it again. I found the issue! Funny how it never shows up till the end.
Hogan
|
|
|
|
|
yes, just like the code when you encounter it. wasn't funny until you end up looking at it for 30 or so minutes and seeing that the semicolon was the problem.
|
|
|
|
|
When happened to me, I thought for a (long) while about a compiler bug, before finding the ****** semicolon.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
took me about five seconds to see the error.
maybe I've stumbled into most sh*t already.
Larswad
|
|
|
|
|
A simple example of how not to copy a string...
sprintf(packet.data.header.filter, "%s", "wild");
it works, I suppose.
Rob Grainger
|
|
|
|
|
The ones I love are these (assuming 8-bit characters) :
strncpy( szDestination, szSource, sizeof(szSource) );
or
strncpy( szDestination, szSource, strlen(szSource) );
or
strncpy( szDestination, szSource, strlen(szDestination) );
|
|
|
|
|
For us noobs, what is wrong with the last one? At least it uses the destination length.
|
|
|
|
|
I am noob too, but I dare to guess that it loses terminating zero.
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
|
It works if the current contents of the destination are smaller than the source string. It won't copy entirely if the current destination contents are less than the source, even if the buffer is physically large enough to hold the source. By using strlen, it does not use the full available length of the destination buffer. strlen versus sizeof
Judy
|
|
|
|
|
Spot on
|
|
|
|
|
well, imagine that:
char szDestination[] = { 0xFF, 0xFF };<br />
<br />
strncpy(szDestination, szSource, strlen(szDestination));
what do you think would be strlen() here?
-- modified at 22:38 Thursday 1st November, 2007
|
|
|
|
|
It's an improved random generator!
|
|
|
|
|
Steve Hansen wrote: It's an improved random generator!
ROFL
I ought to patent this!
Except if you do strncpy() as given you would most likely overwrite the return address or whatever comes next in enclosing function.
|
|
|
|
|
back in K&R C days this was always special:
memcpy( dest, src );
|
|
|
|
|
Seen the same dumb stuff to "clear a string":
sprintf( pcSomeStringBuffer, "%s", "");
<code>
strcpy( pcSomeStringBuffer, "" ); Peace!
-=- James Please rate this message - let me know if I helped or not!<HR> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
Who the hell names database fields 'from' and 'to'? Need I say more?
|
|
|
|
|
This can be a lot of fun if you have to work with that
Cheers
You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)
|
|
|
|
|
SELECT [SELECT], [FROM], [TO], [WHERE], [ORDER] FROM [WHERE] WHERE [WHERE]='GROUP BY OR HAVING'
This blanket smells like ham
|
|
|
|
|
This same schema just caught me again, as I have a policy of naming all my DAL parameters the exactly the same as the column names, which works Ok until a column that is called IsNew in a view is actually called 'new' in the table.
|
|
|
|
|
That reminds me of some fun I used to have (aeons ago) in Coral-66:
'FOR' FOR = 0 'TO' TO 'STEP' STEP 'UNTIL' UNTIL
'BEGIN'
BEGIN;
END;
'END'
something like that anyway - I can't remember the exact syntax
Phil
The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.
|
|
|
|
|
Database designers who don't have to write any SQL.
|
|
|
|