J
jois.de.vivre
Hello, I was wondering if the following code was ok:
// ----------- start code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv)
{
char buf[10];
memset(buf, 'a', sizeof(buf));
int i=0;
char *ptr = &buf[0];
while (1) {
if (i == sizeof(buf))
break;
char byte = *ptr++; /* *** (1) *** */
printf("%c", byte);
++i;
}
printf("\n");
return 0;
}
// ----------- end code
In the last iteration of the loop, ptr will point past the last byte
of memory allocated for this array. This is a contrived example, but
I just wanted to know whether it's ok to point past the valid memory
for this array as long as I don't read or modify the contents of
memory at that address afterwards. On the surface it seems like this
code won't cause me any problems.
Thanks,
Prashant
// ----------- start code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv)
{
char buf[10];
memset(buf, 'a', sizeof(buf));
int i=0;
char *ptr = &buf[0];
while (1) {
if (i == sizeof(buf))
break;
char byte = *ptr++; /* *** (1) *** */
printf("%c", byte);
++i;
}
printf("\n");
return 0;
}
// ----------- end code
In the last iteration of the loop, ptr will point past the last byte
of memory allocated for this array. This is a contrived example, but
I just wanted to know whether it's ok to point past the valid memory
for this array as long as I don't read or modify the contents of
memory at that address afterwards. On the surface it seems like this
code won't cause me any problems.
Thanks,
Prashant