3 ms·
> char response[] = "HTTP/1.1 200 OK\nContent-Type: text/html\n\nlol"; isnt it better to write that as: const char* response = "HTTP/1.1 200 OK\nContent-Type:
by mhogomchungu 12y ago
> char response[] = "HTTP/1.1 200 OK\nContent-Type: text/html\n\nlol";
isnt it better to write that as:
const char* response = "HTTP/1.1 200 OK\nContent-Type: text/html\n\nlol"; ?
Having the string in a writable buffer when you have no intention of modifying the string seems like not a very good idea.
Any decent compiler will evaluate "strlen(response)" at compile time and hence there will be no run time penalty here.
- exDM69 12y ago> Having the string in a writable buffer when you have no intention of modifying the string seems like not a very good idea. Any decent compiler will also notice this and put the string literal in the .RODATA section. Using "const" doesn't really make a difference, it's more of a hint for the compiler. It really doesn't make any difference in this case.
- smoyer 12y agoIt may be a hint to modern compilers, but it's also the documentation of intent for future developers[1]. [1] Where future developers includes me - I'll forget what I wrote in six months or less.
- mattstreet 12y agoNot just as documentation, it would prevent another developer from accidentally modifying it.
- smoyer 12y agoExcellent point ... I kind of assumed that (your IDE giving you a warning that you're trying to modify a constant) when I was thinking about it as documentation but your point more succinctly describes "making it hard to make mistakes".
- Matheus28 12y agoIt does, the compiler doesn't know what the function "send" will do. The const isn't the focus of what he said, it's allocating in the stack vs in a read-only memory location.