• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

char[4] C++ arrays, question

wantedSpidy

Senior member
Is this a segfault error?

char hex[4];
hex[4]='\0';

I mean if it was int hex[4], you could use hex[4]=0, or whatever.
I always thought you couldn't use the last byte of a character array, because the last byte was set to null, so why can't I do that myself in the program?

Comments?

Thanks
 
char hex[4];

This makes a char array named "hex" that is four characters long. The last MUST be a null to avoid buffer overruns.

strcpy(hex, "str");

That's as much as you can fit in it. Not sure if the last byte is automatically NULL after you declare it or not but perhaps someone else can clarify that. To my knowledge it is not necessarily automatically zero, so you'd do hex[3] = NULL; to terminate it (strcpy might do that anyway).

And yes hex[4] only has FOUR total elements: hex[0], [1], [2], and [3], there is no [4] as that would be accessing a nonexistent 5th element (and running into memory after that with no warning, BTW).
 
The last byte does not have to be NULL. It is convienent when using character strings.

All you have allocated is 4 consectutive bytes in memory. You need to ensure that you access them proplery.
 
Originally posted by: SoundTheSurrender
I thought you had to do something like hex + 3 = '\0';

to point to the last slot in the array.

I'm probably wrong.

If memory serves me correct, that should be: *(hex + 3) = '\0';

Originally posted by: wantedSpidy
Thanks.
I wasn't aware that the compiler wouldn't warn me!!

C++ doesn't warn you about such things. That's why it's not usually recommended for newer programmers... well, unless you have a nice IDE that will try to warn you of possibly erroneous code.
 
Depending on how you play with your memory design, C & C++ can allow you to exceed the assigned memory when using a pointer.

As pointed above *(hex+3) could point to another valid mamory address; but not give you the result that you are looking for.
 
Originally posted by: Aikouka
Originally posted by: SoundTheSurrender
I thought you had to do something like hex + 3 = '\0';

to point to the last slot in the array.

I'm probably wrong.

If memory serves me correct, that should be: *(hex + 3) = '\0';

Yes, which is equivalent to " hex[3] = '\0' " (which would be the normal way of doing such an assignment). " (hex + 3) = '\0' " would be trying to assign a character value to a static pointer, which won't work (and should not compile).

Overrunning the array by trying to read/write hex[4] or *(hex+4) will give undefined behavior. Reading from there will probably give you garbage back, and going far enough past will cause a protection fault. Writing past the end of arrays allocated on the stack will frequently corrupt stack frames and cause a segmentation fault later when you try to return from the function call. At best you'll be overwriting some other stack variable and causing very strange bugs.

strcpy() copies the null terminator as well. String literals are implicitly null-terminated in C. So strcpy(hex, "str") will result in hex[0] holding 's', hex[1] holding 't', hex[2] holding 'r', and hex[3] having a '\0'.
 
Back
Top