argv to string segmentation fault

This is a discussion on argv to string segmentation fault within the C++ Programming forums, part of the General Programming Boards category; Hi all, I have this piece of code that causes segmentation fault: Code: .... check argv[2] exists .... std::string* op_file; ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    15

    argv to string segmentation fault

    Hi all,

    I have this piece of code that causes segmentation fault:
    Code:
    .... check argv[2] exists ....
    
    std::string* op_file;
    ...
    std::cout << "argv[2] is: " << argv[2] << std::endl;
    op_file = new std::string(argv[2]);
    ...
    The result I am getting is:

    Code:
    argv[2] is: somefile.txt
    Segmentation fault (core dumped)
    Thanks a lot.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    2,411
    show the full code. what you've shown isn't enough to see what's going on.
    Code:
    namespace life
    {
        const bool change = true;
    }

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Code:
    std::string* op_file;
    ...
    std::cout << "argv[2] is: " << argv[2] << std::endl;
    op_file = new std::string(argv[2]);
    There is no need to do this. This isn't Java.
    Code:
    std::cout << "argv[2] is: " << argv[2] << std::endl;
    std::string op_file = argv[2];
    Will do fine.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    15
    Well, I feel like an idiot.

    Had this guy:

    Code:
     *(op_file+i)
    in a debug message right after the code I posted.

    Sorry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String - Segmentation Fault
    By Raj 89 in forum C Programming
    Replies: 8
    Last Post: 11-27-2012, 03:44 AM
  2. Replies: 3
    Last Post: 11-05-2012, 03:21 PM
  3. String segmentation fault
    By dojha00 in forum C Programming
    Replies: 7
    Last Post: 08-29-2012, 03:30 AM
  4. Segmentation fault when changing a string
    By lilydjwg in forum C Programming
    Replies: 6
    Last Post: 12-02-2009, 06:43 AM
  5. segmentation fault when processing a string
    By Nakel in forum C++ Programming
    Replies: 2
    Last Post: 04-24-2003, 04:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21