home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:11776 comp.os.msdos.programmer:8183
- Newsgroups: comp.lang.c,comp.os.msdos.programmer
- Path: sparky!uunet!caen!uwm.edu!csd4.csd.uwm.edu!rca
- From: rca@csd4.csd.uwm.edu (Robert C Allender)
- Subject: Summary for "Is it MY problem...or Borland's?"
- Message-ID: <1992Jul30.195645.5294@uwm.edu>
- Originator: rca@csd4.csd.uwm.edu
- Sender: news@uwm.edu (USENET News System)
- Organization: Computing Services Division, University of Wisconsin - Milwaukee
- Date: Thu, 30 Jul 1992 19:56:45 GMT
- Lines: 208
-
- ***************************************************************************
- * This is a summary of the *pertinent* responses to the post: "Is it MY *
- * problem...or is it Borland's?" Thanks to all respondents, the *
- * problem has been fixed. *
- ***************************************************************************
-
- First off, for those who are interested, I declared a char * in main() and
- wanted to use it in several different functions as a pointer to an array.
- When I passed the pointer to function #1 to get the array from the keyboard,
- everything worked fine. If I passed the pointer back to main(), I still had
- access to the array, but once I passed it to another function, the array
- started to dissappear ar become filled with garbage. Being about as novice
- as they get in C programming, I didn't know what was wrong, but I checked the
- manuals, etc. and nothing seemed wrong with what I wrote.
-
- Little did I know that it WAS my problem...Here are the correct answers I got.
-
-
- From: cdsmn.mn.org!wells@uwm.UUCP (Rich Wells)
-
- In article <1992Jul25.162844.18778@uwm.edu> you write:
- >I initialize a char * in main() and pass it to a function that gets a string,
- >passing the address of the string back to main(). If I try to print the
- >string back at main(), it works. I pass the char * to the next function, and
- >I can print the string at the beginning of that function, but by the end of
- >it, the string is garbage. If I then pass the value to main() and send it to
- >yet aother function, the array is empty.
-
-
- Does scrollbox() do something like this?:
-
- char * scrollbox( ... )
- {
- char s[SOME_SIZE];
- ...
- return s;
- }
-
- If so, there's your problem. You're returning the address of a
- local variable that no longer exists after scrollbox() returns.
-
- The solution (if this is indeed the problem) is to either malloc
- some memory for the string to return, as in:
-
- s2 = malloc(strlen(s)+1);
- strcpy(s2, s);
- return s2;
-
- in which case the caller must remember to free the memory. Or
- you can supply the buffer to the scrollbox(), as in
-
- void scrollbox( char * s, int len, ...)
- {
- ...
- }
-
- (note that you should also specify the length of the buffer, so
- that scrollbox() can be sure not to overwrite the end of it).
-
- Richard Wells wells@cdsmn.mn.org or ...!tcnet!cdsmn!wells
-
- ****************************************************************************
-
- From: Kenneth Herron <kherron@ms.uky.edu>
-
- Your variable c is just a pointer. You never allocate any space for
- it. The space that it's pointing to is probably on the stack and so
- overwritten the next time you do a function call, or else a static
- buffer that gets used by some other function. You probably need to
- modify the scrollbox() function to allocate the space using malloc(),
- or if it's a library function, you need to copy the strings somewhere
- safe when the call returns.
-
- ****************************************************************************
-
- From rda399k@monu6.cc.monash.edu.au Wed Jul 29 00:47:10 1992
-
- Hi Robert,
-
- I only had a brief look at your program but from what I can see
- what you have done is fine, except that you forgot to reserve space
- for where your pointer (char *) c points to !!
-
- That is unless the function make_box() returns the pointer to a
- array of type char.
-
- What is basically happening is that the other routines are just
- reserving space temporarily and using it, it just happens to be where
- your pointer c points to.
-
- To correct the problem, try this ..
-
- main()
- {
- char drive, c[80]; // Space for 80 characters that wont get
- // written by anything else !
- ..
- ..
- }
-
- *Steve* rda399k@monu6.cc.monash.edu.au
-
- ****************************************************************************
-
- From ath@linkoping.trab.se Sun Jul 26 02:10:00 1992
-
- >I initialize a char * in main() and pass it to a function that gets a string,
- >passing the address of the string back to main().
-
- As far as I can se from the code, you don't do this. You just accept
- a char * as result from scrollbox().
-
- >If I try to print the
- >string back at main(), it works. I pass the char * to the next function, and
- >I can print the string at the beginning of that function, but by the end of
- >it, the string is garbage.
-
- Since the code to scrollbox isn't included, I can only guess. I guess
- that it returns a pointer to a static char[], either in scrollbox, or
- in one of the routines it calls. I also guess that this array is
- clobbered by some later call.
-
- This guess is easily tested by immediately copying the string returned
- from scroll_box to some local or malloced area, and then passing
- *that* to list_dir.
-
- Anders Thulin ath@linkoping.trab.se
- Telia Research AB, Teknikringen 2B, S-583 30 Linkoping, Sweden
-
- ****************************************************************************
-
- From swalter@nyx.cs.du.edu Sat Jul 25 17:16:26 1992
-
- Does scrollbox() allocate space for the pointer that's passed back? If
- not, it's writing data somewhere in memory that's eventually going to be
- overwritten.
- ...
- My guess as to what was happening is that the pointer (uninitialized) was
- pointing into dataspace somewhere which, the first time it's used, would
- produce the array. However, unless the space is declared "used
- permanently" by array declaration or callocing, it's reused when needed.
- By the time you got to the next function, the same space was reclaimed for
- something else ... thus wiping your array.
-
- -Scotty
-
- *****************************************************************************
-
- From anto@sol.acs.unt.edu Sat Jul 25 17:50:33 1992
-
- In comp.os.msdos.programmer you write:
-
- >[stuff deleted]
- > c = scrollbox(drive,61,10); <---- This gets a directory, etc.
-
- Does "scrollbox" return a ptr to an automatic (local) string or a static/
- heap string? If it's local then it shouldn't be used after the function it's
- local to terminates. If it's static, you must copy the value (not the
- pointer) somewhere else if you want to call the function more than once _and_
- keep the value. If it's a heap allocated string (allocated by malloc or calloc)
- then you shouldn't have this problem (unless there's another bug lurking
- somewhere else).
-
- I hope this helps,
- Anto.
-
- *****************************************************************************
-
- From: raymondc@microsoft.com
-
- I bet scrollbox() returns the address of a local variable.
-
- *****************************************************************************
-
- From: bangell@cs.utah.edu (bob angell)
-
- Sounds like a problem of over-writing your memory (I didn't look too closely
- at your code....but did you malloc memory on the heap for this array? or are
- you trusting the stack to keep track of things? Use the heap and make sure you
- are keeping the memory "clean".
-
- Hope this helps.
-
- -Bob-
-
- ****************************************************************************
-
-
- Thanks again to everyone who helped out!
-
- -Rob (rca@csd4.csd.uwm.edu)
-
-
-
-
-
-
- --------------------------------------------------------------------------
- | Robert C. Allender | "Exactly!" said Deep Thought. "So once you do |
- | rca@csd4.csd.uwm.edu | know what the question actually is, you'll |
- | rca@bfs.uwm.edu | know what the answer means." |
- | | -The Hitchhiker's Guide to the Galaxy |
- --------------------------------------------------------------------------
- --
-
-
-
-
-