-4

Possible Duplicate:
At least the first 31 ,or 63 characters of an internal name are significant?

Here's a direct quote from the Book (K&R, 2nd ed, p. 35):

"At least the first 31 characters of an internal name are significant. For function names and external variables, the number may be less than 31, because external names may be used by assemblers and loaders over which the language has no control. For external names, the standard guarantees only for 6 characters and a single case."

and in c99 there is no length limitation on its internal names, but only the first 63 are significant.

My question is why are these limits specifically 31 or 63? Why this number specifically why not 19,24 or any other number? If it's an implementation issue, is there a benefit from making it 31 or 63?

Community
  • 1
  • 1
HATEM EL-AZAB
  • 331
  • 1
  • 3
  • 11

1 Answers1

4

It's so the length can be a power of two, even including NUL-termination.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • and what is the advantages from make being power of two – HATEM EL-AZAB Apr 15 '12 at 17:32
  • @HATEMEL-AZAB, it tends to be more efficient. For instance, often, the actual chunks malloc returns are in units of a power of two. – Matthew Flaschen Apr 15 '12 at 17:35
  • @Matthew: That's a myth. There's no benefit to allocators which return power-of-two-sized chunks only, and tons of disadvantages. Anybody still doing that is living in the 70s, and unlike some implementation practices where "living in the 70s" has at least some value, this particular one has no value. – R.. GitHub STOP HELPING ICE Apr 15 '12 at 17:52
  • 1
    Ehm, at least recently @R.. uCLibc allocator was living in the 70s. If you are surrounded by crazy, sometimes it's best to act crazy yourself... :-) – Prof. Falken Apr 15 '12 at 18:43
  • @R.., I didn't mean an allocator that returned only power-of-two-size chunks. Rather, one that returned multiples of a power of 2. E.g. if the unit is 16, it could return 16 bytes, or a multiple thereof (32, 160, etc.). Either way, I am not saying all allocators are like that. For instance, AIX 3.1 had a power-of-two-sized chunk only [policy](http://publib.boulder.ibm.com/infocenter/pseries/v5r3/index.jsp?topic=/com.ibm.aix.genprogc/doc/genprogc/sys_mem_alloc.htm), and it was released after C89, and well after the 70's (1990). – Matthew Flaschen Apr 15 '12 at 18:57
  • @AmigableClarkKant: uClibc's default allocator is dlmalloc, which, essentially, is the only reasonable/viable malloc algorithm. (Various tweaks/variants are possible, but its basic idea of sorting free chunks by lists of chunks of the same size is the only known reasonable algorithm.) It has two other options, one of which might suck that bad; I'm not sure. – R.. GitHub STOP HELPING ICE Apr 15 '12 at 19:17
  • @Matthew: Then the issue is not being a power of two, but being a multiple of the alignment (so typically a multiple of 8, 16, or 32, depending on the machine). – R.. GitHub STOP HELPING ICE Apr 15 '12 at 19:18
  • the answer means that every thing should be taken as power of 2 ,but for some language the first 6 characters are significant is this is power of 2? – HATEM EL-AZAB Apr 15 '12 at 20:21
  • @R.., right, that's basically what my first comment meant, though I don't know if all such malloc implementations use the alignment as the unit. However, AIX 3.1 in fact did what you said (always an exact power of 2, not just a multiple of one). – Matthew Flaschen Apr 15 '12 at 22:15
  • @HATEMEL-AZAB, I never said every limit is a power of 2. 6 obviously isn't. – Matthew Flaschen Apr 15 '12 at 22:17
  • @R.., at least the one I was subjected to was power of 2. I think we fixed that once we switched from vendor ported version of old Snapgear tree. :-) – Prof. Falken Apr 16 '12 at 07:16
  • I believe these are what one calls "buddy system allocators". Despite the (fading, but still present) hype about their instructional value for simplicity, they're no simpler than the basic dlmalloc algorithm, much slower (O(log n) for free instead of O(1)), and fragment memory much worse. This is a case where putting the extra constraint (power of 2 "buddies") that was intended to be something you could take advantage of really creates *more work* for you rather than less. – R.. GitHub STOP HELPING ICE Apr 16 '12 at 10:03