char *p; placement and mysterious segfault on sprintf(p, "%d", 3);

Miernik miernik@ctnet.pl
Mon May 19 09:36:00 GMT 2003


In article <3EC7A113.1020802@btinternet.com>, Graham Stott wrote:

> Examine your code where do you think sprintf is writing
> the result?

Can you elaborate a little, because I do not see the problem. 

I am looking at the example in PRINTF(3) manpage. 

              make_message(const char *fmt, ...) {
                 /* Guess we need no more than 100 bytes. */
                 int n, size = 100;
                 char *p;
                 va_list ap;
                 if ((p = malloc (size)) == NULL)
                    return NULL;
                 while (1) {
                    /* Try to print in the allocated space. */
                    va_start(ap, fmt);
                    n = vsnprintf (p, size, fmt, ap);

p is here defined the same way, and used in vsnprintf. So why it may 
not be used in a simple sprintf?


And why it doesn't segfault when I change the order of declarations?
Or when I insert another declaration of "int d;" (d is not used 
anywhere later)?

-- 
    Miernik         ________________________ jabber:miernik@jabber.gda.pl
__ ICQ: 4004001 ___/__ tel: +48608233394 __/      mailto:miernik@ctnet.pl
Do not use Paypal. They sometimes shut down accounts and steal users money!
http://www.paypalwarning.com/



More information about the Gcc-help mailing list