Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   sprintf (http://www.velocityreviews.com/forums/t313836-sprintf.html)

Oodini 06-26-2003 07:49 AM

sprintf
 
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


Joona I Palaste 06-26-2003 07:55 AM

Re: sprintf
 
Oodini <svdbg@free.fr> 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 (palaste@cc.helsinki.fi) ---------------------------\
| 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

Martin Ambuhl 06-26-2003 07:57 AM

Re: sprintf
 
Oodini <svdbg@free.fr> wrote (26 Jun 2003) in
news:3EFAA588.2060000@free.fr / 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

Pieter Droogendijk 06-26-2003 07:58 AM

Re: sprintf
 
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.

John Smith 06-26-2003 08:30 AM

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


"Joona I Palaste" <palaste@cc.helsinki.fi> wrote in message
news:bde8so$5fs$1@oravannahka.helsinki.fi...
> Oodini <svdbg@free.fr> 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 (palaste@cc.helsinki.fi) ---------------------------\
> | 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




Oodini 06-26-2003 01:31 PM

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

Very sorry...


John Smith 06-26-2003 02:52 PM

Re: sprintf
 
[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.



Dan Pop 06-26-2003 03:03 PM

Re: sprintf
 
In <slrnbfm07h.6j9.haude@kir.physnet.uni-hamburg.de> Daniel Haude <haude@physnet.uni-hamburg.de> writes:

>On 26 Jun 2003 07:55:04 GMT,
> Joona I Palaste <palaste@cc.helsinki.fi> wrote
> in Msg. <bde8so$5fs$1@oravannahka.helsinki.fi>
>>> 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@ifh.de

CBFalconer 06-26-2003 03:38 PM

Re: sprintf
 
Ed Morton wrote:
> "John Smith" <someone@microsoft.com> 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 (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!



CBFalconer 06-26-2003 04:20 PM

Re: sprintf
 
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 (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!




All times are GMT. The time now is 03:16 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.