This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |
Other format: | [Raw text] |
- if (*len > 0) + if (*len == 0) + { + /* A zero-length Fortran string is "". */ + char * tmp = internal_malloc_size (1); + tmp[0] = '\0'; + *dest = tmp; + } + else
Do you zero-terminate the string because you don't want to allocate zero memory? I don't know if allocating a pointer to zero memory would work, though.
I don't think you can portably call malloc(0) and expect a non-NULL pointer out of it. From http://www.opengroup.org/onlinepubs/009695399/functions/malloc.html :If the size of the space requested is 0, the behavior is implementation-defined: the value returned shall be either a null pointer or a unique pointer.Also, internal_malloc_size catches this case and returns NULL, so I think it's more reasonable to special-case zero right there. (And I do set the allocated memory to zero just to make sure we don't end up with unassigned memory.)
An alternative would be to have a static char zero_length_string[0]; // Maybe [1] at file scope and then do *dest = zero_length_string; This would evade the unnecessary allocations.
Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
---|---|---|
Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |