S
somenath
somenath said:On Jun 18, 4:54 pm, Ben Bacarisse <[email protected]> wrote:#include <stdio.h>
#include <stdlib.h>
void send(char *str);
int main(void)
{
char s[512];
sprintf(s, "%p", malloc(1024));
send(s);
return 0;
}
void send(char *str)
{
void *p;
sscanf(str, "%p", &p);
free(p);
}Could you please explain why it won’t show undefined behavior?
When I ran this code I get
Segmentation fault (core dumped).
Exactly the code above gives you a segmentation fault? If so, what
compiler it it? I can only point to what the standard says about the
%p format which is that you can convert a pointer to text and back
again.
I am running the bellow code
$ cat simple.c
#include <stdio.h>
#include <stdlib.h>
void send(char *str);
int main(void)
{
char s[512];
sprintf(s, "p", malloc(1024));
send(s);
return 0;
}
void send(char *str)
{
void *p;
sscanf(str, "%p", &p);
free(p);
}
I think I missed the % in sprintf(s, "p", malloc(1024));
I am using
gcc version 3.3.1 (cygming special)