5 Replies - 844 Views - Last Post: 13 March 2010 - 08:45 AM Rate Topic: -----

#1 xTorvos  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 271
  • Joined: 23-October 09

Seg Fault with strcpy(x, ? : )

Posted 13 March 2010 - 07:51 AM

Hello!

I'm new to using the ternary operator and strcpy(), so I'm not sure if I'm using either correctly. I was wondering if someone could give me some guidance as to why this code would be producing a segmentation fault (when my input is "-" or "test.txt") and what I could do to correct it.

    char * file_name;

    for(i = 1; i < argc; i++)
    {
        if(!(argv[i][0] == '-' && strlen(argv[i]) > 1))
        {
            strcpy(file_name,(argv[i][0] == '-') ? "stdin" : argv[i]);
        }
    }



Something I've tried is changing it to this which produces the same error:
if(argv[i][0] == '-')
    strcpy(file_name, "stdin");
else
    strcpy(file_name, argv[i]);



This leads me to believe that it has something to do with my strcpy(). Could someone clue me in as to what I'm doing that is causing this?

Thanks for your consideration!

Is This A Good Question/Topic? 0
  • +

Replies To: Seg Fault with strcpy(x, ? : )

#2 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Seg Fault with strcpy(x, ? : )

Posted 13 March 2010 - 07:58 AM

You didn't allocate memory for file_name. Use the operator new:
char *file_name = new char[length]; //length is an int telling the size of the char array

And after you are finished with the array, free the memory:
delete[] file_name;


but remember, you should stick with STL string in C++, it's much more safer and easier.

This post has been edited by Anarion: 13 March 2010 - 07:59 AM

Was This Post Helpful? 1
  • +
  • -

#3 xTorvos  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 271
  • Joined: 23-October 09

Re: Seg Fault with strcpy(x, ? : )

Posted 13 March 2010 - 08:06 AM

Sorry, I'm using C, not C++.

The main reason I didn't want to allocate the memory was because I didn't know what size I would need for the file name. I've done some research on the OS that I'm programming for and apparently its ext3 file system which you can find with the command
df -T
in Linux. This means it has a maximum of 256 bytes so I'm just going to declare file_name as
char file_name[256]
. I'm pretty sure this will do the trick.

Thanks for the help though. :)
Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6078
  • View blog
  • Posts: 23,548
  • Joined: 23-August 08

Re: Seg Fault with strcpy(x, ? : )

Posted 13 March 2010 - 08:19 AM

Might be helpful
Was This Post Helpful? 2
  • +
  • -

#5 xTorvos  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 61
  • View blog
  • Posts: 271
  • Joined: 23-October 09

Re: Seg Fault with strcpy(x, ? : )

Posted 13 March 2010 - 08:30 AM

View PostJackOfAllTrades, on 13 March 2010 - 07:19 AM, said:



Very cool! I'm going to stick with a set value of 256 since I know exactly what the value is, but I'll definitely keep this in my toolbox for later. Thanks!
Was This Post Helpful? 0
  • +
  • -

#6 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Seg Fault with strcpy(x, ? : )

Posted 13 March 2010 - 08:45 AM

Quote

The main reason I didn't want to allocate the memory was because I didn't know what size I would need for the file name

You cannot assign data to an unallocated/uninitialized pointer. That's why you got a segfault. So as yourself mentioned, either use a normal array of chars, or allocate it yourself before using.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1