简体   繁体   中英

why strcat_s causing problems

I'm facing problem that I get random chars output instead of getting first and mid and last name combined which is the purpose of the program and when I run the debugger it says that the problem in strcat_s but I don't know what's the problem with it

#include <iostream>
#include <string.h>
class name {
private:
    char first[20], mid[20], last[20];
public:

    name();
    name(const char*, const char*, const char*);
    ~name();
    char* show();
};
name::name()
{
    first[0] = mid[0] = last[0] = '\0';
}
name::name(const char* f, const char* m, const char* l)
{
    size_t lenF = strlen(f), lenM = strlen(m), lenL = strlen(l);
    if (strlen(f) > 20) lenF = 20;
    else if (strlen(m) > 20) lenM = 20;
    else if (strlen(l) > 20) lenL = 20;
    strncpy_s(first, f, lenF);
    strncpy_s(mid, m, lenM);
    strncpy_s(last, l, lenL);
}
name::~name()
{
    std::cout << "distructing..." << std::endl;
}
char* name::show()
{
    char temp[62];
    strcpy_s(temp, first);
    strcat_s(temp, " ");
    strcat_s(temp, mid);
    strcat_s(temp, " ");
    strcat_s(temp, last);
    return temp;
}
int main()
{
    name a("kinan", "fathee", "ayed");
    std::cout << a.show() << std::endl;
}

Your logic for returning the full name is incorrect. You have a local variable temp and you are returning a reference to that variable. However, this variable is destroyed once show() function is completed. So, in your main function you have a reference but its pointing to something that is already destroyed. That's why you will see random characters when you print it. Here is the solution to your problem. You need to create a dynamic array ie a pointer so that it does not get destroyed.

char *name::show()
{
    int size = strlen(first) + strlen(mid) + strlen(last) + 3;
    char *temp = new char[size];
    strcpy_s(temp, size, first);
    strcat_s(temp, size, " ");
    strcat_s(temp, size, mid);
    strcat_s(temp, size, " ");
    strcat_s(temp, size, last);
    return temp;
}

int main()
{
    name a("kinan", "fathee", "ayed");
    char *temp = a.show();
    std::cout << temp << std::endl;
    delete[] temp;
}

Edit: In your original code, if you print temp at the end of show function before returning it, you will see that temp contains full name.

Edit: Temp is a local variable. Every local variable is destroyed at the end of the function's execution. In the suggested solution, I am creating an array dynamically on the heap. When we dynamically create an array, it's not removed from the heap automatically. So, when I return a reference to that array and use it in main it still works.

Edit: I have added delete[] in main function to deallocate memory.

Should be reserved 1 character for '\0', therefore you need to write strncpy_s(first, f, lenF - 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM