6

I have a function where I'm passing in an iterator to a char * buffer (which is also a char *). The function needs to increment the iterator. Anyway, I found that a good method of passsing the iterator into the function is by passing the pointer by reference:

bool myFunction(unsigned char * &iter) { ... 

However, I've heard that this is bad form and could cause problems. Here is the method that my coworker suggested I use:

typedef unsigned char * unsignedcharptr; bool myFunction(unsignedcharptr &iter) { ... 

It looks to me like they're both doing the same thing. Is there a compiler difference between these two methods (I'm using Visual Studio 2005)? Which is correct?

8
  • 4
    I would rather consider typedefs for pointer types bad form to cause problems, but well, for the compiler there isn't any difference. Commented Feb 10, 2012 at 16:53
  • why not return a distance to increment by instead of modifying the pointer? Commented Feb 10, 2012 at 16:54
  • 1
    Semantically there is no difference, but yes second one is mostly preferred, typededs are easy to read. Commented Feb 10, 2012 at 16:55
  • typedef creates an alias for an existing type, and used for readability purposes mostly. Commented Feb 10, 2012 at 16:55
  • 1
    @Mr.Anubis Well, just until you forget that iter is a pointer and not an object. A * just says so much more than a ptr inside a long type name. Commented Feb 10, 2012 at 17:03

2 Answers 2

7

I don't think there's any difference between the two. You should ask your coworker why he believes there is.

What might be the cause is for maintainability, where if you wanted to switch from unsigned char * to char * you'd only have to change one place. But this, I think, could lead to other problems.

Sign up to request clarification or add additional context in comments.

3 Comments

@Adrian that's subjective though. I find unsigned char* more readable than unsignedintptr, while some comments suggest otherwise. What do you think?
Nah, it wasn't about readability. I think she thinks that it would cause memory issues.
@JoeLyga I don't know what you both think exclusively xD but she got that wrong if you talk about your particular above code example.
5

Is there a compiler difference between these two methods (I'm using Visual Studio 2005)?

As others have correctly noted, "no".

Which is correct?

Between the two alternatives, it entirely comes down to the "should I hide pointers behind typedefs" debate. There are valid arguments for either position

However, I think that both of your code snippets suffer from over-specialization. I prefer to code algorithms as template functions so that I Don't Repeat Myself.

If your design supports it, you could generalize your code to accept any iterator:

template <class InputIterator> bool myFunction(InputIterator &iter) { std::cout << *iter; ++iter; return true; } 

1 Comment

So how is one version better than the other?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.