last day (15 days later) » 

4:21 PM
-5
A: Unhandled exception at 0x00363A09, Stack cookie instrumentation code detected a stack-based buffer overrun

6502sizeof(x) returns the size in chars of object x, not the number of elements in case x is an array. Note that I used the term chars instead of "bytes". The reason is that there's a somewhat common misconception that a char can be more than one byte. This is false for the definitions of "byte" use...

 
@Tiphaine: a character is always one "byte" for C and C++. May be it has more than 8 bit, but that's a different thing. Standard mandates sizeof(char) to be 1 on every platform (including ones in which CHAR_BIT is not 8).
@LightnessRacesinOrbit: the first comment clearly shows that the commenter thought that sizeof(char) could be different from 1. Saying bytes doesn't help because s/he will keep thinking this is the case. I think that using character (that was in bold for a reason) helped clarifying also this important point even if admittedly a better explanation would have been better (that's why I edited the answer). My suggestion of deleting the comments was honest: the original commenter is not going to look better for insisting after finding s/he was wrong, and admitting the error would be the best.
@Cerbrus: I know... however that is also the size in characters. I prefer to use the term characters because byte for a lot of people means 8-bit. This is for example what obviously the first commenter thought.
@LightnessRacesinOrbit: Apparently it was not irrelevant at all. The original commenter says "If a character is 1 byte, it will be the same, but don't take this for granted". I can take it for granted because it's what the standard mandates. Clearly the commenter thought that sizeof returned the number of octects, when this is not the case.
 
Kinda with @Lightness here. Insisting that "characters" be the term used here is a bit specious, especially when byte is the correct, unambiguous term to use.
 
@LightnessRacesinOrbit: I'm sorry but I think Tiphaine thought instead that sizeof(char) could have been something different from 1. That is the common misconception about sizeof I am talking about (for example on github if you search for "sizeof(char)" you get more that 2M hits).
 
You get that many hits because sizeof(char) is a very common code phrase.
 
@RobertHarvey: the problem with "bytes" is that the term has, in my opinion, a stronger (albeit formally wrong) correlation to 8 bit. For C++ char and byte are always the same size. If you however say a novice that sizeof returns the size in bytes there's IMO an high risk that s/he will get the wrong message, understanding "octects" (8-bit bytes) instead of "c++ standard bytes" (CHAR_BIT bytes).
 
4:21 PM
@CraigMeier: Regardless, the editorial about character misconceptions doesn't really have much to do with the OP's question.
 
@CraigMeier: Good point, changed from character to char
@RobertHarvey: The OP thought sizeof returned the number of elements. The first commenter to my answer however thought that sizeof returned the number of octects, and was "correcting" me because he thought that my answer was wrong because a char could have been more than one byte.
 
[shrug] Like other folks have already stated, it's your answer.
 
@RobertHarvey: about sizeof(char) being common: code using sizeof(char) simply shows (in most cases) that the author doesn't know how C/C++ works and that a sizeof(char) is always 1. This is a typical fragment: word = (char*)malloc(sizeof(char) * 5); word = "begin"; where alloc+leak+assignment clearly shows the confusion in author ideas (let's also ignore subtleties like the missing space for the NUL terminator).
 
At the end of the day, it's your answer. It's got a -5 score because you've hijacked the post to make an editorial point, but it's still your call.
 
@RobertHarvey: I didn't hijack anything. There was an answer that I think is correct and that got a comment saying it was wrong. Indeed the comment was wrong and the wording I used was EXACTLY to try to illuminate the dark corner of sizeof that the commenter got wrong. The post was not perfect, and I tried to make it better providing a deeper explanation. The accepted answer is correct, but in my opinion glides over this dark corner that can actually bite badly and that novices should IMO be warned about (and yes, for work I have to deal with a platform where CHAR_BITS is 16).
 
4:21 PM
The OP never asked about sizeof(char). As you've already correctly stated, his problem actually has nothing whatsoever to do with that.
 
@RobertHarvey: The OP was confused about what sizeof returns. Saying that returns the number of bytes (omitting that bytes can be bigger that 8 bit) is telling half the truth IMO, because most coders when reading "byte" don't think to "C++ standard byte". The debate started with the first COMMENTER to my answer, not the OP and was about sizeof(char). I told him/her that s/he was wrong, that's a common mistake with C++ and this insisting like was doing was not good for the image s/he leaves on the internet. Then a 300k out of the blue came and changed the spirit of my answer for no reason.
 
It's the OP's question, not the commenter's. A better response would have been to refute the commenter's assertion by posting a comment, not hijacking your answer.
 
