Stupid memory question

This is a discussion on Stupid memory question within the C Programming forums, part of the General Programming Boards category; I want to assing argv[1] (or argv[2]) to char *file: Code: file = &(*argv[1]); It's segmentation fault (of course). But ...

  1. #1
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312

    Stupid memory question

    I want to assing argv[1] (or argv[2]) to char *file:

    Code:
    file = &(*argv[1]);
    It's segmentation fault (of course). But I can't think of how to do this (I'm tired or something :P)
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    char *szFile = argv[1];
    Make sure you argv[1] exists before you access it.

  3. #3
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    That's a segmentation fault too (I'm sure argv[1] exists!)
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Ideswa's code is actually equivalent to that, though somewhat more convoluted. If it's segfaulting anyway, argv[1] is probably NULL (argv[argc] is always NULL) or beyond the end of the array. Check to make sure it's a valid array index before you use it.
    Code:
    if(argc >= 2) {
        file = argv[1];
    }
    else print_usage();
    Note that argv[0] (the program name) is guaranteed to exist, so you don't have to check for its existance. I think it will be "" if nothing else, so you're okay printing it. [Someone correct me if I'm wrong.]

    [edit] argc has to be at least 2 for argv[1] to be a parameter. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Ok, please explain why this is segfaulting (to be sure, I changed everything to argv[0]):

    Code:
    char *file;
    int n;
    int lines = 0;
    int curLine = 0;
    
    if(argc == 2)
    {
    	file = argv[0];
    	n = 5;
    }
    else if(argc == 3)
    {
    	n = atoi(argv[1]);
    	file = argv[0];
    }
    else
    {
    	perror("Argument error!");
    }
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Write out an entire program. What you posted shouldn't segfault.

    BTW, I wouldn't suggest opening argv[0], though. At the very least, be prepared for it to fail.

  7. #7
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Because of first using malloc(), I still used free(file) at the end, whichwas causing the segfault! Thanks for the help
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Something like this would work, because free(NULL), by definition, does nothing. Then you wouldn't have to check argc twice.
    Code:
    char *file = NULL;
    
    if(argc >= 2) {
        file = malloc(strlen(argv[1]) + 1);
        strcpy(file, argv[1]);
    }
    
    free(file);
    Just a thought. Perhaps you're already doing that. Or perhaps you could exit the program when argc is invalid, skipping the free altogether. Or you could just ignore my ramblings and leave it how it is . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  2. Replies: 7
    Last Post: 11-04-2005, 12:17 AM
  3. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  4. Question about Memory Leaks with *char
    By Diamonds in forum C++ Programming
    Replies: 11
    Last Post: 12-16-2002, 07:00 AM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 02:41 PM

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