pointer calling trouble

O

OzgurGul

int main(){

/*
I have a function :) and I want to use it but I dunno how to call
it... (I have trouble that pointers)
please help me...
mmm... thanks for all advance!!
*/



return 0;
}
class MyClass {
....
....

public:

....

/**
* Breaks the given file name up to directory, name, and extension.
* @param fname The file name.
* @param dir Variable to receive the newly allocated directory
string.
* @param name Variable to receive the newly allocated name string.
* @param ext Variable to receive the newly allocated extension
string.
*/
virtual void parseFilename(const char* fname,
char** dir,
char** name,
char** ext);

....
....
}
 
M

Markus Moll

Hi
* @param dir Variable to receive the newly allocated directory
string.
* @param name Variable to receive the newly allocated name string.
* @param ext Variable to receive the newly allocated extension
string.
*/
virtual void parseFilename(const char* fname,
char** dir,
char** name,
char** ext);

I reckon this is not your code, but notice however that in my opinion it's
poor design. parseFilename expects you to give a string containing a
filename as the first argument, followed by pointers to char pointers that
will hold the addresses of the corresponding dir/name/ext parts of that
filename upon successful return.
So you are expected to use it like that:

char *dir, *name, *ext;
MyClass mc; // default constructor available/reasonable?
mc.parseFilename("/usr/lib/libc.so", &dir, &name, &ext);
/* now dir points to a C-string containing the directory part, name to the
name and ext to a string giving the extension */

Problems with this:
- It's C-strings all over. C-strings aren't exactly bad, but they make code
error-prone and clumsy. Prefer std::string (declared in <string>)
- parseFilename accepts pointers to pointers where it had better take
references to these pointers: parseFilename("/file.ext", 0, 0, 0);
- parseFilename allocates memory, but obviously you have to deallocate it
(does MyClass offer any deallocation function for that purpose?). That's
bad because you don't know whether to use free or delete (or maybe
something completely different)

Markus
 
D

David Rubin

Markus Moll said:

[snip - use it like this]
char *dir, *name, *ext;
MyClass mc; // default constructor available/reasonable?
mc.parseFilename("/usr/lib/libc.so", &dir, &name, &ext);
/* now dir points to a C-string containing the directory part, name to the
name and ext to a string giving the extension */

Problems with this:
- It's C-strings all over. C-strings aren't exactly bad, but they make code
error-prone and clumsy. Prefer std::string (declared in <string>)

This assumes that parseFilename allocates memory. It does not have to.
- parseFilename accepts pointers to pointers where it had better take
references to these pointers: parseFilename("/file.ext", 0, 0, 0);

Conventional wisdom (i.e,. C++PL3ed) says not to pass by reference if
the value of the argument is going to be changed.
- parseFilename allocates memory, but obviously you have to deallocate it
(does MyClass offer any deallocation function for that purpose?). That's
bad because you don't know whether to use free or delete (or maybe
something completely different)

Nothing about parseFilename suggests that it allocates memory. In
fact, I would assume that it doesn't since it uses pointer-to-char
parameters. What is obviously missing here is the contract for this
function specifying what its semantics and behavior are.

Additionally, this function returns 'void' which is not helpful
insofar as determining whether the function succeeded, or if and how
the 'fname' argument is incorrectly formatted. 'int' would be a good
choice here.

/david
 
H

Howard

This assumes that parseFilename allocates memory. It does not have to.
Nothing about parseFilename suggests that it allocates memory. In
fact, I would assume that it doesn't since it uses pointer-to-char
parameters. What is obviously missing here is the contract for this
function specifying what its semantics and behavior are.


/david

Huh? Read the comments again. It most certainly *does* specify that it
alllocates new memory for those pointers. And that's exactly why it uses
char**, so that it can modify the pointers passed to it.

-Howard
 
D

David Rubin

[snip - comments]
Huh? Read the comments again. It most certainly *does* specify that it
alllocates new memory for those pointers. And that's exactly why it uses
char**, so that it can modify the pointers passed to it.

Somehow I missed that. My bad. Just goes to show how important
comments are since I had a much different idea about what the function
does without (apparantly) reading the comments. /david
 

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
474,183
Messages
2,570,965
Members
47,512
Latest member
FinleyNick

Latest Threads

Top