sprintf

Discussion in 'C Programming' started by Oodini, Jun 26, 2003.

  1. Oodini

    Oodini Guest

    Hello,

    I've got some troubles by using sprintf on gcc.
    Here is a sample program:

    ----------------------------------------------------------------
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    main()
    {
    FILE *outfile;
    char *string1, *string2;
    float value=3.74;

    string1 = "turlutu\r\n";

    if ((outfile = fopen("postscript.ps","w")) == NULL)
    printf("Ksssss !!");

    printf("%f",value);
    // sprintf(string2,"%f",value);
    fputs(string1,outfile);
    fclose(outfile);
    }
    ----------------------------------------------------------------

    As soon as I uncomment sprintf, I got an error at the runtime (no
    problem during the compilation):

    4 [main] a 1796 handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
    18482 [main] a 1796 open_stackdumpfile: Dumping stack trace to
    a.exe.stackdump

    Compiled on gcc 3.2 2002/09/27
    Ran on Windows 2000 SP3

    Below is the stack dump...

    Exception: STATUS_ACCESS_VIOLATION at eip=610B73CF
    eax=61009E03 ebx=0A040498 ecx=00000001 edx=0022FE30 esi=0A040498
    edi=61009E03
    ebp=0022E908 esp=0022E8FC program=G:\oodini\Moteurs de
    rendu\zob\libsipp\a.exe
    cs=001B ds=0023 es=0023 fs=0038 gs=0000 ss=0023
    Stack trace:
    Frame Function Args
    0022E908 610B73CF (61009E03, 0A040498, 00000001, 00000003)
    0022E948 610C5A20 (0022FE30, 0022FDA0, 00000000, 00000001)
    0022E968 610BF602 (0022FE30, 0022FDA0, EF5C2900, 00004000)
    0022FDF8 610BFF21 (610CB050, 0022FE30, 004010B4, 0022FEC0)
    0022FE18 610BF70C (0022FE30, 004010B4, 0022FEB8, 00000000)
    0022FEA8 610B91AD (61009E03, 004010B4, 20000000, 400DEB85)
    0022FEE0 0040113A (00000001, 0A0403D8, 0A040328, 00000001)
    0022FF40 61007408 (610D1F58, FFFFFFFE, 0000002C, 610D1E7C)
    0022FF90 610076ED (00000000, 00000000, 8042F070, 00000000)
    0022FFB0 00402722 (004010B8, 037F0009, 0022FFF0, 77E9847C)
    0022FFC0 0040103C (00000000, 00000000, 7FFDF000, 00000000)
    0022FFF0 77E9847C (00401000, 00000000, 000000C8, 00000100)
    End of stack trace
     
    Oodini, Jun 26, 2003
    #1
    1. Advertising

  2. Oodini <> scribbled the following:
    > Hello,


    > I've got some troubles by using sprintf on gcc.
    > Here is a sample program:


    > ----------------------------------------------------------------
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>


    > main()
    > {
    > FILE *outfile;
    > char *string1, *string2;
    > float value=3.74;
    >
    > string1 = "turlutu\r\n";
    >
    > if ((outfile = fopen("postscript.ps","w")) == NULL)
    > printf("Ksssss !!");


    > printf("%f",value);


    I don't see you allocating any memory for string2 to point at anywhere.
    In fact, the value of string2 is indeterminate during your entire
    program.

    > // sprintf(string2,"%f",value);
    > fputs(string1,outfile);
    > fclose(outfile);
    > }
    > ----------------------------------------------------------------


    > As soon as I uncomment sprintf, I got an error at the runtime (no
    > problem during the compilation):


    Well _of course_ you do. What do you think scribbling over non-
    allocated memory does to your system?

    (snip irrelevant information)

    You'll want to have a look at malloc() and free() some day.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "The trouble with the French is they don't have a word for entrepreneur."
    - George Bush
     
    Joona I Palaste, Jun 26, 2003
    #2
    1. Advertising

  3. Oodini <> wrote (26 Jun 2003) in
    news: / comp.lang.c:

    > Hello,
    >
    > I've got some troubles by using sprintf on gcc.


    Of course you did.

    > Here is a sample program:
    >
    > ----------------------------------------------------------------
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > main()
    > {
    > FILE *outfile;
    > char *string1, *string2;
    > float value=3.74;
    >
    > string1 = "turlutu\r\n";
    >
    > if ((outfile = fopen("postscript.ps","w")) == NULL)
    > printf("Ksssss !!");
    >
    > printf("%f",value);
    > // sprintf(string2,"%f",value);


    With this line uncommented, string2 is pointed to your car's engine
    ignition system. The results of writing a real there are implementation
    defined.

    > fputs(string1,outfile);
    > fclose(outfile);
    > }
    > ----------------------------------------------------------------
    >
    > As soon as I uncomment sprintf, I got an error at the runtime (no
    > problem during the compilation):


    Of course you did. Where did you think you were storing this
    information? string2 could point anywhere or nowhere.




    --
    Martin Ambuhl
    Returning soon to the
    Fourth Largest City in America
     
    Martin Ambuhl, Jun 26, 2003
    #3
  4. On Thu, 26 Jun 2003 09:49:28 +0200, Oodini wrote:
    > Hello,
    >
    > I've got some troubles by using sprintf on gcc.
    > Here is a sample program:
    >
    > ----------------------------------------------------------------
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>
    >
    > main()
    > {
    > FILE *outfile;
    > char *string1, *string2;
    > float value=3.74;
    >
    > string1 = "turlutu\r\n";
    >
    > if ((outfile = fopen("postscript.ps","w")) == NULL)
    > printf("Ksssss !!");
    >
    > printf("%f",value);
    > // sprintf(string2,"%f",value);
    > fputs(string1,outfile);
    > fclose(outfile);
    > }
    > ----------------------------------------------------------------
    >
    > As soon as I uncomment sprintf, I got an error at the runtime (no
    > problem during the compilation):


    sprintf doesn't allocate memory. Try string1 = malloc(50); before
    writing to it. That's a very icky solution though. The function asprintf
    prints to a string it allocates. It's a GNU extension though, not C or
    POSIX, so you might do better using something like this (I think I
    ripped this from the asprintf manual page once):

    int myprintf(char **p, const char *fmt, ...)
    {
    /* Guess we need no more than 100 bytes. */
    int size = 100, n = -1;
    va_list ap;
    *p = NULL;
    if ((*p = malloc (size)) == NULL)
    return -1;
    va_start(ap, fmt);
    while (1) {
    /* Try to print in the allocated space. */
    n = vsnprintf (*p, size, fmt, ap);
    /* If that worked, return the string. */
    if (n > -1 && n < size)
    break;
    /* Else try again with more space. */
    if (n > -1) /* glibc 2.1 */
    size = n+1; /* precisely what is needed */
    else /* glibc 2.0 */
    size *= 2; /* twice the old size */
    if ((*p = realloc (*p, size)) == NULL) {
    n = -1;
    break;
    }
    }
    va_end(ap);
    return n;
    }


    <snip stackdump and arbitrary information>

    --
    You have a deep interest in all that is artistic.
     
    Pieter Droogendijk, Jun 26, 2003
    #4
  5. Oodini

    John Smith Guest

    Aren't you the expert.... How about a polite reply?


    "Joona I Palaste" <> wrote in message
    news:bde8so$5fs$...
    > Oodini <> scribbled the following:
    > > Hello,

    >
    > > I've got some troubles by using sprintf on gcc.
    > > Here is a sample program:

    >
    > > ----------------------------------------------------------------
    > > #include <stdio.h>
    > > #include <stdlib.h>
    > > #include <string.h>

    >
    > > main()
    > > {
    > > FILE *outfile;
    > > char *string1, *string2;
    > > float value=3.74;
    > >
    > > string1 = "turlutu\r\n";
    > >
    > > if ((outfile = fopen("postscript.ps","w")) == NULL)
    > > printf("Ksssss !!");

    >
    > > printf("%f",value);

    >
    > I don't see you allocating any memory for string2 to point at anywhere.
    > In fact, the value of string2 is indeterminate during your entire
    > program.
    >
    > > // sprintf(string2,"%f",value);
    > > fputs(string1,outfile);
    > > fclose(outfile);
    > > }
    > > ----------------------------------------------------------------

    >
    > > As soon as I uncomment sprintf, I got an error at the runtime (no
    > > problem during the compilation):

    >
    > Well _of course_ you do. What do you think scribbling over non-
    > allocated memory does to your system?
    >
    > (snip irrelevant information)
    >
    > You'll want to have a look at malloc() and free() some day.
    >
    > --
    > /-- Joona Palaste () ---------------------------\
    > | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    > | http://www.helsinki.fi/~palaste W++ B OP+ |
    > \----------------------------------------- Finland rules! ------------/
    > "The trouble with the French is they don't have a word for entrepreneur."
    > - George Bush
     
    John Smith, Jun 26, 2003
    #5
  6. Oodini

    Oodini Guest

    Thanks a alot all for your numerous answers to my stupid problem. :)

    Very sorry...
     
    Oodini, Jun 26, 2003
    #6
  7. Oodini

    John Smith Guest

    [snipped, otherwise I'd be accused of top-posting again]

    I see what you mean about Dan Pop's replies.

    If the simple questions irritate you guys, just stop reading them, not
    everyone has ten thousand years experience.
     
    John Smith, Jun 26, 2003
    #7
  8. Oodini

    Dan Pop Guest

    In <-hamburg.de> Daniel Haude <-hamburg.de> writes:

    >On 26 Jun 2003 07:55:04 GMT,
    > Joona I Palaste <> wrote
    > in Msg. <bde8so$5fs$>
    >>> As soon as I uncomment sprintf, I got an error at the runtime (no
    >>> problem during the compilation):

    >>
    >> Well _of course_ you do.

    >
    >Sadly, it's not so "of course". Ever programmed in DOS? Anything worked,
    >sort of. When I ported some of my early C products from DOS to Linux I was
    >close to thinking that C just desn't work on Linux. Pretty much all I'd
    >written so far was a single big segfault.


    DOS implementations did their best to catch the error. The first bytes
    of the default data segment contained a certain "signature" (usually the
    vendor's copyright message). If you wrote something through a null
    pointer (and, with a bit of luck, your uninitialised pointers could be
    null pointers) you'd destroy that signature. As part of the program
    termination procedure, the integrity of the signature was checked and the
    (rather cryptical) run time error message "null pointer assignment"
    (or something like this) was displayed if it was found to be corrupted.
    It was an excellent clue that your code needed to be fixed, even if
    it appeared to work fine.

    Not as effective as a well deserved segfault, but quite useful.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jun 26, 2003
    #8
  9. Oodini

    CBFalconer Guest

    Ed Morton wrote:
    > "John Smith" <> wrote in message
    >
    > > Aren't you the expert.... How about a polite reply?

    >
    > Welcome to CLC where we put the "fun" in "dysfunctional". Joonas
    > reply was about as polite as you're liable to see! Maybe I've
    > become immune, but it didn't even raise my eyebrow. Some posters
    > have described this newsgroup as "The angst of a thousand
    > teenagers", "starved", and "rabid", but if you can ignore (or
    > find a way to enjoy!) the social issues, it's worth sticking
    > around since the actual content of the responses is always,
    > eventually, accurate.


    With the proviso that the question is on-topic only. Which is a
    major reason for complaining both about off-topic queries and
    about informative replies to such. Redirection, however, is
    acceptable.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Jun 26, 2003
    #9
  10. Oodini

    CBFalconer Guest

    John Smith wrote:
    >
    > [snipped, otherwise I'd be accused of top-posting again]


    Which is an incredibly childish thing to do, not to mention rude.
    Most adults are capable of conforming to the rules of their
    society, especially after the underlying purposes of those rules
    are available to them. The spoiled little children throw
    tantrums, kick things, and scream "I won't".

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Jun 26, 2003
    #10
  11. Oodini

    CBFalconer Guest

    Joona I Palaste wrote:
    > Dan Pop <> scribbled the following:
    > > Oodini <> writes:

    >
    > >> I've got some troubles by using sprintf on gcc.
    > >> Here is a sample program:

    >
    > > Your stupid mistake is clearly addressed by the FAQ. Why
    > > didn't you read it *before* posting?

    >
    > You see? *This* is what I would call impolite. =)


    However, in this case, it only requires excising one word to reach
    clc normality. I would put it in the bin with "Dans conciliatory
    messages". :)

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
     
    CBFalconer, Jun 26, 2003
    #11
  12. People have allready answered your question so I'll just make an analogy.

    Sorry

    Meet Tim the teacher. He has thia special class where he hands out
    paper slips to his pupils which details the page in a note book pupils
    are to write in. This is just a small experiement so Tim can see how
    much sense it will make when all the pages have been used. Even though
    pages are chosen at random Tim must take care so he does not give out an
    already used page number. Also, Tim has only a few paper slips which he
    need to reuse.

    Meet Bob the bully. He is given a paper slip by Tim the teacher and
    proceeds to write his best writings to date. Which is quite good seeing
    as Bob is unlike other bullies in that he likes writing..

    Meet Willie the weenie. He is also given a paper slip by Tim the
    teacher. Willie open the note book and finds that someone has already
    written on his page. "Stupid person, I'll just remove their writing
    and write there myself." he thinks. But lo and behold, it turns out that
    Tim forgot to put a new number on his reuseable paper slip and Willie is
    in fact erasing Bobs text.

    Bob the bully is of course not happy about this and uses his superior
    strength to punch Willie the weenie in the stomach. This causes Willie
    to dump core.

    Tim is not happy about this of course and starts shouting and screaming
    at Bob.

    So be careful where you point your pointers, you might cause people
    to shout and scream. This was aptly demonstrated by several regulars in
    this thread. So it really seems undefined behaviour can do just about
    anything, even make people make noises they otherwise would not have
    made.

    Undefined behaviour can also cause the appearance of stupid analogies.


    --
    Thomas.
    "What is the future c existence which does not do in all languages"
     
    Thomas Stegen, Jun 26, 2003
    #12
  13. On Thu, 26 Jun 2003 09:30:13 +0100, in comp.lang.c , "John Smith"
    <> wrote:

    >Aren't you the expert.... How about a polite reply?


    ROFL.

    You're new round here, aren't you?

    First thing: Don't top post. you'll only get flamed.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Jun 26, 2003
    #13
  14. Oodini

    Mark Gordon Guest

    On 26 Jun 2003 09:04:22 GMT
    Joona I Palaste <> wrote:

    > Martin Ambuhl <> scribbled the following:
    > > Of course you did.

    >
    > ...
    >
    > > With this line uncommented, string2 is pointed to your car's engine
    > > ignition system. The results of writing a real there are
    > > implementation defined.

    >
    > ...
    >
    > > Of course you did. Where did you think you were storing this
    > > information? string2 could point anywhere or nowhere.

    >
    > Is this a more polite reply then?


    I would say so, but I also think it is less accurate. Correct me if I'm
    wrong (as if someone would not) but isn't writing to an uninitialised
    pointer undefined behaviour rather than implementation defined
    behaviour?

    The important distinction being that with implementation defined the
    behaviour is defined, documented and generally something useful.
    --
    Mark Gordon
    Paid to be a Geek & a Senior Software Developer
    Although my email address says spamtrap, it is real and I read it.
     
    Mark Gordon, Jun 27, 2003
    #14
  15. Mark Gordon <> scribbled the following:
    > On 26 Jun 2003 09:04:22 GMT
    > Joona I Palaste <> wrote:
    >> Martin Ambuhl <> scribbled the following:
    >> > With this line uncommented, string2 is pointed to your car's engine
    >> > ignition system. The results of writing a real there are
    >> > implementation defined.

    >>
    >> Is this a more polite reply then?


    > I would say so, but I also think it is less accurate. Correct me if I'm
    > wrong (as if someone would not) but isn't writing to an uninitialised
    > pointer undefined behaviour rather than implementation defined
    > behaviour?


    > The important distinction being that with implementation defined the
    > behaviour is defined, documented and generally something useful.


    I believe Martin was *not* saying that writing to non-allocated memory
    is implementation defined. He said that writing to your car's engine
    ignition system is implementation defined. Whether non-allocated memory
    is in your car's engine ignition system or somewhere else is undefined.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "Parthenogenetic procreation in humans will result in the founding of a new
    religion."
    - John Nordberg
     
    Joona I Palaste, Jun 27, 2003
    #15
  16. Oodini

    John Smith Guest

    "Mark McIntyre" <> wrote in message
    news:...
    > On Thu, 26 Jun 2003 09:30:13 +0100, in comp.lang.c , "John Smith"
    > <> wrote:
    >
    > >Aren't you the expert.... How about a polite reply?

    >
    > ROFL.
    >
    > You're new round here, aren't you?
    >
    > First thing: Don't top post. you'll only get flamed.
    >
    > --
    > Mark McIntyre
    > CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    > CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
    >
    >
    > ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet

    News==----
    > http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000

    Newsgroups
    > ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption

    =---

    I noticed. Thanks anyway :)
     
    John Smith, Jun 27, 2003
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shea martin

    sprintf

    shea martin, Sep 2, 2004, in forum: Java
    Replies:
    5
    Views:
    3,631
    shea martin
    Sep 3, 2004
  2. Pep
    Replies:
    5
    Views:
    4,104
  3. CJ
    Replies:
    1
    Views:
    1,393
    Davlet Panech
    Oct 28, 2003
  4. Mike Chirico
    Replies:
    2
    Views:
    3,920
    Grumble
    Nov 19, 2003
  5. Pilatus
    Replies:
    3
    Views:
    563
    Pilatus
    Dec 18, 2003
Loading...

Share This Page