Anything wrong
-
The first yes makes me not to click on the x of this window. Could you please tell me what is wrong with the code before I click on x?
:beer:
Smith# wrote:
char chz[4]={0};
this buffer is too small, your sprintf statement may generate a minus sign, three digits, a space, and a NULL, that is 6 characters. 60 of these could take up to 301 chars, so
char szLogBuff [512]={0};
is sufficiently large, and as others already pointed out, your total initialization is a waste, as each strcat will move the initial terminating NULL backwards. BTW: you could get the correct result with less code, and save some cycles, and never have the bug you had, by having sprintf() fill the final buffer right away; all it takes is a variable pointer as the destination, initialized to szLogBuff, and incremented by the return value of sprintf! :)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
The code (albeit not beautiful) is correct. With
szLogBuf[512]={0};
you actually initialize the whole buffer with 0 (that is '\0'). :)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.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]CPallini wrote:
The code ... is correct.
:thumbsdown:
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
CPallini wrote:
The code ... is correct.
:thumbsdown:
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
It isn't wrong. It isn't elegant nor optimal, but not wrong. I think this is a great achievement, after all. (It's a subliminal suggestion for you: could you please hire the OP?)
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.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Smith# wrote:
char chz[4]={0};
this buffer is too small, your sprintf statement may generate a minus sign, three digits, a space, and a NULL, that is 6 characters. 60 of these could take up to 301 chars, so
char szLogBuff [512]={0};
is sufficiently large, and as others already pointed out, your total initialization is a waste, as each strcat will move the initial terminating NULL backwards. BTW: you could get the correct result with less code, and save some cycles, and never have the bug you had, by having sprintf() fill the final buffer right away; all it takes is a variable pointer as the destination, initialized to szLogBuff, and incremented by the return value of sprintf! :)Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Luc Pattyn wrote:
your sprintf statement may generate a minus sign
No, it cannot. ;P
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.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
It isn't wrong. It isn't elegant nor optimal, but not wrong. I think this is a great achievement, after all. (It's a subliminal suggestion for you: could you please hire the OP?)
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.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]CPallini wrote:
It isn't elegant
true.
CPallini wrote:
nor optimal
true.
CPallini wrote:
not wrong
false. see my reply to OP. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Luc Pattyn wrote:
your sprintf statement may generate a minus sign
No, it cannot. ;P
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.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]from the code snippet I can't tell what the definition of BYTE is, could be signed, could be unsigned; I prepare for the worst. Anyway, it isn't really relevant, the buffer isn't sufficiently large. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
CPallini wrote:
It isn't elegant
true.
CPallini wrote:
nor optimal
true.
CPallini wrote:
not wrong
false. see my reply to OP. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
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.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
from the code snippet I can't tell what the definition of BYTE is, could be signed, could be unsigned; I prepare for the worst. Anyway, it isn't really relevant, the buffer isn't sufficiently large. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
You didn't read the snippet, did you? (hint: it _s_prints the index, the BYTE part is completely insignificant). :)
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.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You didn't read the snippet, did you? (hint: it _s_prints the index, the BYTE part is completely insignificant). :)
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.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]How silly. So that must be wrong too. :laugh:
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
How silly. So that must be wrong too. :laugh:
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
:laugh:
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.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]