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

  • SEARCH
  • Home
  • 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 6058195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:34:23+00:00 2026-05-23T08:34:23+00:00

I know that you will rap me over the knuckles but. Why does it

  • 0

I know that you will rap me over the knuckles but.

Why does it make Segmentation fault

char* cmd;
strcpy(cmd, argv[0]);

when this doesn’t

char *cmd;
cmd = "plop";

I didn’t practice since a while, and can’t remember why.

ps: actually, i know that something like that, before the strcpy, would be better

char *cmd = (char*) malloc(strlen(argv[0]));

but i’m just wondering why this segmentation fault.

Thanks !

  • 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-05-23T08:34:24+00:00Added an answer on May 23, 2026 at 8:34 am

    When you do:

    char * cmd;
    

    You’re allocating a pointer on the stack. This pointer is not initialized to any meaningful value.

    Then, when you do this:

    strcpy(cmd, argv[0]);
    

    You copy the string contained in argv[0] to the address pointed to cmd, which is… something meaningless. Since you’re lucky, it simply segfaults.

    When you do this:

    cmd = "plop";
    

    You assign to cmd the address to a statically allocated string constant. Since such strings are read only, writing on them is undefined behavior.

    So, how to solve this? Allocate memory for the runtime to write to. There’s two ways:

    The first one is to allocate data on the stack, like this:

    char cmd[100]; // for instance
    

    This allocates an array of 100 chars on the stack. However, it’s not necessarily robust, because you must know in advance how much memory you’ll need. The stack is also smaller than the heap. Which leads us to option number 2:

    char *cmd = malloc(whatever_you_need); // no need to cast, by the way, unless you're in C++
    

    This allocates whatever_you_need chars on the heap. Don’t forget to release the memory with free once you’re done with it.

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

Sidebar

Related Questions

I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
I know that this feature will be deprecated in C++0x, but for me as
I know that DLR will be released together with C# 4.0, but I don't
I know that we can make application using Adobe air that will run on
does anyone know of an existing module or script that will remove or identify
I know there are several programs out there that will sync files over the
I want to know how to make a color picker with jQuery that will
tl;dr-edition: I have a compilation I know that will fail, but want the subset
I know that Java enforce the programmer to list all exceptions that will be
I want to know that what will happen to my activity incase of an

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.