E
EoindeBarra
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!!!!!
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!!!!!