Thread: Segmentation fault (core dumped)

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    38

    Segmentation fault (core dumped)

    This is doing my head in and I have been at it for hours. I have googled with no luck. tried different loops again no joy.

    I have done what I need to do however without using an array, its messy and its limited, now I am improving my code by using an array.

    The code is meant to assign the first character after a space to an array. The program runs fine providing there are no spaces.. HA. If there is a space I get "Segmentation fault (core dumped)

    So in turn given I get the above error when I input a space I am 99% sure the problem is in the code below. But I can not figure it out.

    I will post the whole code if required but I didn't want to waste anyones time.

    Code:
        
    for (int i = 0; i < strlen(input); i++)    {
            if (input[i] == 32)    // Checking if a space is present.
            {
                count++;   // Counting spaces from input to use later.
                *argv[x] = input[i + 1];   // assigning the char after space to an array.
                x++;        
                argc++;
            }
        }


    Thanks in advance.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Please post why you think this location is legal to write values to?

    Code:
    *argv[x]
    Edit: For that matter why do you think this is legal
    Code:
    argc++
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    38
    Code:
    *argv[x] = input[i + 1];


    Am i wrong assuming I can use x to place the character in that position of the array?
    x is declared earlier in the program and as you can see it increases by one if that if statement is initialised in the event another space or '32' is present.

    Also I am not sure why I cant use this:
    Code:
     argc++;
    Thanks, Ryan

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    38
    I should add I am trying to use an unassigned array and it will only increase as required using the above code appose to using a loop to detect how many places I need to allocate the array... Essentially I am attempting to eliminate lines of code, more so for learning purposes seeing if I can take a different approach (taking less steps)

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I don't think I've ever seen anyone attempt to use argv or argc in any other way that treats them as constants.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Ryan Huddo View Post
    I should add I am trying to use an unassigned array and it will only increase as required using the above code appose to using a loop to detect how many places I need to allocate the array... Essentially I am attempting to eliminate lines of code, more so for learning purposes seeing if I can take a different approach (taking less steps)
    You haven't told us what argv and argc are. The presumption is that these are the canonically-named parameters to main, but that doesn't make much sense the way you're using them. Post your entire program so we can see what's going on here.

    @stahta01: It is legal to modify argc and even to modify the strings pointed to by argv (although you would obviously need to stay within the original lengths).

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Please post the smallest complete code that illustrates your problem. The snippet you posted leaves too much to one's imagination. For example how and where were all those variables defined? While argv and argc are normally associated with command line arguments that is not always the case. Also even though the command line arguments are not constants, I recommend you consider them as constants anyway.

    Jim

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    38
    I don't mean to insult anyones intelligence because your all obviously good at this but regardless I will comment on lines to indicate my intent.

    Code:
    /*
    When running this I am simply running it as "./name" not ./name blah foo baa. 
    I am hoping I can re-allocate argc and argv during the program
    */
    int main(int argc, char *argv[])                                        // Trying this for the first time.. :/ 
    {
        int count = 1;                                                            // Counting each space from input
        int x = 1;                                                                 // Using for place holders in argv
        string input;
        
        printf("Input: ");
        input = GetString();
        
        for (int i = 0; i < strlen(input); i++)              // Checking for spaces or 32 from the input
        {
            if (input[i] == 32)
            {
                count++;                                           // again counting spaces.
                *argv[x] = input[i + 1];                     // Assigning the first char to *argv[x], in this case 1. for example "Hello, world". *argv[x] = w.
                x++;                                               // making x++ for the above line on the next loop
                argc++;                                         // Increasing my array size of argv, if I am in fact using this correctly. 
            }
        }
    
    
        *argv[0] = input[0];
        
        for (int i = 0; i < count; i++)
        {
            printf("%c\n", *argv[i]);
        }
    }
    Thanks again, Ryan
    Last edited by Ryan Huddo; 07-21-2015 at 12:08 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ryan Huddo
    When running this I am simply running it as "./name" not ./name blah foo baa.
    In that case, command line arguments do not come into play at all.

    Quote Originally Posted by Ryan Huddo
    I am hoping I can re-allocate argc and argv during the program
    No, you cannot, at least not when they conventionally denote the number of command line arguments and the command line arguments themselves. But why would you need to do so? Since you are obtaining the input from the user (presumably) via standard input, you could just work with that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    When running this I am simply running it as "./name" not ./name blah foo baa.
    O_o

    If you aren't filling `argc` and `argv` with something by supplying arguments, the expression `*argv[x] = input[i + 1]` writes to memory you don't own.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    38
    Quote Originally Posted by laserlight View Post
    In that case, command line arguments do not come into play at all.


    No, you cannot, at least not when they conventionally denote the number of command line arguments and the command line arguments themselves. But why would you need to do so? Since you are obtaining the input from the user (presumably) via standard input, you could just work with that.
    Ah well that answers that issue, that brings me back to the first set of code i wrote, essentially:

    Code:
    // GetString
    // for loop to find spaces
    // - allocate spaces into an int
    // use value to assign an array
    // for loop to assign correct characters to the array as required.
    // printf.
    So is this the only way or can I assign a undeclared array and increase it as required during the only for loop, like I ATTEMPTED to do with the above.
    e.g.
    Code:
    array[a]
    // GetString
    // for loop
    // - Check spaces
    // - assign char to array
    // - array++
    //printf.
    Cheers, Ryan

  12. #12
    Registered User
    Join Date
    Apr 2013
    Posts
    38
    Quote Originally Posted by phantomotap View Post
    the expression `*argv[x] = input[i + 1]` writes to memory you don't own.
    Soma
    Ahhh, makes so much sense, haha learning curb.

  13. #13
    Registered User
    Join Date
    Apr 2013
    Posts
    38
    Just letting everyone know I figured it out, I don't completely understand it and if someone wants to explain it to me please feel free, but working code as follows;

    Code:
    int main(void){
        string input;
        int count = 1;
        int x = 1;
        
        printf("Input ");
        input = GetString();
        
        int *init = malloc(strlen(input) * sizeof(*init));          //new line. 
        
        for (int i = 0; i < strlen(input); i++)
        {
            if (input[i] == 32)
            {
                count++;
                init[x] = input[i + 1];
                x++;
            }
        }
        free(init);
        init[0] = input[0];
        
        for (int i = 0; i < count; i++)
        {
            printf("%c\n", init[i]);
        }
    }
    Thanks everyone for there input

    Ryan

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ryan Huddo
    Just letting everyone know I figured it out, I don't completely understand it and if someone wants to explain it to me please feel free, but working code as follows;
    I am a little apprehensive about the fact that you left out the header inclusions, etc, and then have stuff like this:
    Code:
    string input;
    /* ... */
    input = GetString();
    int *init = malloc(strlen(input) * sizeof(*init));
    What is string? If it is a typedef for char*, then GetString probably does dynamic memory allocation, in which case you need to free(input) when done. If it is a char[N], then fine, but I suggest that you avoid such a pointer/array typedef. If you want some abstraction, create a struct instead.

    Why is init a pointer to int rather than a pointer to char? It looks like you are using it as if it were a pointer to char.

    You should avoid magic numbers like the 32 here:
    Code:
    if (input[i] == 32)
    You probably want to check for a space, so it should have been:
    Code:
    if (input[i] == ' ')
    Next, this is problematic:
    Code:
    free(init);
    init[0] = input[0];
    You free(init), then immediately assign to init[0]. This results in undefined behaviour, even if in practice it may appear to work because the memory freed has not been allocated and used elsewhere. You probably should free(init) later.

    Oh, and why do you start counting from 1? I noticed:
    Code:
    int count = 1;
    int x = 1;
    This is not always wrong, but you then write things like:
    Code:
    init[x] = input[i + 1];
    and:
    Code:
    for (int i = 0; i < count; i++)
    For the former, it means that you never assign to init[0]; for the latter, it means that you may be looping one more time than you should.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by laserlight View Post
    I am a little apprehensive about the fact that you left out the header inclusions, etc, and then have stuff like this:
    Code:
    string input;
    /* ... */
    input = GetString();
    int *init = malloc(strlen(input) * sizeof(*init));
    What is string? If it is a typedef for char*, then GetString probably does dynamic memory allocation, in which case you need to free(input) when done.
    ...
    If GetString() comes from David Malan's CS50 class at Harvard University (#include "cs50.h") then string is indeed a typedef for char*, and GetString() reallocates it as it goes.

    So, follow Laserlight's advice.
    Last edited by migf1; 07-21-2015 at 06:11 AM.
    "Talk is cheap, show me the code" - Linus Torvalds

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault (core dumped) ??
    By tjberghold in forum C++ Programming
    Replies: 2
    Last Post: 12-07-2014, 06:52 PM
  2. Segmentation fault (core dumped)
    By benjaminp in forum C Programming
    Replies: 8
    Last Post: 10-10-2012, 12:46 PM
  3. Segmentation Fault (Core Dumped)
    By pureenergy13 in forum C Programming
    Replies: 3
    Last Post: 11-02-2011, 07:50 AM
  4. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM