Thread: is a const string in a function created on the heap?

  1. #1
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116

    is a const string in a function created on the heap?

    Hi,
    Code:
    const char* f(Type t)
    {
        if(t==TYPE0)
            return "type zero";
        if(t==TYPE1)
            return "type one";
        return "no valid type"
    }
    
    ...
    const char* v=f(TYPE0);
    ...
    can I do that? will v have a valid string in it or just junk.
    are the strings in function f are created on the stack or the heap?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Well, why not try it yourself?

    Code:
    #include <stdio.h>
    
    #define TYPE0 0
    #define TYPE1 1
    
    typedef int Type;
    
    const char* f(Type t);
    
    const char* f(Type t)
    {
        if(t==TYPE0)
            return "type zero";
        if(t==TYPE1)
            return "type one";
        return "no valid type";
    }
    
    const char* v=f(TYPE0);
    
    int main(void)
    {
    	printf("v = <%s>\n",v);
    	return 0;
    }
    Produces:

    Code:
    19: error: initializer element is not constant
    Let's move where we declare v:

    Code:
    #include <stdio.h>
    
    #define TYPE0 0
    #define TYPE1 1
    
    typedef int Type;
    
    const char* f(Type t);
    
    const char* f(Type t)
    {
        if(t==TYPE0)
            return "type zero";
        if(t==TYPE1)
            return "type one";
        return "no valid type";
    }
    
    int main(void)
    {
    	const char* v=f(TYPE0);
    	
    	printf("v = <%s>\n",v);
    	return 0;
    }
    Output:

    Code:
    v = <type zero>
    Perfect.

    As to where string literals are created, they are generally created in read-only memory (although this cannot be guarenteed and therefore read-only memory should be assumed). In segments, this might be the .data segment.

  3. #3
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    I did tried it myself and it worked but I was not sure that it is just the compiler trying to be nice to me and that it would work on other platforms.
    would it?

    BTW thanks for the elaborated lightning quick respond.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    The second version of the program I posted above should work on a variety of platforms, since it's compliant with both C89/C90 and C99.

  5. #5
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116

    Talking

    Quote Originally Posted by MacGyver View Post
    The second version of the program I posted above should work on a variety of platforms, since it's compliant with both C89/C90 and C99.
    Thanks that is what I wanted to know. (-:

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  3. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  4. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM