1

Is it possible to globally disable NUL-terminated strings in GCC?

I am using my own string library, and I have absolutely no need for the final NUL characters as it already stores the proper length internally in a struct.

However, if I wanted to append 10 strings, this would mean that 10 bytes are unnecessarily allocated on the stack. With wide strings it is even worse: As for x86, there are 40 bytes wasted; and for x86_64, 80 bytes!

I defined a macro to add those stack-allocated strings to my struct:

#define AppendString(ppDest, pSource) \
  AppendSubString(ppDest, (*ppDest)->len + 1, pSource, 0, sizeof(pSource) - 1)

Using sizeof(...) - 1 works quite well but I am wondering whether I could get rid of NUL termination in order to save a few bytes?

12
  • What should happen to empty strings? How do you propose to deal with them? Nov 20, 2009 at 17:36
  • 4
    I'm pretty sure 80 bytes is not going to be a problem for your program. Especially on x86.
    – Carl Norum
    Nov 20, 2009 at 17:38
  • 2
    Good luck with replacing the string functions in C with your own versions. And all of the functions to which you pass a nul-terminated string. And getting adequate performance. Could you explain a bit more about your problem so that you don't get shouted at for trying to save a byte by allocating two at the beginning of the string please? Nov 20, 2009 at 17:47
  • 1
    Indeed, this is clearly premature optimization. Even if many language interpreters define their own string types, in many cases they still include the 0 byte because it's negligible compared to even length field/malloc overhead.
    – intgr
    Nov 20, 2009 at 17:51
  • 4
    Why, in the name of Charles Babbage, are you concerned with saving space like that? Unless you're working on some extremely strange problem that you haven't mentioned, you don't need to do this, and you're going to cause far more problems for yourself than you solve. Nov 20, 2009 at 18:04

7 Answers 7

1

This is pretty awful, but you can explicitly specify the length of every character array constant:

char my_constant[6] = "foobar";
assert(sizeof my_constant == 6);

wchar_t wide_constant[6] = L"foobar";
assert(sizeof wide_constant == 6*sizeof(wchar_t));
10
  • 3
    You could make it a macro: #define NEW_STRING(var, val) char var[sizeof(val)-1] = val
    – mbauman
    Nov 20, 2009 at 17:38
  • 1
    If your own string type is defined as a struct of {length; pointer} then you might as well use length=0 and pointer=NULL -- accessing pointer would be invalid anyway, because there are no characters to read.
    – intgr
    Nov 20, 2009 at 17:39
  • @intgr: With your code I am getting: "error: wide character array initialized from non-wide string" @Sinan Ünür: Even empty strings are NUL-terminated. That means that sizeof() - 1 results 0 as expected. In my case AppendSubString() would stop right away and does not add anything. @Matt B.: I am aware of it but actually it does not solve my problem. The AppendString() macro is used in my code like this everywhere: AppendString(&string, "Hello World"); As you can see, I am not using any variables for this purpose.
    – user206268
    Nov 20, 2009 at 18:15
  • @timn: Oops, I forgot that you need to prefix L"" to wide-character literals. I updated my example.
    – intgr
    Nov 20, 2009 at 18:18
  • @timn: In the case of the string literal (like you cite AppendString(&string, "Hello World")), the "Hello World" is not stored on the stack (at least for x86*). It's stored in the read-only segment of the executable, and AppendString receives a pointer to that segment. So in this case, you're not increasing your stack size, but rather executable size. I certainly hope one byte won't break you in either case, but this isn't even the one you're worried about.
    – mbauman
    Nov 20, 2009 at 19:27
1

I understand you're only dealing with strings declared in your program:

 ....
 char str1[10];
 char str2[12];
 ....

and not with text buffers you allocate with malloc() and friends otherwise sizeof is not going to help you.

Anyway, i would just think twice about removing the \0 at the end: you would lose the compatibility with C standard library functions.

Unless you are going to rewrite any single string function for your library (sprintf, for example), are you sure you want to do it?

