473,386 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

pointer calling trouble

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);

....
....
}
Jul 22 '05 #1
4 1381
Hi

OzgurGul wrote:
* @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

Jul 22 '05 #2
Markus Moll <mo**@rbg.informatik.tu-darmstadt.de> wrote in message news:<41***********************@newsread4.arcor-online.net>...
Hi

OzgurGul wrote:
* @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);

[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
Jul 22 '05 #3

"David Rubin" <da********@warpmail.net> wrote in message
news:82*************************@posting.google.co m...
Markus Moll <mo**@rbg.informatik.tu-darmstadt.de> wrote in message
news:<41***********************@newsread4.arcor-online.net>...
Hi

OzgurGul wrote:
> * @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.
> */

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

Jul 22 '05 #4
"Howard" <al*****@hotmail.com> wrote in message news:<%M*********************@bgtnsc05-news.ops.worldnet.att.net>...

[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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: nifsmith | last post by:
Hi I am creating my own Queue class to learn about Queues and pointers. I have come across a question of two styles and I don't know if there are any dangers associated with them. I coded...
16
by: fix | last post by:
Hi all, I am new to C and I just started to program for a homework. I included my code down there. It is a fraction "class". I declared the Fraction struct, tried to create some invalid fraction,...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
10
by: Robert Palma | last post by:
I'm having trouble figuring out how to pass a pointer to a double array (1 dimensional) to a C function. Declaring array as: double xx; Declaring func. int process( double *input ) Calling...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
10
by: Ant | last post by:
Hi, I am having trouble with a member function pointer. It sees to give me the followign error Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. ...
8
by: toton | last post by:
HI, One more small doubt from today's mail. I have certain function which returns a pointer (sometimes a const pointer from a const member function). And certain member function needs reference...
1
by: harter.jim | last post by:
I am currently calling a 3rd party external library using P/Invoke. The library gives me a handful of functions that I need to call. I have been successful at calling many of them, but I am some...
30
by: Jess | last post by:
Hello, I tried a program as follows: include<iostream> using namespace std; class A{ public:
26
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.