T
tweak
I have been messing around with buffers, and
I found it peculiar that the code below will
run without a segmentation fault.
As far as I know, overwriting the allocated space
from a call to malloc() results in undefined
behavior.
Any ideas why overwriting the dynamically
assigned space doesn't cause a segmentation
fault?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int
main(void)
{
char *ptr = NULL;
char buffer[256]; /* intentionally did not initialize */
int y = 0;
if ( ( ptr = malloc(1) ) == 0 ) {
perror("malloc() problem");
exit(1);
}
for ( int i; i < 255; i++ )
buffer = 'Z';
while ( (ptr[y] = buffer[y]) != '\0' )
y++;
(void)printf("ptr is: %s\n", ptr);
free(ptr);
return 0;
}
I found it peculiar that the code below will
run without a segmentation fault.
As far as I know, overwriting the allocated space
from a call to malloc() results in undefined
behavior.
Any ideas why overwriting the dynamically
assigned space doesn't cause a segmentation
fault?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int
main(void)
{
char *ptr = NULL;
char buffer[256]; /* intentionally did not initialize */
int y = 0;
if ( ( ptr = malloc(1) ) == 0 ) {
perror("malloc() problem");
exit(1);
}
for ( int i; i < 255; i++ )
buffer = 'Z';
while ( (ptr[y] = buffer[y]) != '\0' )
y++;
(void)printf("ptr is: %s\n", ptr);
free(ptr);
return 0;
}