7

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.

7
  • 5
    You understand the difference between variable names and variables, do you? May 15, 2009 at 20:25
  • 3
    I think you're misunderstanding pointers. A pointer is just the memory address at which the value it points to resides. The variable name used has absolutely no effect on this. Variable names are just a construct to make writing the code easier; they don't change the resulting binary.
    – Herms
    May 15, 2009 at 20:29
  • i voted up, because this is a good question imo May 15, 2009 at 20:33
  • 3
    @litb, please undelete your answer, it's excellent! I began an edit to explain compilation and linking, but aborted it, and then saw yours. I think it's just the information that Babiker needs. May 15, 2009 at 20:35
  • Herms, don't be too sure about that. names that have linkage (file-scope (C) / namespace-scope (C++) variables) are registered into the symbol table for relocation purposes normally. they can be stripped out with the "strip" tool, though May 15, 2009 at 20:36

9 Answers 9

23

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.

6
  • 3
    And to a small extent, the memory and time needed to compile.
    – Unknown
    May 15, 2009 at 20:25
  • 22
    The last thing we need is for developers to name their variables as short as possible with the intent to "optimize compile time".
    – rein
    May 15, 2009 at 20:28
  • @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 :( May 15, 2009 at 20:34
  • 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
    May 15, 2009 at 20:36
  • 1
    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. May 15, 2009 at 20:39
16

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
1
  • 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. May 15, 2009 at 20:47
9

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.

4

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. :)

5
  • 4
    There again, mamy people think that using hungarian in any shape or form is an extremely bad idea.
    – anon
    May 15, 2009 at 20:38
  • 2
    No, Microsoft no longer recommends hungarian notation, and most people think it's ugly after they realize just how flawed it is.
    – jalf
    May 15, 2009 at 20:42
  • 1
    Can you link to the place where they say they no longer recommend it?
    – i_am_jorf
    May 15, 2009 at 21:44
  • 2
    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
    May 15, 2009 at 22:50
  • 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.
    – i_am_jorf
    May 16, 2009 at 3:04
2

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.

1

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

1

Well I understand what you are asking here. Its good to remember that the variable names only affect the size of the text file and the time it took you to write that. But do remember that variable names and pointer names and function names etc.. all get lost in translation to machine code by the GNU GCC compiler. The final thing that will be stored in .o or .exe will be 0s and 1s, so you shouldn't worry about names being to big :)

0

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.

0

There are two critical points here that I would like to atempt to distill out:

  1. The length of a variable name has no impact on the space allocated for the data in the final compiled C/C++ program as the compiler comes up with its own names for variables.
    • N.B. Some compilers, especially the older ones, may only recognise the first few letters of a variable name so having very long overly complicated variable names in your source code may lead to clashes
    • Also, if you create symbol files for debugging they will contain all your variable names so will become unecessarily large but this might not significantly slow down debugging, I've never checked!
  2. The space needed to store a pointer may indeed be larger than the space needed than the data pointed to but there are still circumstances in which you may wish to use them in any case. For instance in an architecture such as COM where the practise is that functions return only a result code (success or failure) and all data that is changed is done through pointers passed on the stack:
    /*
        pszString could be pointing to only one character which
        is less space than the pointer
    */
    HRESULT OneLetterSplat(char * pszString)
    {
        *pszString = 'a';
        return SUCCESS
    }
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.