• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 398
  • Last Modified:

Function that returns a charpointer

Hey all,
Need a refresher here...been ages since I wrote a function that returns a charpointer.

Is the following safe?

#define CHECK_MODE 100
#define PROCESS_MODE 200

// return a string that contains the mode currently in use
char *ShowMode(int mode)
{
   char mode_buff[20];

     case CHECK_MODE
        strcpy(mode_buff, "CHECK MODE");
        break;

     case PROCESS_MODE:
        strcpy(mode_buff, "PROCESS MODE");
        break;

     default:
        mode_buff[0] = 0;
        break;

    return (mode_buff);
}

Usage:
void test()
{

    int mode;

    <set the mode here>
     printf("The mode is %s\n", ShowMode(mode));
}

My concern is that I have some vague memory that for functions that return a charpointer, the variable you return must be a function argument (or perhaps a static char declared locally within the function).  My way, you have a local char, but it's not static.

Please advise.
Thanks,
Steve

0
Stephen Kairys
Asked:
Stephen Kairys
  • 5
  • 4
3 Solutions
 
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
Update...
I just tried the above function, and it apparently returned garbage when I display its returnvalue.
I then changed the local var mode_buff to be static and it apperas to have worked.

So, would appreciate knowing why that change should make a differnce and if I've gone far enough by making the local var static (if at all possi ble I want to avoid adding a paramter to this function.

Thanks again!
Steve
0
 
evilrixSenior Software Engineer (Avast)Commented:
No it is not safe since mode_buff is destroyed when the function ends.

Do this instead...
char const *ShowMode(int mode)
{
   switch(mode)
   {
     case CHECK_MODE
        return "CHECK MODE"; // literals live for the life of the program

     case PROCESS_MODE:
        return "PROCESS MODE";
   }
   
   return 0;

}

Open in new window

0
 
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
OK, but don't static vars live for the life of the program. It seemed to work when I changed my local var to be a static. Is that OK? Thanks
0
Live Q & A: Securing Your Wi-Fi for Summer Travel

Traveling this summer? Join us on June 18, 2018 for a live stream to learn about the importance of Wi-Fi security and 3 easy measures you can start taking immediately to protect your private data while using public Wi-Fi. Follow us today to learn more!

 
evilrixSenior Software Engineer (Avast)Commented:
>> but don't static vars live for the life of the program
They do but why bother when you can just return a pointer to a literal?
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> Is that OK? Thanks
All places that reference the pointer reference mode_buff and when this is changed all places referencing it will see that change. That may or may not be what you want. Also, if the code is multi-threaded you will have to handle synchronisation otherwise you'll end up with a race condition.
0
 
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
mode_buff will never change unless I need it again via a call to the function.
code is not multi-threaded.

Just curious - I assume that the reason that it's OK to have something like this

int square_of(int i)
{
    int local_i

    local_i = i*i;
    return (local_i);
}

is becuase I am NOT reutrning a pointer to something, right?
Thanks. I think I'm going to playh it safe and return the actual literal :)
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> is becuase I am NOT reutrning a pointer to something, right?
Correct... you are returning a copy of what is in local_i and not a reference to local_i that will go out of scope when the function ends.

>> I'm going to playh it safe and return the actual literal :)
Unless there is a compelling reason not to it is the simplest (and most efficient) solution.
0
 
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
OK. Trying out your sol'n now, will post back after I confirm it works in the my application. Thanks.
0
 
Stephen KairysTechnical Writer - ConsultantAuthor Commented:
THANK YOU!! That worked. Have a good evening.
-Steve
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Prepare for an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program curriculum features two internationally recognized certifications from the EC-Council at no additional time or cost.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now