C/C++ Option to Initialize Variables?

Alec Teal a.teal@warwick.ac.uk
Mon Feb 18 12:02:00 GMT 2013


On 18/02/13 11:40, Jeffrey Walton wrote:
> Hi All,
>
> http://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html#C-Dialect-Options
>
> Is there an option to initialize variables to known values in a C/C++ program?
>
> My use case is 'debug' builds and finding use of uninitialized values
> that get lucky by being 0 most of the time. For example:
>
> void DoSomeithWithFoo(FOO** ppf) {
>    if(ppf && *ppf == NULL) {
>      *ppf = new FOO;
>      ...
>    }
> }
>
> FOO* p;
> DoSomeithWithFoo(&p);
>
> So I would like something that initializes to known, but non-NULL,
> such as 0xCDCDCDCD or 0xFDFDFDFD (similar to Visual Studio behavior).
>
> Jeff
>
Probably not, put =0, if I say "int x;" I am just saying there is an 
int, it's name is x, deal with it. I may assign to it later, sticking =0 
at the end implicitly wouldn't be good for anything really.
However! calloc initializes the memory to zero change malloc(size to 
calloc(1,size and you're done.

Does that help?

Alec



More information about the Gcc mailing list