Article View: comp.os.msdos.programmer
Article #7860Re: Where is the bug in this C code?
From: Scott McKellar
Date: Wed, 16 Aug 1995 14:31
Date: Wed, 16 Aug 1995 14:31
77 lines
3087 bytes
3087 bytes
In article <40o266$gjt@aplinfo.jhuapl.edu>, <Varndell@jhuapl.edu> writes:
> >Hello,
> >I wrote a small C code for getting the system time. The code was written
> >in TURBO C++ under DOS. The variable shows the system time in debugger,
> >but while printing that variable with printf it prints garbage. the
> >code is
> >
> >#include<stdio.h>
> >#include<stdlib.h>
> >#include<string.h>
> >#include<dos.h>
> >
> >char *mytime(void);
> >
> >int main(void) {
> > char *t;
> > if ( !(t = (char *)malloc(sizeof(char)*9)) ){ //space includes null
> > printf("Error in allocating memory");
> > exit(0);
> > }
> > t= mytime(); // Here in debugger the variable t shows the system time
>
> ^^ BUG #1 -- You just lost your pointer to malloc'd memory.
>
> > printf("The time is %s", t); //here it prints garbage. WHY?
> > free(t);
>
> ^^ BUG #2 -- You can't free this now because of BUG #1
>
> > return 0;
> >}
> >
> >char *mytime(void) {
> > /*using dos int 21h in int86() function to get the system time and
> >storing it in a string variabl. The address of this variable is
returned*/
> > return address of the variable
>
> BUG #3 -- Can't help you here. Must be returning pointer to garbage.
>
> >}
Bugs 1 and 2 -- Nice catch, GV.
As for Bug 3: My guess is that you stored the string variable in an auto
variable. That is, the compiler allocated some temporary space for it on
the stack, and you returned a pointer to it.
But once you returned from the stack, the memory to which that pointer
points did not belong to you any more. You could see it in the debugger
because nothing had happened yet to disturb the contents of that memory.
But once you called another function -- printf() in this case -- that
memory was overwritten in the course of setting up a stack frame. By the
time printf() saw it, it was full of other stuff that looked like garbage.
Since you didn't post the code for mytime(), the above is only a guess.
Assuming I guessed right, probably the best way to fix it is to pass
mytime() a pointer to a buffer owned by the calling code. Then mytime()
can fill the buffer thus provided; it doesn't have to return a pointer.
For example:
if ( !(t = (char *)malloc(sizeof(char)*9)) ){ //space includes null
printf("Error in allocating memory");
exit(0);
}
mytime( t );
printf("The time is %s", t);
free(t);
Note that this approach fixes the first two bugs as well.
Scott McKellar
Southwestern Bell Telephone
St. Louis, MO
(314) 235-3595 jm407a@multi.sbc.com
Technical Details
Message-ID:
<3891@swuts.sbc.com>Path:
arc1-ca.pugleaf.net!texbell.sbc.com!swuts!multi.sbc.comReferences:
<40o0st$fgs@coranto.ucs.mun.ca> <40o266$gjt@aplinfo.jhuapl.edu>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!texbell.sbc.com!swuts!multi.sbc.com From: jm407a@multi.sbc.com (Scott McKellar) Newsgroups: comp.os.msdos.programmer,comp.lang.c Subject: Re: Where is the bug in this C code? Message-ID: <3891@swuts.sbc.com> Date: 16 Aug 95 14:31:34 GMT References: <40o0st$fgs@coranto.ucs.mun.ca> <40o266$gjt@aplinfo.jhuapl.edu> Sender: usenet@swuts.sbc.com Followup-To: comp.os.msdos.programmer MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII