What I did is I created n children and than the parent sent the message "start" to them using n pipes. One pipe for each child.Now what I'm struggling to do is to send the parent back the number of each child.
This is my code until now:
int main()
{
int n=5;
int p[n-1][2];
int i;
for(i=0;i<n;i++){
if(pipe(p[i])>0){
perror("pipe error");
exit(1);
}
}
for(i=0;i<n;i++){
pid_t pid=fork();
if(pid<0){
perror("fork error");
exit(1);
}
if(pid==0){
int j;
for(j=0;j<n;j++){
close(p[j][1]);
}
for(j=0;j<i;j++){
close(p[j][0]);
}
char msg[256];
int h;
read(p[i][0],&h,sizeof(int));
read(p[i][0],msg,h*sizeof(char));
cout<<i<<"_"<<msg<<endl;
close(p[i][0]);//here I would like to send the number i to the parent
for(j=i+1;j<n;j++){
close(p[j][0]);
}
exit(0);
}
}
char ms[256];
strcpy(ms,"start");
int ho=strlen(ms);
for(i=0;i<n;i++){
if(write(p[i][1],&ho,sizeof(int))==-1){
perror("write error");
exit(1);
}
if(write(p[i][1],ms,ho*sizeof(ms))==-1){
perror("write error");
exit(1);
}
close(p[i][1]);
}
for(int j=0;j<n;j++)
close(p[j][0]);//then read the number of each child and print it
while(wait(NULL)>0){};
exit(0);
}
And this is the output:
0_start
2_start
1_start
4_start
3_start
So I successfully sent the message start to each child.But I can't figure out how will the parent receive the numbers sent by the children.
You can do a similar process But here the parent has the read end of the pipe and the children the write end. Extended the example above to include one pipe only . You could have multiple pipes one each for each child.
int main()
{
int n=5;
int p[n-1][2];
int pw[2]; // pipe child writes into
int i;
for(i=0;i<n;i++){
if(pipe(p[i])>0){
perror("pipe error");
exit(1);
}
}
if(pipe(pw)>0){
perror("pipe error");
exit(1);
}
for(i=0;i<n;i++){
pid_t pid=fork();
if(pid<0){
perror("fork error");
exit(1);
}
if(pid==0){
int j;
for(j=0;j<n;j++){
close(p[j][1]);
}
close(pw[0]);// close read end - child
for(j=0;j<n;j++){
if ( i!= j ) close(p[j][0]);
}
char msg[256];
int h;
read(p[i][0],&h,sizeof(int));
read(p[i][0],msg,h*sizeof(char));
cout<<i<<"_"<<msg<<endl;
close(p[i][0]);//here I would like to send the number i to the parent
write(pw[1],&i,sizeof(int)); // send i
close(pw[1]);
exit(0);
}
}
char ms[256];
strcpy(ms,"start");
int ho=strlen(ms);
int value;
for(i=0;i<n;i++){
if(write(p[i][1],&ho,sizeof(int))==-1){
perror("write error");
exit(1);
}
if(write(p[i][1],ms,ho*sizeof(ms))==-1){
perror("write error");
exit(1);
}
close(p[i][1]);
close(pw[1]); //close write end
if(read(pw[0],&value,sizeof(int))==-1){ // read from child process
perror("write error");
exit(1);
}
cout << " in main "<<value<<endl; // display number
}
for(int j=0;j<n;j++)
close(p[j][0]);//then read the number of each child and print it
while( wait(NULL) > 0 ){;}
exit(0);
}
Related
I wanted to do this problem but I cannot take input message:
Create a program that does the following:
1.Create a parent and a child process
2.The parent process reads a number from keyboard and sends it to the child
3.The child calculates if the given number is prime or not and prints the result on screen
This is my code:
#include <iostream>
#include <unistd.h> // for fork()
#include <string.h> // for strerror()
#include <sys/wait.h>
#include <sys/types.h>
using namespace std;
bool isprime(int number);
int main()
{
int num;
pid_t pid;
int fd[2];
char buffer[100];
pipe(fd);
pid = fork();
//parent process
if (pid > 0)
{
cin>>num;
write(fd[1], &num, sizeof(num));
close(fd[1]);
int status;
//Do not check for errors here
wait(&status);
}
//child process
else if (pid == 0)
{
read(fd[0], buffer, 100);
close(fd[0]);
if (isprime(num))
{
cout<<"number is prime";
}
else
{
cout<<"number is not prime";
}
return EXIT_SUCCESS;
}
else
{
cout << "fork() failed (" << strerror(errno) << ")" << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
bool isprime(int number)
{
if (number < 2)
return false;
if (number == 2)
return true;
for (int i = 2; (i*i) <= number; i++)
{
// Take the rest of the division
if ((number % i) == 0)
return false;
}
return true;
}
this my result of run
Using a pipe along with fork is not that hard, but you must respect some rules:
each part should close the handle that it does not use. Not doing it is a key for future problems
starting from the fork, changes in one process are not reflected in the other one
Your code should become:
...
//parent process
if (pid > 0)
{
close(fd[0]); // close the part that only the other process will use
cin>>num;
write(fd[1], &num, sizeof(num));
close(fd[1]);
int status;
//Do not check for errors here
wait(&status);
}
//child process
else if (pid == 0)
{
close(fd[1]); // close the part used by the other process
read(fd[0], &num, sizeof(num)); // read into num what the parent has written
close(fd[0]);
...
In real world code, you should check that every read is successfull (both from cin and from the pipe...)
I am trying to pipe data from one child process to another. When I run this, it hangs. If I don't make it wait for the first child process, it goes back to the top of the loop prompting for commands without giving the expected output, and when I prompt it to quit, it dumps all of the output I was expecting. I had it working with just one child process, but then the second execvp killed the parent process, and I didn't get back to the top of the loop prompting for more commands.
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
using namespace std;
int main(int argc, const char * argv[]) {
bool quit=0;
char quitArray[] = "quit";
int pipeReturnValue, fork1ReturnValue, fork2ReturnValue, pipefd[2], checkForQuit;
//Enter a loop where each iteration prompts for two single-line inputs
while (!quit) {
//Get command 1
char command1[128];
printf("Enter command 1: ");
fgets(command1,128,stdin);
command1[strlen(command1) -1] = 0;
//Exit if user enters quit
checkForQuit = strncmp(command1, quitArray, 4);
if (checkForQuit == 0) {
exit(0);
}
//Get command 2
char command2[128];
printf("Enter command 2: ");
fgets(command2,128,stdin);
command2[strlen(command2) -1] = 0;
//Exit if user enters quit
checkForQuit = strncmp(command2, quitArray, 4);
if (checkForQuit == 0) {
exit(0);
}
//Open pipe
pipeReturnValue = pipe(pipefd);
if (pipeReturnValue < 0) {
perror("Pipe failed");
exit(1);
}
//Fork 1
fork1ReturnValue = fork();
if(fork1ReturnValue < 0) {
perror("Fork failed");
exit(1);
}
else if (fork1ReturnValue == 0) {
//Fork 2
fork2ReturnValue = fork();
if (fork2ReturnValue < 0) {
perror("Fork 2 failed");
}
else if (fork2ReturnValue == 0) {
//close read end of pipe
close(pipefd[0]);
//parse command 1 arguments
//store tokens in array
char *arguments[6] = {};
arguments[0] = strtok(command1, " ");
int tokenCounter = 0;
while (arguments[tokenCounter] != NULL) {
tokenCounter++;
arguments[tokenCounter] = strtok(NULL, " ");
}
//dup stdo to pipe
dup2(pipefd[1], 1);
//execute arguments
execvp(arguments[0], arguments);
}
else {
wait(&fork2ReturnValue);
//close write end of pipe
close(pipefd[1]);
//parse command 2 arguments
//store tokens in array
char *arguments[6] = {};
arguments[0] = strtok(command2, " ");
int tokenCounter = 0;
while (arguments[tokenCounter] != NULL) {
tokenCounter++;
arguments[tokenCounter] = strtok(NULL, " ");
}
//dup stdin to pipe
dup2(pipefd[0], 0);
//exec
execvp(arguments[0], arguments);
}
}
else {
wait(&fork1ReturnValue);
}
}
return 0;
}
I finally figured it out. I needed to open the pipe after the first fork rather than before.
#include <iostream>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
using namespace std;
int main(int argc, const char * argv[]) {
bool quit=0;
char command1[128],
command2[128],
quitArray[] = "quit";
int pipeReturnValue,
fork1ReturnValue,
fork2ReturnValue,
checkForQuit,
pipefd[2];
//Loop where each iteration prompts for two single-line inputs
while (!quit) {
//Get command 1
printf("Enter command 1: ");
fgets(command1,128,stdin);
command1[strlen(command1) -1] = 0;
//Exit if user enters quit
checkForQuit = strncmp(command1, quitArray, 4);
if (checkForQuit == 0) {
quit = 1;
exit(0);
}
//Get command 2 and trim trailing new line character
printf("Enter command 2: ");
fgets(command2,128,stdin);
command2[strlen(command2) -1] = 0;
//Exit if user enters quit
checkForQuit = strncmp(command2, quitArray, 4);
if (checkForQuit == 0) {
quit = 1;
exit(0);
}
//Fork to create 1st child process, return error if fork fails
fork1ReturnValue = fork();
if(fork1ReturnValue < 0) {
perror("Fork 1 failed");
exit(1);
}
//Open pipe, return error if fork fails
pipeReturnValue = pipe(pipefd);
if (pipeReturnValue < 0) {
perror("Pipe failed");
exit(1);
}
//First child process
else if (fork1ReturnValue == 0) {
//Fork to create 2nd child process, return error if fork fails
fork2ReturnValue = fork();
if (fork2ReturnValue < 0) {
perror("Fork 2 failed");
}
//Second child process
else if (fork2ReturnValue == 0) {
//close read end of pipe
close(pipefd[0]);
//Parse command 1 arguments, store tokens in an array
char *arguments[6] = {};
arguments[0] = strtok(command1, " ");
int tokenCounter = 0;
while (arguments[tokenCounter] != NULL) {
tokenCounter++;
arguments[tokenCounter] = strtok(NULL, " ");
}
//Dup standard output to write side of pipe
dup2(pipefd[1], 1);
//Execute arguments from command 1
execvp(arguments[0], arguments);
}
//First child code continued
else {
//Wait for child 2 to to terminate
wait(&fork2ReturnValue);
//Close write end of pipe
close(pipefd[1]);
//Parse command 2 arguments, store tokens in array
char *arguments[6] = {};
arguments[0] = strtok(command2, " ");
int tokenCounter = 0;
while (arguments[tokenCounter] != NULL) {
tokenCounter++;
arguments[tokenCounter] = strtok(NULL, " ");
}
//dup standard input to read side of pipe
dup2(pipefd[0], 0);
//Execute arguments from command 2
execvp(arguments[0], arguments);
}
}
//Parent process continued
else {
//Wait for child 1 to terminate
wait(&fork1ReturnValue);
}
//return to top of loop
}
return 0;
}
I'm a newbie to socket programming and I'm trying to write a fork() function to enable the client to receive information sent by each four servers.
This is piece of the code I wrote, but it says parse Issue Expected expression.
I checked everything, used the "show invisible' but didn't work, the problem persist.
int pid = fork();
if(pid == 0)
{
close(tcp_socket);
char buffer[255]={0};
int bytes_num;
char serID[] = "0";
//std::string serID = "0";
// char serID[7] = "0";
while(true)
{
//recv(int sockfd, void *buf, size_t len, int flags);
bytes_num = recv(tcp_socket2,buffer,254,0);
if(bytes_num == 0)
{
break;
}
if(serID[] == "0")---------------->problem here
{
serID[]= buffer[0];
}
printf("serverID %s\n", serID);
if(serID[]=="serverA")---------------->problem here
{
//printf("NOW files!\n");
strcpy(str_file1[msg_num1],buffer);
msg_num1++;
}
if(serID[]=="serverB")---------------->problem here
{
strcpy(str_file2[msg_num2],buffer);
msg_num2++;
}
if(serID[]=="serverC")---------------->problem here
{
strcpy(str_file3[msg_num3],buffer);
msg_num3++;
}
if(serID[]=="serverD")---------------->problem here
{
strcpy(str_file4[msg_num4],buffer);
msg_num4++;
}
memset(buffer,0,sizeof(buffer));
}
if((serID[]=="serverA"))---------------->problem here
{
pf_file1 = fopen("rcvd_fileA.txt","w");
for(int i=0; i< msg_num1;i++)
{
//printf("now we read files!\n");
fprintf(pf_file1,"%s\n",str_file1[i]);
}
fclose(pf_file1);
}
if((serID[]=="serverB"))---------------->problem here
{
pf_file2 = fopen("rcvd_fileB.txt","w");
for(int i=0; i< msg_num2;i++)
{
fprintf(pf_file2,"%s\n",str_file2[i]);
}
fclose(pf_file2);
}
if((serID[]=="serverC"))---------------->problem here
{
pf_file3 = fopen("rcvd_fileC.txt","w");
for(int i=0; i< msg_num3;i++)
{
fprintf(pf_file3,"%s\n",str_file3[i]);
}
fclose(pf_file3);
}
if((serID[]=="serverD"))---------------->problem here
{
pf_file4 = fopen("rcvd_fileD.txt","w");
for(int i=0; i< msg_num4;i++)
{
fprintf(pf_file4,"%s\n",str_file4[i]);
}
fclose(pf_file4);
}
printf("The Client receives neighbor information from the Server %s with TCP port number %d and IP address %s \n",serID.c_str(),ntohs(sin.sin_port), ipstr);
close(tcp_socket2);
// printf("The Server %c has the following neighbor information: \n",serverID);
return 0;
}
////////////////////
I would really appreciate any one could help! Thanks in advance!
I am just putting the code here for the execute procedure
There exists a structure which contains a member char* command_list[MAXCOMMANDS][MAXARGUMENTS]
The first position of each row in this member contains the command and the rest of the items in the row are arguments
I have made the execute procedure which on giving the input ls | wc gives **
DUP2 HERE : Bad file descriptor
Structure below
struct command
{
ifstream input;
ofstream output;
int num_commands=-1;
int num_args[MAXCOMMANDS]={0};
char* command_list[MAXCOMMANDS][MAXARGS]; //command and their arguments storage
vector<string> pr_operator; //Pipe or redirection operators storage
bool background_task;
bool append;
};
Execute function below
int execute()
{
pid_t pid,wpid;
int status;
int num_pipes=count_pipes();
int pfds[2*num_pipes];
for(int i=0;i<num_pipes;i++)
{
if(pipe(pfds+2*i)<0)
{
perror("Cannot Pipe");
exit(1);
}
}
for(int i=0,j=0;i<=s.num_commands;i++,j+=2)
{
pid=fork();
if(pid==0)
{
if(i>0) //if not first command
{
if(dup2(pfds[j-2],0)<0)
{perror("DUP2 HERE");exit(1);}
close(pfds[j-2]);
close(pfds[j-1]);
}
if(i<s.num_commands) // if not last command
{
if(dup2(pfds[j+1],1)<0)
{perror("DUP2");exit(1);}
close(pfds[j+1]);
close(pfds[j]);
}
if(execvp(s.command_list[i][0],s.command_list[i])==-1)
{
perror("My Shell");
exit(1);
}
}
else if(pid<0)
{
perror("My Shell");
exit(1);
}
else
{
for(int k=0;k<2*num_pipes;k++)
close(pfds[k]);
for(int k=0;k<num_pipes+1;k++)
wait(&status);
}
}
return 1;
}
In my assignment I have to fork processes to simulate a distributed operating system with process allocating using Heuristic Algorithm On Linux using IPC.
Now I fork n children and then make them simulate the algorithm, that's not the problem though the problem is in the message queue connections between all of them
There is 2 message queues UP and DOWN they are both not working atm.
Every time I try to send something over any of these queues they are received at the other side as garbage.
So, I use this struct and those methods for dealing with message queue
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
struct msgbuff
{
long mtype;
char mtext[70];
};
void Send(key_t msgqid,char* S,long pid)
{
int send_val;
struct msgbuff message;
message.mtype = pid; /* arbitrary value */
strcpy(message.mtext,S);
cout<<"Message is "<<message.mtext<<endl;
//strcpy(message.mtext, str);
cout<<getpid()<<" Entering Send process"<<endl;
send_val = msgsnd (msgqid, &message, sizeof(message), IPC_NOWAIT);
if(send_val == -1)
perror("Error in send");
}
char* Recieve(key_t msgqid,bool nowait)
{
int rec_val;
struct msgbuff message;
cout<<getpid()<<" Entering Receive process"<<endl;
/* receive all types of messages */
if(nowait)
rec_val = msgrcv(msgqid, &message, sizeof(message), getpid(), IPC_NOWAIT);
else rec_val = msgrcv(msgqid, &message, sizeof(message), getpid(), !IPC_NOWAIT);
if(rec_val == -1){
// perror("Error in receive");
return "none";
}
return message.mtext;
}
Then I use them at every child like so
DOWN = msgget(200, IPC_CREAT|0644);
UP = msgget(201,IPC_CREAT|0644);
And
while(1)
{
int countfail =0;
char* ask =Recieve(DOWN,true); //nowait
string asks(ask);
cout<<getpid()<<" ask= "<<asks<<endl; //This here prints either garbage (symbols and other random characters) or "none"
if(strcmp(ask,"none")!=0)
{
///////Logic for the algorithm
cout<<"*********"<<getpid()<<" In ASK "<<endl;
stringstream ss1;
ss1.str(ask);
int senderpid=0,processSize=0;
ss1>>processSize>>senderpid;
char* processRecMessage = new char[70];
///setting up what will be sent to other process
if(count+ processSize <= load)
{
count+=processSize;
strcpy(processRecMessage,"Taken");
}
else
{
strcpy(processRecMessage,"Not Taken");
}
//Another Garbage here
Send(UP,processRecMessage,senderpid);
}
else{
int nextProcess = (rand()%3) +1;
if(count + nextProcess <=load)
{
count +=nextProcess;
}
else{
for(int k = 0;k<3;k++)
{
int selectedChild = rand()%n;
cout<<getpid()<<" Selected Child no "<<selectedChild+1<<" PID="<<children[selectedChild]<<endl;
char* x = new char[70];
stringstream ss;
ss<<nextProcess;
ss<<" "<<getpid();
strcpy(x,ss.str().c_str());// x= "nextProcess pid"
cout<<x<<endl;
//// sending here also recieves garbage
Send (DOWN , x,children[selectedChild]);
//// receiving garbage
x= Recieve(UP,false);
cout<<getpid()<<" UP Value = "<<x<<endl;
if (strcmp(x,"Taken")==0){
cout<<"Process sent from "<<getpid()<<" to "<<children[selectedChild]<<endl;
break;
}
else
{
countfail++;
printf("TRAIL #%d failed\n",countfail);
}
}
if(countfail==3)
{
cout<<"Algorithm failed to allocate process.\n";
cout<<"Terminating Process "<<getpid()<<endl;
break;
}
}
}
}
So If anyone could help me with this I would be grateful
p.s.:
I delete all message queues after each fail run of the program so each time they start anew but still no use.
edit:
Adding the full code
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <math.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <string.h>
#include <map>
#include <sstream>
#include <ctime>
using namespace std;
struct msgbuff
{
long mtype;
char mtext[70];
};
void Send(key_t msgqid,char* S,long pid)
{
int send_val;
struct msgbuff message;
message.mtype = pid; /* arbitrary value */
strcpy(message.mtext,S);
cout<<"Message is "<<message.mtext<<endl;
//strcpy(message.mtext, str);
cout<<getpid()<<" Entering Send process"<<endl;
send_val = msgsnd (msgqid, &message, sizeof(message), IPC_NOWAIT);
if(send_val == -1)
perror("Errror in send");
}
char* Recieve(key_t msgqid,bool nowait)
{
int rec_val;
struct msgbuff message;
cout<<getpid()<<" Entering Receive process"<<endl;
/* receive all types of messages */
if(nowait)
rec_val = msgrcv(msgqid, &message, sizeof(message), getpid(), IPC_NOWAIT);
else rec_val = msgrcv(msgqid, &message, sizeof(message), getpid(), !IPC_NOWAIT);
if(rec_val == -1){
// perror("Error in receive");
return "none";
}
return message.mtext;
}
int main()
{
int n ;
pid_t pid;
key_t DOWN,UP;
DOWN = msgget(200, IPC_CREAT|0644);//Creates new identifier
UP = msgget(201,IPC_CREAT|0644);
int shmid;
shmid = shmget(50, 393216, IPC_CREAT|0644);
void *shmaddr;
shmaddr = shmat(shmid, (void *)0, 0);
printf("DOWN = %d\n", DOWN);
printf("UP = %d\n", UP);
cout<<"Please enter the number of machines "<<endl;
cin>>n;
int* children = new int[n];
string pids;
stringstream ss4;
ss4<<n;
string number = ss4.str();
pids+=number;
pids+=" ";
for (int i=0;i<n;i++)
{
pid = fork();
children[i]=pid;
stringstream ss3;
ss3<<pid;
string pidstr=ss3.str();
pids+=pidstr;
pids+=" ";
if (pid==0)
break;
}
if (pid==-1)
cout<<"Error in fork" <<endl;
else if (pid==0) // child
{
sleep(1);
DOWN = msgget(200, IPC_CREAT|0644);
UP = msgget(201,IPC_CREAT|0644);
//cout<<"Entering child process"<<endl;
shmid = shmget(50, 393216, IPC_CREAT|0644);
shmaddr = shmat(shmid, (void *)0, 0);
char* pidsrec = new char[100];
strcpy(pidsrec,(char*) shmaddr);
stringstream sss;
string spid(pidsrec);
sss.str(spid);
sss>>n;
children = new int[n];
for(int i =0;i<n;i++)
{
sss>>children[i];
//cout<<getpid()<<"Child #"<<i<<" = "<<children[i]<<endl;
}
srand(getpid());
int load = (rand()%10) +1; // load of operating on this system from 1-10
int count=0;
while(1)
{
int countfail =0;
char* ask =Recieve(DOWN,true); //nowait
string asks(ask);
cout<<getpid()<<" ask= "<<asks<<endl;
if(strcmp(ask,"none")!=0)
{
cout<<"*********"<<getpid()<<" In ASK "<<endl;
stringstream ss1;
ss1.str(ask);
int senderpid=0,processSize=0;
ss1>>processSize>>senderpid;
char* processRecMessage = new char[70];
if(count+ processSize <= load)
{
count+=processSize;
strcpy(processRecMessage,"Taken");
}
else
{
strcpy(processRecMessage,"Not Taken");
}
Send(UP,processRecMessage,senderpid);
}
else{
int nextProcess = (rand()%3) +1;
if(count + nextProcess <=load)
{
count +=nextProcess;
}
else{
for(int k = 0;k<3;k++)
{
int selectedChild = rand()%n;
cout<<getpid()<<" Selected Child no "<<selectedChild+1<<" PID="<<children[selectedChild]<<endl;
char* x = new char[70];
stringstream ss;
ss<<nextProcess;
ss<<" "<<getpid();
strcpy(x,ss.str().c_str());// x= "nextProcess pid"
cout<<x<<endl;
Send (DOWN , x,children[selectedChild]);
x= Recieve(UP,false);
cout<<getpid()<<" UP Value = "<<x<<endl;
if (strcmp(x,"Taken")==0){
cout<<"Process sent from "<<getpid()<<" to "<<children[selectedChild]<<endl;
break;
}
else
{
countfail++;
printf("TRAIL #%d failed\n",countfail);
}
}
if(countfail==3)
{
cout<<"Algorithm failed to allocate process.\n";
cout<<"Terminating Process "<<getpid()<<endl;
break;
}
}
}
}
}
else //parent
{
strcpy((char*) shmaddr,pids.c_str());
}
cout<<getpid()<<" GOODBYE"<<endl;
return 0;
}
I try it with any number of processes and there is always garbage received at the receive end of the message queue
In your Receive function, when you do
return message.mtext;
you are returning a pointer to data allocated locally on the stack. This area of the stack will not be valid after the function returns, and hence the dereferencing the pointer will cause undefined behavior. Also, if you use this pointer after calling another function, that area of the stack will most likely have been overwritten by the other function.
Since you're using C++, why not return std::string? It will solve this problem.