Thread: Functions, stack

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    34

    Functions, stack

    Hi,

    I have the following code:

    Code:
    #include <stdio.h>
    
    char * returnString () {
    	
    	char *string;
    	
    	string = "Blah";
    	
    	return (string);
    		
    }
    
    int main() {
    	
    	printf("%s\n", returnString());	
    	
    	return (0);	
    }
    First question: Is the string "Blah" stored on stack? If so, how come it can be printed in main()? Shouldn't it be removed off the stack right upon exiting function returnString()?

    Second question: After compiling this code, my compiler gives me the following warning:

    Call to function 'returnString' with no prototype in function main
    Could you tell me why?


    Thanks a lot, guys.

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Quote Originally Posted by Ariod
    First question: Is the string "Blah" stored on stack? If so, how come it can be printed in main()? Shouldn't it be removed off the stack right upon exiting function returnString()?
    "Blah" is in the text segment, and the variable string is on the stack. For what it's worth, data on the stack is not erased when the stack pointer is moved to reclaim the space allocated during the call.
    Jason Deckard

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Regarding the "no prototype" warning message, are you compiling your C code as C++? Which compiler are you using?
    If you understand what you're doing, you're not learning anything.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your error
    Code:
     Call to function 'returnString' with no prototype in function main
    means that you need a prototype before the function's first use.
    Code:
        char *string;
    	
        string = "Blah";
    You have no way of knowing what string points to, and you assign "Blah" to what ever that is! Not good. malloc() the string first (or declare it as [number]).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Actually, you don't need a prototype in C for a function that's defined before it's called. The OP shouldn't be getting that warning.

    And the assignment of "Blah" to string is perfectly legal. If he was trying to do something like strcpy(string, "Blah") without allocating memory for string then it would be bad. But all the OP is doing in this case is saying string points to "Blah" which is valid. Just don't try changing the contents of "Blah" (i.e. string[0] = 'r') because string literals are commonly stored in read-only memory.
    Last edited by itsme86; 07-27-2005 at 11:12 AM.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    34
    Quote Originally Posted by Deckard
    "Blah" is in the text segment, and the variable string is on the stack. For what it's worth, data on the stack is not erased when the stack pointer is moved to reclaim the space allocated during the call.
    So the string (the actual text) is not stored on the stack? Sorry, I'm just trying to understand this.

    Quote Originally Posted by itsme86
    Regarding the "no prototype" warning message, are you compiling your C code as C++? Which compiler are you using?
    I'm using Borland's free C/C++ compiler. The file's extension is .c if that means anything, I'm new to this compiler so I don't know if that's enough information for it to compile as C, not C++.

    I wrote a few programs where the function definition was before where it was being called, and it compiled without any warnings. This is the first time I got this warning.


    Thanks for your replies.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    So the string (the actual text) is not stored on the stack? Sorry, I'm just trying to understand this.
    Correct. Only local, non-static variables are stored on the stack. Your pointer string is stored on the stack, but that only contains the address of "Blah", not "Blah" itself. If your function returned &string it would be bad (a pointer to the variable on the stack).

    I'm not familiar with Borland's compiler, but I use DEV-C++ in Windows and it seems to be a very decent free IDE.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Actually, you don't need a prototype in C for a function that's defined before it's called. The OP shouldn't be getting that warning.
    You're right, you don't (although it's a good habit to get into).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    To add to itsme86's answer, it is the C standard which requires that a string literal will be valid for the lifetime of a program.
    http://dev.unicals.com/papers/c89-draft.html#3.1.4

    A character string literal has static storage duration and type ``array of char ,'' and is initialized with the given characters.
    http://dev.unicals.com/papers/c89-draft.html#3.1.2.4

    An object declared with external or internal linkage, or with the storage-class specifier static has static storage duration. For such an object, storage is reserved and its stored value is initialized only once, prior to program startup. The object exists and retains its last-stored value throughout the execution of the entire program.
    Second question: After compiling this code, my compiler gives me the following warning:
    Call to function 'returnString' with no prototype in function main
    Could you tell me why?
    Code:
    char * returnString ()
    In C, this is not a complete function definition.
    Empty parameter lists

    C distinguishes between a function declared with an empty parameter list and a function declared with a parameter list consisting of only void. The former is an unprototyped function taking an unspecified number of arguments, while the latter is a prototyped function taking no arguments.
    To define a function that takes no arguments, you must use the keyword void:
    Code:
    char * returnString (void) {
    
    int main(void) {

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    34
    Quote Originally Posted by anonytmouse
    Code:
    char * returnString ()
    In C, this is not a complete function definition.

    To define a function that takes no arguments, you must use the keyword void:
    Code:
    char * returnString (void) {
    
    int main(void) {
    Thanks for the tip, I get no warnings now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack Implementation and Exceptions
    By audinue in forum C Programming
    Replies: 4
    Last Post: 06-22-2008, 09:32 AM
  2. Stack overflow errors in 3 areas
    By ulillillia in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 03:20 PM
  3. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  4. Help With Stacks
    By penance in forum C Programming
    Replies: 7
    Last Post: 10-09-2005, 02:47 PM
  5. What am I doing wrong, stack?
    By TeenyTig in forum C Programming
    Replies: 2
    Last Post: 05-27-2002, 02:12 PM