T
Thomas Stegen
Is the following code valid? As I read the standard it is valid.
The only thing that can be difficult in general is finding the
minimum size of LARGE_ENOUGH. Or am I missing something?
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define LARGE_ENOUGH 4096
int main(void)
{
void *ptr;
long *l;
short *s;
unsigned i = 0;
ptr = malloc(LARGE_ENOUGH);
if(ptr == NULL)
{
puts("malloc failed.");
return EXIT_FAILURE;
}
l = ptr;
*l = LONG_MAX;
s = ptr;
for(i = 0; (void *)(l + 1) < (void *)s; s++)
{/*empty*/}
*s = SHRT_MAX;
printf("l = %ld, s = %hd.\n", *l, *s);
return 0;
}
The only thing that can be difficult in general is finding the
minimum size of LARGE_ENOUGH. Or am I missing something?
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define LARGE_ENOUGH 4096
int main(void)
{
void *ptr;
long *l;
short *s;
unsigned i = 0;
ptr = malloc(LARGE_ENOUGH);
if(ptr == NULL)
{
puts("malloc failed.");
return EXIT_FAILURE;
}
l = ptr;
*l = LONG_MAX;
s = ptr;
for(i = 0; (void *)(l + 1) < (void *)s; s++)
{/*empty*/}
*s = SHRT_MAX;
printf("l = %ld, s = %hd.\n", *l, *s);
return 0;
}