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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:27:03+00:00 2026-05-27T18:27:03+00:00

Part of a larger program I am making requires a path to be read

  • 0

Part of a larger program I am making requires a path to be read in from the command line and stored in a class. Because paths can be an arbitrary size and it’s needed in multiple functions, I store it in a char* in the header file. But, for some reason, when I assign a value to it, the program segfaults.

The debugger (gdb) shows the following:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b4828a in std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char*) ()
   from /usr/lib/libstdc++.so.6

Here’s the program I wrote to demonstrate the problem:

test.cpp:

#include "test.h"

#include <iostream>
#include <cstring>

Test::Test() {
  filepath = NULL;
}

void Test::set_path(char* string) {
  char temp[strlen(string) + 1];
  strcpy(filepath, temp);
}

char * Test::get_path() {
  return filepath;
}

int main(int argc, char *argv[]) {
  std::cout << "Enter a file path: ";
  char *temp;
  std::cin >> temp;
  Test *temp2 = new Test();
  temp2->set_path(temp);
  std::cout << "Path: " << temp2->get_path() << std::endl;
}

test.h:

#ifndef TEST_H
#define TEST_H

class Test {
private:
  char *filepath;

public:
  Test();
  void set_path(char *);
  char * get_path();
};

#endif // TEST_H

I’m not sure why it crashes. Is there something wrong with the method I’m doing this? Also, rather than just switching to strings, I’d like to find out more about this problem.

Thanks in advance!

  • 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-27T18:27:04+00:00Added an answer on May 27, 2026 at 6:27 pm

    temp (inside main) is uninitialized and not pointing to any valid allocated block of memory, therefore the line:

    std::cin >> temp;
    

    is causing input to be written into some unknown part of memory, causing undefined behaviour. You should either:

    • Make temp a char[] and only read in an amount of characters that will fit in the buffer.
    • Point temp to a valid buffer.
    • Better yet, make temp an std::string, and let the std::string class worry about the memory management.

    You’re also going to be having a similar problem with filePath after you fix the above problem. filePath is being set to NULL in the Test constructor, and then you’re copying temp to the block of memory pointed by filePath in Test::set_path:

    strcpy(filepath, temp);
    

    NULL refers to an address that you’re not allowed to dereference. You should change all of your C-strings to std::strings and use the std::string member functions and overloaded operators to deal with strings in C++.

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

Sidebar

Related Questions

Assuming self-registration is used to install components as part of a larger installer program,
I used the following piece of code to read data from files as part
I wrote a binary search function as part of a larger program, but it
As part of a larger Perl program, I am checking the outputs of diff
My program needs to read chunks from a huge binary file with random access.
As part of a larger program, I have a method which computes the coefficients
When I compile and run this code (it's part of a much larger program),
I have the following block of code that is part of a larger program.
I'm interested in formulae made up from lots of conjunctions (part of a larger
I made small program to divide large pictures and take part of them. When

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.