tags:

views:

376

answers:

8
+3  Q: 

C++ pointer names

I understand pointers are used to point to objects so you would have to the same around in a program. But were and how are pointer names stored. Would it be an overkill to declare a pointer name that occupies more resource than the object it points to for example:

int intOne = 0;
int *this_pointer_is_pointing_towards_intOne = &intOne;

I know this is a ridiculous example but i was just trying to get the idea across.

Edit: the name of the pointer has to be stored somewhere taking more bytes than the address of the pointed at object.

+4  A: 

Microsoft uses the "p" prefix to indicate a pointer:

int intOne = 0;
int* pIntOne = &intOne;

They actually use Hungarian on everything.

It works pretty well once you get used to seeing it. Many people think it is ugly at first.

Whatever you decide, I think it's valuable to denote that something is a pointer type in its name, though I wouldn't go quite so far as your example. :)

jeffamaphone
There again, mamy people think that using hungarian in any shape or form is an extremely bad idea.
anon
No, Microsoft no longer recommends hungarian notation, and most people think it's ugly after they realize just how flawed it is.
jalf
Can you link to the place where they say they no longer recommend it?
jeffamaphone
Sticking "p" at the front of the name isn't a terribly bad idea. It's when you start getting lpsczCthulu that you need to worry about summoning Great Old Ones from the Deep.
Zan Lynx
Oh I completely agree, the limit should be three. The worst I would ever do is psz (for both char* and WCHAR*, btw). It's good to see psz vs. sz because you can call ARRAYSIZE() on sz's but not psz's. I would never bother with the c's and m_'s are completely worthless.
jeffamaphone
+20  A: 

The length of the variable name doesn't have any effect on the size of your program, just the length of time it takes to write the program.

overslacked
And to a small extent, the memory and time needed to compile.
Unknown
The last thing we need is for developers to name their variables as short as possible with the intent to "optimize compile time".
rein
And, with good code completion, your don't even notice it.
Trillian
i love how you put it.
Johannes Schaub - litb
@rein: Indeed, the instructor who teaches Software Engineering at our college has said long var names have the disadvantage of making the compile time longer and I was like :-O when I heard that. I have to stand these stupid guys who teach here :(
Mehrdad Afshari
Oh, it will make compile-time longer. The compiler has to do string compares when compiling, so the length of the variables will affect it. However, the difference shouldn't really be noticeable.
Herms
I understand, but the guy who told this was thinking in 1960s and the WORST part is that he teaches "software engineering" and should promote writing maintainable software, not recommend optimizing for compile time.
Mehrdad Afshari
If it was the difference between 4 hours and 15 minutes, I too would name all my variables A, B, Y, X and I.
Zan Lynx
A: 

Well pointers are most often used for things that are allocated dynamically, and/or functions.

In the case of dynamic allocation, you can just precede it with ptr, if you're insistent on using this notation. In the case of functions, fun works just as well.

Andrei Krotkov
+9  A: 

Pointer names are not stored. The pointer name (or any variable name for that matter) are not compiled into the final binary (provided you don't compile with symbols set to on).

Pointers are simply integers (or longs) that are stored in memory which, in turn, point to the item they are pointing to in some location in memory.

rein
+15  A: 

The name of local variables are only needed for the compiler to find the variables you want to refer to. After compiling, those names usually are erased and completely replaced by numeric symbols or equivalents. This happens for all names that have no linkage practically (of course if you do a debug build, things may be different). So, the same is true for function parameters.

The name of global variables, for example, can't be erased, because you may use it from another unit in your program, and the linker has to be able to look it up. But after your program has been linked, even the name of those can be erased.

And after all, these do not occupy runtime memory. Those names are stored in a reallocation table for the purpose of linking (see the strip program how to remove those names).

But anyway, we are talking about a few bytes which are already wasted by alignment and whatnot. Compare that to the hell-long names of template instantiations. Try out this:

readelf -sW /usr/lib/libboost_*-mt.so  | awk '{ print length($0), $0 }' | sort -n
Johannes Schaub - litb
I just really like the way you mention global variables being kept until the linking stage... I think the OP has a few more courses to go before he'll understand it all, but it's good information.
overslacked
+2  A: 

I agree with previous posts but I would like to point out something around it. Sometimes people overuse pointers thinking that their use will automatically provide small memory footprints. This is not always the case. Consider this piece of code:

void myfunc(const char *var) {
    // Function body
}

A pointer to char will take 4 bytes in a 32 bits architecture while the char itself would usually take 1 byte. (Here var is assumed to point to a single byte, not to a string.) Can you see the point? On the other hand, you should always use pointers (or references) for complex objects:

void myfunc(const string &str) {
    // Function body
}

Of course, in case you want to modify the variable inside your function you should remove the const keyword.

Sergio
+1  A: 

I don't see a reason why it should be preceded with anything at all. Your compiler/editor will do the job for you.

navigator
A: 
Captain Lepton