2
  • I am not using heap allocated strings because that is exactly what my string library is for. Manually allocating memory is too dangerous as there is always the risk of buffer overflows. As stated above rewriting the string functions was not really hard. Thanks to C99 I might use a simple hack in order to still keep compatibility to Glibc functions: char tmp[string->len + 1]; tmp[string->len + 1] = '\0'; printf("%s", tmp);
    – user206268
    Nov 20, 2009 at 18:44
  • I'm sure you have good reasons for doing this but, if I understand how buffer overrun exploits work, having the string on the stack is more dangerous than having them allocated on the heap. About adding the '\0' at the end, I don't see how the content of string is copied into temp, and I think that you're using a gcc extension rather than C99.
    – Remo.D
    Nov 20, 2009 at 23:17
0

I can't remember the details, but when I do

char my_constant[5]

it is possible that it will reserve 8 bytes anyway, because some machines can't address the middle of a word.

It's nearly always best to leave this sort of thing to the compiler and let it handle the optmisation for you, unless there is a really really good reason to do so.

1
  • 1
    It's called "alignment" (also "internal fragmentation"). It's true that throwing away the NUL byte does not reduce most strings, but when it does, it reduces by the alignment size. So, on average, each string would still consume 1 byte less memory.
    – intgr
    Nov 20, 2009 at 18:02
0

If you're not using any of the Standard Library function that deal with strings you can forget about the NUL terminating byte.

No strlen(), no fgets(), no atoi(), no strtoul(), no fopen(), no printf() with the %s conversion specifier ...

Declare your "not quite C strings" with just the needed space;

struct NotQuiteCString { /* ... */ };

struct NotQuiteCString variable;
variable.data = malloc(5);
data[0] = 'H'; /* ... */ data[4] = 'o'; /* "hello" */
2
  • Basically this is more or less what I am currently doing. It might be indeed better to keep NUL-termination generally turned on but perhaps there is something like a pragma allowing me to enable/disable the NUL terminating byte for a given code part, i.e. my string library functions?
    – user206268
    Nov 20, 2009 at 18:55
  • 1
    Just don't use it. You use arrays of int everyday, lots of time a day ... and you never even once used a terminating int. Do the same with char arrays (when I do that I specifically "sign" my chars: signed char or unsigned char: to me, only plain char can be C strings).
    – pmg
    Nov 20, 2009 at 19:02
0

Indeed this is only in case you are really low in memory. Otherwise I don't recommend to do so.

It seems most proper way to do thing you are talking about is:

  • To prepare some minimal 'listing' file in a form of:
    string1_constant_name "str1"
    string2_constant_name "str2"
    ...
  • To construct utility which processes your file and generates declarations such as
    const char string1_constant[4] = "str1";

Of course I'd not recommend to do this by hands, because otherwise you can get in trouble after any string change.

So now you have both non-terminated strings because of fixed auto-generated arrays and also you have sizeof() for every variable. This solution seems acceptable.

Benefits are easy localization, possibility to add some level of checks to make this solution risk lower and R/O data segment savings.

Drawback is need to include all of such string constants in every module (as include to keep sizeof() known). So this only makes sense if your linker merges such symbols (some don't).

1
  • Nice idea! Unfortunately it requires me to always pull the whole code through a "preprocessor" before compiling. If there is really no such option in GCC to turn off NUL termination, I will stick with my current approach.
    – user206268
    Nov 20, 2009 at 19:00
0

Aren't these similar to Pascal-style strings, or Hollerith Strings? I think this is only useful if you actually want the String data to preserve NULLs, in which you're really pushing around arbitrary memory, not "strings" per se.

1
  • Yes, I am using them in a similar way like Hollerith strings. In my code it is defined as follows: typedef struct { unsigned int len; unsigned int maxLen; char buf[0]; } There are other advantages as well. Wikipedia (taken from "String literal") says: * eliminates text searching (for the delimiter character) and therefore requires significantly less overhead * avoids the (100% programmer induced) problem of delimiter collision * enables the inclusion of metacharacters that might otherwise be mistaken as commands * can be used for quite effective data compression of plain text strings
    – user206268
    Nov 29, 2009 at 15:09
0

The question uses false assumptions - it assumes that storing the length (e.g. implicitly by passing it as a number to a function) incurs no overhead, but that's not true.

While one might save space by not storing the 0-byte (or wchar), the size must be stored somewhere, and the example hints that it is passed as a constant argument to a function somewhere, which almost certainly takes more space, in code. If the same string is used multiple times, the overhead is per use, not per-string.

Having a wrapper that uses strlen to determine the length of a string and isn't inlined will almost certainly save more space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.