sprintf

O

Oodini

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
 
J

Joona I Palaste

Oodini said:
I've got some troubles by using sprintf on gcc.
Here is a sample program:
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 ([email protected]) ---------------------------\
| 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
 
M

Martin Ambuhl

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);
}

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

Pieter Droogendijk

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);
}

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>
 
J

John Smith

[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.
 
D

Dan Pop

In said:
On 26 Jun 2003 07:55:04 GMT,


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
 
C

CBFalconer

Ed said:
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.
 
C

CBFalconer

John said:
[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".
 
C

CBFalconer

Joona said:
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". :)
 
T

Thomas Stegen

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.
 
M

Mark Gordon

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.
 
J

Joona I Palaste

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 ([email protected]) ---------------------------\
| 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
 
J

John Smith

Mark McIntyre said:
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 :)
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top