F
Francois Grieu
// This post is a valid C program
#include <stdio.h>
#include <stdlib.h>
typedef struct tFoo
{
int *fPtr;
int fIntA;
int fIntB;
} tFoo;
tFoo *gFoo;
// assuming inNbFoo>0 and inNbFoo<=SIZE_MAX/sizeof(tFoo)
// allocate inNbFoo tFoo and set all fields to NULL or 0
void setfoo(int inNbFoo)
{
gFoo = calloc(inNbFoo,sizeof(*gFoo));
// can the following be supressed?
if (gFoo!=NULL)
while(--inNbFoo)
gFoo[inNbFoo].fPtr = NULL;
}
int main(void)
{
setfoo(7);
if (gFoo!=NULL && gFoo[0].fPtr!=NULL)
printf("wrongo\n");
return 0;
}
/*
For strict portability, it appears that the while loop is mandatory,
in case NULL is not represented by the all-zero bit pattern.
But is there a portable way to determine at compile time that it
is not needed, which is the case on most machines, and/or
otherwise help the compiler to remove the while loop ?
TIA,
Francois Grieu (reposted with correction of comments)
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct tFoo
{
int *fPtr;
int fIntA;
int fIntB;
} tFoo;
tFoo *gFoo;
// assuming inNbFoo>0 and inNbFoo<=SIZE_MAX/sizeof(tFoo)
// allocate inNbFoo tFoo and set all fields to NULL or 0
void setfoo(int inNbFoo)
{
gFoo = calloc(inNbFoo,sizeof(*gFoo));
// can the following be supressed?
if (gFoo!=NULL)
while(--inNbFoo)
gFoo[inNbFoo].fPtr = NULL;
}
int main(void)
{
setfoo(7);
if (gFoo!=NULL && gFoo[0].fPtr!=NULL)
printf("wrongo\n");
return 0;
}
/*
For strict portability, it appears that the while loop is mandatory,
in case NULL is not represented by the all-zero bit pattern.
But is there a portable way to determine at compile time that it
is not needed, which is the case on most machines, and/or
otherwise help the compiler to remove the while loop ?
TIA,
Francois Grieu (reposted with correction of comments)
*/