Thread: argv to string segmentation fault

  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
    3,445
    show the full code. what you've shown isn't enough to see what's going on.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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, 04:44 AM
  2. Replies: 3
    Last Post: 11-05-2012, 04: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, 07: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