Yet another pointer issue!!

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!!!!!
 
C

CBFalconer

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.
 
T

tedu

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.
 
J

jjf

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.
 
J

John Bode

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top