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 7710175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:03:25+00:00 2026-06-01T01:03:25+00:00

Possible Duplicate: What is the difference between char a[] = “string” and char *p

  • 0

Possible Duplicate:
What is the difference between char a[] = “string” and char *p = “string”?

What is the difference between using memcpy on stack memory vs on heap memory?
The following code works on Tru64 but segfaults on LINUX

char * string2 = "            ";
(void)memcpy((char *)(string2),(char *)("ALT=---,--"),(size_t)(10));

The second version works on LINUX

char * string2 = malloc(sizeof(char)*12);
(void)memcpy((char *)(string2),(char *)("ALT=---,--"),(size_t)(10));

Can someone explain the segfault on LINUX?

  • 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-01T01:03:27+00:00Added an answer on June 1, 2026 at 1:03 am

    The first example has an Undefined Behavior. And so it might work correctly or not or show any random behavior.

    Explanation:
    The first example, declares a pointer string2to a string literal. String literals are stored in Implementation defined read only memory locations. A user program is not allowed to modify this memory. Any attempt to do so results in Undefined Behavior.

    Reference:

    C99 standard 6.4.5/5 "String Literals – Semantics":

    In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence. For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence; for wide string literals, the array elements have type wchar_t, and are initialized with the sequence of wide characters…

    It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.

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

Sidebar

Related Questions

Possible Duplicate: What is the difference between String.Empty and “” Is equivalent to String.Empty
Possible Duplicate: Difference between “var” and “object” in C# I would like to know
Possible Duplicates: Is there any difference between “string” and 'string' in Python? Single quotes
Possible Duplicate: Difference between WIN32 and other c string I got this code inside
Possible Duplicate: Difference in SQL Between operator and “>=” & “<=” operator I see
Possible Duplicate: What’s the difference between “Array()” and “[]” while declaring a JavaScript array?
Possible Duplicate: Ruby: difference between || and ‘or’ Using Ruby || and or are
Possible Duplicate: Difference between using var and not using var in JavaScript For the
Possible Duplicate: Difference between void main and int main? Alright, so I'm using bloodshed
Possible Duplicate: Difference between using var and not using var in JavaScript Hello, I

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.