Alternative to #define VARNAME "text".....

Started by
13 comments, last by Splinter of Chaos 15 years, 9 months ago
Quote:Original post by BeanDog
Quote:Original post by Splinter of Chaos
If I understand what you want, you should use a pointer. Technically, it's an integer that can be set to constant that can be substituted for the value of a string.

Ah, where's Zahlman when you need him to explain the deep doctrine of the difference between integers and pointers?


Technically, I'm the minion in charge of that doctrine [wink]

Splinter of Chaos: there are two ways to look at the "pointers are integers" statement. The first way is to notice that pointers and integers behave differently: pointer arithmetic and comparison is based on a buffer-offset definition, which means it's unsafe to compare the order of pointers to objects that are not within the same buffer, or to add random offsets to pointers. Sure, pointers can be converted to and from integers, but it's not a morphism (the various operations on integers don't match the operations on their associated pointers). In that perspective, pointers and integers are not the same.

The other way is to notice that pointers are represented, at the machine level, as a sequence of bits. On many platforms, this sequence has the same size and storage locations as an integers (though this is not necessarily the case). And so, like 99% of any data inside a computer, pointers are actually integers. This is, of course, not really useful because of the generality of that statement.


Advertisement
Quote:Original post by Splinter of Chaos
Technically, it is a whole number, or integer that represents a place in memory.


If you want to make that argument, then technically an address is, at some level, an integer. That is, it is a whole number.

A pointer T* is a data type which represents an address, and is not synonymous to the data type int.
Quote:Original post by Driv3MeFar
If you want to make that argument, then technically an address is, at some level, an integer. That is, it is a whole number.


Even that isn't accurate. An address may have more complicated structure than that. For example, on x86 processors an address is actually two numbers: a segment and and offset into that segment. In the old days of DOS, the memory model worked with 20 address lines and the segment was 16 bits and the offset was also 16 bits. This meant that a far pointer, a pointer that contained both segment and offset, was 32 bits, but multiple pointer values could be used to refer to the same memory block. Pointers are an abstraction that may or may not be mappable to simple integer operations, which is one reason why various programming standard have limitations of valid pointer operations. Ex: the limitation on pointers pointing to one past the end of an allocated memory block. In a flat memory model this may seem like a pretty stupid restriction. However, in the DOS segment:offset memory model, pointing two past an allocated memory block may actually wrap around and end up pointing to the beginning of the memory block.

More to the point, using a pointer to a string literal as a unique identifier for that string is pretty useless since dynamically allocated strings with the same string value will not share the same pointer value for the string. Even two string literals from different parts of the program with the same value may have different pointer values. If you want to map a string value to an integer, use a hash function.
/whistles innocently :)
Quote:Original post by Zahlman
/whistles innocently :)


I think I'm going to take that position.

This topic is closed to new replies.

Advertisement