Operating System - HP-UX
1833758 Members
2380 Online
110063 Solutions
New Discussion

Re: Char Buffer Initialization

 
SOLVED
Go to solution
Alfonso_15
Advisor

Char Buffer Initialization

Good Morning:
I am working gcc 64, for HP11.11. I am migrating a program, and I find several run problems in the handle of variables declared as char pointer:
char *buffer;

Exist in gcc any compile option for initialize the buffer when is declared: I means, when find the instruction
char *buffer;
autmatically put in buffer[0]='\0'
Thanks a lot for any help.
alfonsoarias
2 REPLIES 2
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Char Buffer Initialization

No and this is absolutely not a good idea. At best, it would be code that is not portable.

You could, of course,
char *buffer = {""};

to do this but this is of limited value because all you have done is created a 1-byte string to hold the NUL. If you stop to think for a moment you will realize that your request really doesn't make a lot of sense because no intelligent space allocation can be done. I suspect that you are testing the string for length 0 and if so then allocating space. A better way is this (which will be completely portable):

char *buffer = NULL;


if (buffer == NULL)
{
buffer = (char *) malloc((size_t) (nbytes + 1));
}

If it ain't broke, I can fix that.
Alfonso_15
Advisor

Re: Char Buffer Initialization

Thanks Clay. Now is clear for me.
alfonsoarias