Thread: Seg Fault

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    Seg Fault

    I'm getting a seg-fault in the following code: strcat seems to be throwing the fault.
    I don't understand why that is the case because I am putting strings into both arguments

    Code:
    struct procinfo { char *file; char *info;};
    
    int procSize = 3;
    int procCount= 0;
    char *file = "1.txt";
    FILE *fp;
    
    
    struct procinfo procs[] = {
        "status", "Pid",
        "status", "PPid",
        "environ", "USER=",
    };
    
    
    int main(void){
    
      DIR           *d;
      struct dirent *dir;
    
      if (chdir("/proc")!=0)
        printf("Directory Not Found Properly"); //Set current directory to /proc
    
      d = opendir(".");
    
      if (d){
        while ((dir = readdir(d)) != NULL){
            procCount = 0;
            if(isdigit(dir->d_name[0])){//If directory starts with a number
                printf("PROCESS: %s\n", dir->d_name);  
              
                while(procCount < procSize){
                    file = "/proc/";
                    strcat(file, dir->d_name);
                    printf("%s\n = file", file);

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Is there some place here where you actually allocate memory? Because I'm not seeing it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    nope... so I have to Malloc the string File then? I always get confused about when I have to Malloc and when I don't.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "/proc/" is not a string that can be added to, as there is no additional space at the end of it for new characters.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by HighSeraphim View Post
    nope... so I have to Malloc the string File then? I always get confused about when I have to Malloc and when I don't.
    Any time you don't have a fixed variable/array that you're using, you need to allocate your own.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    if that is the case /proc/\0 should work right? and that isn't the case

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Thanks!

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by HighSeraphim View Post
    if that is the case /proc/\0 should work right? and that isn't the case
    That's what you have. And no, it's not something that can be added to, because there's (still) no room at the end. The string literal has just enough room for what you put inside the quotes, not any spare memory just because.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf seg fault
    By mercuryfrost in forum C Programming
    Replies: 19
    Last Post: 08-20-2009, 01:22 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM