Yet another pointer issue!!

Discussion in 'C Programming' started by EoindeBarra@gmail.com, Jun 6, 2006.

  1. Guest

    I am not sure where I am going wrong in the code below. It seems to be
    working fine, then one of the pointers (directory1) gets garbage added
    to it, and it breakes the rest of the program. Also, there seems to be
    memory issues when I debug the program and I know they are from the
    code below, just not sure why.

    I would really appreciate any guidance on the following problem.

    I have a set method

    int set()
    {

    char * directory1, directory2 ; //two pointers I need to pass to other
    functions

    .......other calls which aren't affecting the pointers above

    which_directory (&directory1); //sets directory1 to a system path - see
    below

    another_directory(directory1, &directory2); //sets directory2 to
    another system path based on directory1

    some_function (directory1, directory2); //once these 2 variables values
    are set, I then need to pass them to another method - but they return
    the wrong values based on the code below!!

    free (directory1)
    free (directory2);

    return 0;

    } //finish set method

    int which_directory(char **directory)
    {
    char current_path[128], path[12] = "/end/of/path";

    *directory = malloc (sizeof(char[strlen(current_path)+1]));

    strcpy(current_path , get_path()); //returns the current path

    sprintf(*directory, "%s%s",current_path,path); //an example being
    /export/home/user1/end/of/path

    free (*directory);
    return 0;
    }

    /* The above function seems to work fine and returns the correct path
    I want it to */

    int another_directory(char * directory, char ** file) //accept the
    contents of directory1, and a pointer to pointer
    {
    char file_directory[128], path_end[15] = "/superuser";


    *file = malloc (sizeof(char[strlen(file_directory)+1]));

    sprintf(*file, "%s%s",directory,path_end); //seems 2 be the problem
    here!!! - trying to place the contents of directory (taken as an
    argument) and path_end to the file pointer

    free (*file);
    return 0;
    }

    I hope I have made the code clear enough. Hope someone can help me - it
    has been getting on my nerves all day!!!!!
    , Jun 6, 2006
    #1
    1. Advertising

  2. CBFalconer Guest

    wrote:
    >
    > I am not sure where I am going wrong in the code below. It seems
    > to be working fine, then one of the pointers (directory1) gets
    > garbage added to it, and it breakes the rest of the program. Also,
    > there seems to be memory issues when I debug the program and I
    > know they are from the code below, just not sure why.
    >
    > I would really appreciate any guidance on the following problem.
    >
    > I have a set method
    >
    > int set()
    > {
    >
    > char * directory1, directory2 ; //two pointers I need to pass to other
    > functions
    >
    > ......other calls which aren't affecting the pointers above


    I read no further. directory1 is a pointer to char, while
    directory2 is a single char. I suspect you really meant:

    char *directory1, *directory2;

    --
    "Our enemies are innovative and resourceful, and so are we.
    They never stop thinking about new ways to harm our country
    and our people, and neither do we." -- G. W. Bush.
    "The people can always be brought to the bidding of the
    leaders. All you have to do is tell them they are being
    attacked and denounce the pacifists for lack of patriotism
    and exposing the country to danger. It works the same way
    in any country." --Hermann Goering.
    CBFalconer, Jun 6, 2006
    #2
    1. Advertising

  3. tedu Guest

    wrote:
    > int which_directory(char **directory)
    > {
    > char current_path[128], path[12] = "/end/of/path";
    >
    > *directory = malloc (sizeof(char[strlen(current_path)+1]));


    current_path isn't initialized, so what do you expect strlen() to
    return? and there's no need for sizeof(char[]). just use strlen().
    you also don't have enough space to append path to this string.

    > strcpy(current_path , get_path()); //returns the current path
    >
    > sprintf(*directory, "%s%s",current_path,path); //an example being
    > /export/home/user1/end/of/path
    >
    > free (*directory);


    why are you freeing this memory? this function accomplishes nothing if
    it doesn't return something interesting.

    > return 0;
    > }
    tedu, Jun 6, 2006
    #3
  4. Guest

    wrote:
    >
    > char * directory1, directory2 ; //two pointers I need to pass to other
    > functions
    > ...
    > another_directory(directory1, &directory2); //sets directory2 to
    > another system path based on directory1
    > ...
    > int another_directory(char * directory, char ** file) //accept the
    > contents of directory1, and a pointer to pointer


    What diagnostics does your compiler issue when you compile this code?
    Do you have your compiler set to issue a high level of warnings?

    Try correcting the things that your compiler tells you about, or post
    here with the details if you don't understand what the compiler is
    saying. Any compiler which doesn't more or less clearly tell you what
    you're doing wrong here should be thrown away.
    , Jun 7, 2006
    #4
  5. John Bode Guest

    wrote:
    > I am not sure where I am going wrong in the code below. It seems to be
    > working fine, then one of the pointers (directory1) gets garbage added
    > to it, and it breakes the rest of the program. Also, there seems to be
    > memory issues when I debug the program and I know they are from the
    > code below, just not sure why.
    >
    > I would really appreciate any guidance on the following problem.
    >
    > I have a set method
    >
    > int set()
    > {
    >
    > char * directory1, directory2 ; //two pointers I need to pass to other
    > functions


    I suspect this is a typo, but you realize directory2 is not a pointer
    to char in the declaration above.

    >
    > ......other calls which aren't affecting the pointers above
    >
    > which_directory (&directory1); //sets directory1 to a system path - see
    > below
    >
    > another_directory(directory1, &directory2); //sets directory2 to
    > another system path based on directory1
    >
    > some_function (directory1, directory2); //once these 2 variables values
    > are set, I then need to pass them to another method - but they return
    > the wrong values based on the code below!!
    >
    > free (directory1)
    > free (directory2);
    >
    > return 0;
    >
    > } //finish set method
    >
    > int which_directory(char **directory)
    > {
    > char current_path[128], path[12] = "/end/of/path";


    current_path is not initialized; it contains random garbage.

    >
    > *directory = malloc (sizeof(char[strlen(current_path)+1]));
    >


    If you want the buffer to be as big as the maximum possible string, use

    *directory = malloc(sizeof current_path);

    If you want the buffer to only be as big as the current contents of
    current_path, use

    *directory = malloc(strlen(current_path) + 1);

    In the second case, remember that current_path is not initialized at
    this point, and contains random garbage. You might want to defer this
    operation until after the strcpy() operation below.

    > strcpy(current_path , get_path()); //returns the current path
    >
    > sprintf(*directory, "%s%s",current_path,path); //an example being
    > /export/home/user1/end/of/path
    >
    > free (*directory);


    Why are you freeing the memory at this point? You're not done with it
    yet.

    > return 0;
    > }
    >
    > /* The above function seems to work fine and returns the correct path
    > I want it to */
    >
    > int another_directory(char * directory, char ** file) //accept the
    > contents of directory1, and a pointer to pointer
    > {
    > char file_directory[128], path_end[15] = "/superuser";
    >
    >
    > *file = malloc (sizeof(char[strlen(file_directory)+1]));
    >


    Same problem as above.

    > sprintf(*file, "%s%s",directory,path_end); //seems 2 be the problem
    > here!!! - trying to place the contents of directory (taken as an
    > argument) and path_end to the file pointer
    >
    > free (*file);


    Why are you freeing the memory at this point?

    > return 0;
    > }
    >
    > I hope I have made the code clear enough. Hope someone can help me - it
    > has been getting on my nerves all day!!!!!
    John Bode, Jun 7, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. volcovcommander

    yet another http 1.1 100 continue issue

    volcovcommander, Oct 31, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    681
    volcovcommander
    Oct 31, 2005
  2. Si
    Replies:
    22
    Views:
    799
    Toby Inkster
    Feb 16, 2005
  3. sugaray

    Yet another pointer related problem

    sugaray, Mar 3, 2004, in forum: C Programming
    Replies:
    7
    Views:
    287
    - CASE -
    Mar 4, 2004
  4. Berehem
    Replies:
    4
    Views:
    529
    Lawrence Kirby
    Apr 28, 2005
  5. Perdikis Perdikis

    Yet another MySQL issue on WinXP

    Perdikis Perdikis, Aug 1, 2009, in forum: Ruby
    Replies:
    6
    Views:
    130
    Mits Beucannon
    Aug 1, 2009
Loading...

Share This Page