Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7724859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:47:40+00:00 2026-06-01T04:47:40+00:00

I have tried troubleshooting this problem and just ended up with nothing, I hope

  • 0

I have tried troubleshooting this problem and just ended up with nothing, I hope you can hint me in the right direction. I have a program where I initialized a char array this way :

char variable1[8];

And then I try to copy a return value of a function i.e

...
strcpy(variable1, (char *)function1());

Where function1() returns char array value.
Here is the structure of function1 :

 char* function1()
{
    ....
    char variable2[8]={'\0'};
    ...
    return (variable2);
}

The program crashes when I try to execute the strcpy line.

I have read on strcpy function here that

Important: You must ensure that the destination buffer (s1) is able to contain all the characters in the source array, including the terminating null byte. Otherwise, strcpy() will overwrite memory past the end of the buffer, causing a buffer overflow, which can cause the program to crash

Therefore I tried to increase variable1 size to 20, but still the program crashes.

Aside from that, I tried different approach i.e use strncpy function call, and it is still giving me the same error.

The strange thing is that I could execute function1() which returns a value without a problem. Only when I tried to copy it over, my program crashes. Any ideas what I should do next?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:47:41+00:00Added an answer on June 1, 2026 at 4:47 am

    Your problem is not because your destination buffer is too small, it’s because the variable containing the source string has gone out of scope before you try to use it.

    Within function1, variable2 is created on the stack but this is effectively destroyed when you exit the function. Trying to use it afterwards (such as in a strcpy operation) is undefined behaviour.

    If you want an array that will survive function exit, you probably want to allocate it from the heap:

    char *function1 (void) {
        return calloc (1, 8);    // allocate and zero out 8 bytes
    }
    

    and remember to free the pointer afterwards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have tried to find solutions for this and can't really come up with anything.
I have tried searching over the internet about this problem but not able to
I have tried this: #define format(f, ...) \ int size = strlen(f) + (sizeof((int[]){__VA_ARGS__})/sizeof(int))
I began troubleshooting my sudden broken routes problem in this SO question: Devise /users/sign_in
I have been troubleshooting this for a while, and I am kind of new
I have been troubleshooting why my product images just won't upload/show in the admin
I have been reading up most questions on this item, but somehow I can't
I have tried to go through the jungle (really, PayPal, why don't you weed
I have tried many permutations but they all don't seems to work well. Am
I have tried a variety of different solutions found on stack and other places

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.