@RobertHarvey: it's what I did. I replied him/her with a comment that s/he was wrong and that sizeof(char) is always 1. The I added a deeper explanation to my answer explaining why I prefer to use "size in characters" instead of "size in bytes" because admittedly it wasn't very clear. Then after another user commented that there's a confusion between "characters" and the char type I changed again to char because I think is even clearer. Check the edit history. Why Lightness changed the spirit of my answer and now is trying to delete it is beyond me...
 
I'm a bit baffled why you keep trying to hold onto this. But again, it's your funeral.
 
Why funeral? I'm not sure I understand...
 
4:22 PM
The purpose of Stack Overflow is to ask questions and get answers, not to engage in a debate with a commenter over something that isn't even in the spec.
And by putting your argument in an answer, you went off the reservation.
Because the answer isn't about your argument in the comments; it's about the question that the OP asked.
 
I honestly think that my answer is better than the one accepted. Probably before the editing that added the deeper explanation wasn't, but still was correct.
 
Clearly you think that.
 
The commenter simply confirmed my experience that a lot of novices and even experienced C++ programmers think that sizeof(char) can be different from 1
 
Which really has nothing to do with the question asked.
 
It has, because the OP thought sizeof of an array returned the number of elements and now, most probably, thinks that sizeof returns the number of 8-bit bytes.
 
4:26 PM
So what does sizeof return?
 
the number of CHAR_BIT bytes, i.e. the number of char that would amount the same size of the object
 
Do you have a reference for that?
 
AHAHAHAHAHAHA ... i cannot believe this. You didn't even took the time to check that I was right!
ok... gimm a sec
 
Never mind. Found it.
It says here that CHAR_BIT is the number of bits in a byte.
 
The sizeof operator yields the number of bytes in the object representation of its operand. The operand is
either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id. The sizeof
operator shall not be applied to an expression that has function or incomplete type, to an enumeration
type whose underlying type is not fixed before all its enumerators have been declared, to the parenthesized
name of such types, or to a glvalue that designates a bit-field. sizeof(char), sizeof(signed char) and
 
4:31 PM
Ok. So what does any of that have to do with the OP's question? And how is that any more illuminating than the phrase "CHAR_BIT is the number of bits in a byte"?
Even the language lawyers are not this pedantic.
The first sentence of your quote says all the OP needs. "The sizeof operator yields the number of bytes in the object representation of its operand."
 
The OP thought sizeof was the number of elements. The accepted answer is correct, but glides over a very important fact that the "bytes" the C++ standard is talking about are not the "bytes" that most programmers think to.
 
What? A byte is a byte.
There aren't "flavors" of bytes. A byte is 8 bits. That's it.
You're getting this backwards. sizeof(char), sizeof(signed char) and sizeof(unsigned char) are all 1 because they're all the size of a byte.
 
No. a byte is not 8 bit.
 
Oh, ffs.
 
I work for example with TMSC320 where CHAR_BIT is 16
it's a texas DSP
IIRC Cray has (had) CHAR_BIT = 32
the only guarantee is that CHAR_BIT >= 8
but there are platform where sizeof(char) = sizeof(int) = 1
 
4:36 PM
The vast majority of people are not working with TMSC320's or computers from the 50's. A byte being 8 bits is going to be correct 99.99 percent of the time.
And none of this still has anything to do with the OP's question.
 
My answer is (was if you guys succeeded deleting it) an explanation of what sizeof returns, avoiding pushing this concept that returns the number of 8-bit bytes (it's not what it does).
The accepted answer is correct, but glides over what is IMO an important detail
you think the detail is irrelevant because CHAR_BITS=8 in 99.99% of the cases
 
It's irrelevant 99.99 percent of the time.
 
CHAR_BIT=16 is used in some low-end platforms (not sure as I'm not an HW guy, but probably simplifies silicon while keeping high clock)
those platform cannot address a single 8-bit byte. memory is composed of 16-bit words on TMSC320
 
OK. But a little perspective could go a long way here. From a practical standpoint, you don't optimize your answers for 0.01 percent of the population.
 
I prefer to not push a misconception... i.e. that sizeof returns the number of 8-bit bytes
 
4:46 PM
Stack Overflow isn't a soapbox. Do that somewhere else.
 
I'd prefer to have around less programmers that think is writingsizeof(char) in a C++ program makes sense (it doesn't)
I don't understand the soapbox reference, sorry. I'm not a native english speaker.
 
@6502 How are you going to accomplish that with an answer that's buried under downvotes? :)
 
Are you going to change your downvote? At least to thank me for making you discover that the actual number of bits in a byte isn't (for the C++ standard) 8?
 
No. That's not why I downvoted.
 
Kidding apart I'm passed long ago the youth mania of correcting who is wrong on the internet. I'm not 100% sure but I think I'll survive anyway.
You downvoted because you hate me? because you think that a 300K must be right and s/he should go around and change other people answers so that they reflect what s/he thinks should be the answer?
 
4:55 PM
I've discussed at length my reasons for the downvote. Hate has nothing to do with it; downvotes are about the post, not the person.
 
yeah sure... do you understand what the post says now? do you agree that is correct what is said?
 
If you consider yourself an educator, then you must also know the importance of focusing on what is relevant. While CHAR_BIT is certainly an interesting factoid, it's almost certainly not relevant to the OP, because he's working on an 8-bit byte platform. It certainly shouldn't be the prime focus of an answer.
GTG. Nice talking with you.
 
C++ is an ugly monster, being correct is fundamental in my opinion.
bye
By the way... the post got deleted
@RobertHarvey: btw the post got deleted
 
5:24 PM
Yeah. For good cause, IMO.
 
Zoe
6:18 PM
@RobertHarvey Just out of curiosity: what is the deletion reason? Extremely low quality should be flaggable as well, and it does attempt to answer the question (stackoverflow.com/help/privileges/trusted-user). Is the correctness of an answer a deletion reason for 20k users?
And just to be clear, I'm not arguing that this post shouldn't have been deleted, I'm just curious about when 20k answer deletion is appropriate beyond when there's an obvious rule violation (link-only, questions as answers, spam, rants, etc.)
 
 
1 hour later…
7:21 PM
@Zoe: he deleted the post because he can. He's clearly not objective on this issue as apparently he didn't even take the time to check that what I was saying was correct (check the chat and you'll see he discovered that a byte for C++ standard can have more than 8 bit while chatting with me. I don't see any technical reason except he want to "punish" me in some form I suppose. I'm not 100% sure why however...
 
Zoe
You do know 20k users deleted it first, right?
 
Yes... and I didn't understand why, especially because now all the noise was moved out to chat
I flagged the deletion for moderators... but that's not going to work if moderators hate me :-)
 
Zoe
The documentation is still against you. If you're going to discuss the deletion, take that to meta (or, well, flags, I guess), but I figured I might as well ask about the rules on deletion while that's actually the topic because I'm curious, not to argue about the deletion.
 
All I wanted was my answer (that I think correct, btw). I got it defaced by a 300K first then deleted for no valid reason. What is the part of the documentation that is against me? May be I don't understand SO rules...
 
Zoe
The language documentation, which states bytes
 
7:27 PM
Yes... but they're not 8-bit bytes
they're CHAR_BIT bytes, and CHAR_BIT can be bigger than 8
thinking that sizeof returns the size in 8-bit bytes is exactly the common misconception that my answer tries to fight with
 
Zoe
yeah, and the definition of a CHAR_BIT is "the amount of bits in a byte". Not the amount of bits in a char, in a byte. Also, from stackoverflow.com/a/9727504/6296561:
> character

single-byte character
Char is defined by a byte, but a byte doesn't have to be 8 bits
 
and there are platforms in which this happens for real... at work I've to deal with a DSP where CHAR_BIT is 16, for example
 
Zoe
saying it returns the size in chars doesn't make sense when a char is defined as one byte
So? It's still defined as one byte
 
It's the same but I prefer to avoid the term byte because for C++ means something and for most programmers means something else
 
Zoe
Yeah, but when you use a different term because of personal perference, you're bound to have that pointed out
 
7:41 PM
even an experienced programmer like Robert Harvey thought that CHAR_BIT had to be 8
The OP thought that sizeof returned the number of elements in the array. Of course that's totally wrong. But just saying that returns the number of bytes is misleading, because who reads will think that it's the number of 8-bit bytes, while instead is the number of CHAR_BIT bytes
my answer existed just to point that out and make it clear
 
Zoe
derp, nvm
 
Can you read the first phrase and tell me why this is not an answer to the question?
 
Zoe
1 min ago, by Zoe
derp, nvm
 
yes, sure
but there are platforms where sizeof(char) = sizeof(int) = 1 and ints go from -32768 to 32767
 
Zoe
Yeah, on 16 bit systems
 
7:52 PM
not on all of them, but for example on the DSP we use on our machines
actually it's a compiler thing, not a system thing.
there are supercomputers where CHAR_BITS is 32, but you can also compile with have CHAR_BITS=8 At least in C
they make pointers bigger to store also which byte the pointer can point to.
did you know that not all pointers need to be the same size? That's the reason
a pointer to a char may be bigger
 
8:30 PM
would be ok to repost it?
 
Zoe
No idea
 

last day (15 days later) »