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

  1. Oodini

    Oodini Guest


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

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

    FILE *outfile;
    char *string1, *string2;
    float value=3.74;

    string1 = "turlutu\r\n";

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

    // sprintf(string2,"%f",value);

    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

    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
    ebp=0022E908 esp=0022E8FC program=G:\oodini\Moteurs de
    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. Advertisements

  2. 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
    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
    1. Advertisements

  3. 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
    Of course you did. Where did you think you were storing this
    information? string2 could point anywhere or nowhere.
    Martin Ambuhl, Jun 26, 2003
  4. 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)
    /* 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;
    return n;

    <snip stackdump and arbitrary information>
    Pieter Droogendijk, Jun 26, 2003
  5. Oodini

    John Smith Guest

    Aren't you the expert.... How about a polite reply?
    John Smith, Jun 26, 2003
  6. Oodini

    Oodini Guest

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

    Very sorry...
    Oodini, Jun 26, 2003
  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
  8. Oodini

    Dan Pop Guest

    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 Pop, Jun 26, 2003
  9. Oodini

    CBFalconer Guest

    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
    CBFalconer, Jun 26, 2003
  10. Oodini

    CBFalconer Guest

    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".
    CBFalconer, Jun 26, 2003
  11. Oodini

    CBFalconer Guest

    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". :)
    CBFalconer, Jun 26, 2003
  12. People have allready answered your question so I'll just make an analogy.


    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

    Undefined behaviour can also cause the appearance of stupid analogies.
    Thomas Stegen, Jun 26, 2003
  13. ROFL.

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

    First thing: Don't top post. you'll only get flamed.
    Mark McIntyre, Jun 26, 2003
  14. Oodini

    Mark Gordon Guest

    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

    The important distinction being that with implementation defined the
    behaviour is defined, documented and generally something useful.
    Mark Gordon, Jun 27, 2003
  15. 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
    - John Nordberg
    Joona I Palaste, Jun 27, 2003
  16. Oodini

    John Smith Guest


    I noticed. Thanks anyway :)
    John Smith, Jun 27, 2003
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.