Where is int ptr defined




















Conversions between integers and pointers can have undesired consequences depending on the implementation. According to the C Standard, subclause 6. An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.

Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined.

The result need not be in the range of values of any integer type. Do not convert an integer type to a pointer type if the resulting pointer is incorrectly aligned, does not point to an entity of the referenced type, or is a trap representation.

Do not convert a pointer type to an integer type if the result cannot be represented in the integer type. See undefined behavior The mapping between pointers and integers must be consistent with the addressing structure of the execution environment.

Issues may arise, for example, on architectures that have a segmented memory model. The size of a pointer can be greater than the size of an integer, such as in an implementation where pointers are 64 bits and unsigned integers are 32 bits.

This code example is noncompliant on such implementations because the result of converting the bit ptr cannot be represented in the bit integer type:. The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. In this noncompliant code example, the pointer ptr is converted to an integer value.

The high-order 9 bits of the number are used to hold a flag value, and the result is converted back into a pointer. This example is noncompliant on an implementation where pointers are 64 bits and unsigned integers are 32 bits because the result of converting the bit ptr cannot be represented in the bit integer type.

A similar scheme was used in early versions of Emacs, limiting its portability and preventing the ability to edit files larger than 8MB. This compliant solution uses a struct to provide storage for both the pointer and the flag value. This solution is portable to machines of different word sizes, both smaller and larger than 32 bits, working even when pointers cannot be represented in any integer type.

It is sometimes necessary to access memory at a specific location, requiring a literal integer to pointer conversion. In this noncompliant code, a pointer is set directly to an integer constant, where it is unknown whether the result will be as intended:. The result of this assignment is implementation-defined , might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation.

A particular platform that is, hardware, operating system, compiler, and Standard C library might guarantee that a memory address is correctly aligned for the pointer type, and actually contains a value for that type. A common practice is to use addresses that are known to point to hardware that provides valid values. Converting from pointer to integer or vice versa results in code that is not portable and may create unexpected pointers to invalid memory locations.

Partially supported: reports casts from pointer types to smaller integer types which lose information. Checks for unsafe conversion between pointer and integer rule partially covered.

Search for vulnerabilities resulting from the violation of this rule on the CERT website. Key here explains table format and definitions. Key here for mapping notes. An e xample explaining the above two equations follows:. I would take what you have and move it to that rule.

I would add to that rule but honestly I think it is wrong. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. A platform-specific type that is used to represent a pointer or a handle. SerializableAttribute ComVisibleAttribute. Is this page helpful?

Yes No. Any additional feedback? Skip Submit. IntPtr Int A read-only field that represents a pointer or handle that has been initialized to zero. Add IntPtr, Int CompareTo IntPtr. CompareTo Object. Equals IntPtr.

Indicates whether the current object is equal to another object of the same type. Equals Object. Returns a value indicating whether this instance is equal to a specified object. Parse String. Parse String, IFormatProvider. Parse String, NumberStyles. Subtract IntPtr, Int Converts the value of this instance to a pointer to an unspecified type.

ToString IFormatProvider. ToString String. Formats the value of the current instance using the specified format. Tries to format the value of the current instance into the provided span of characters. TryParse String, IntPtr. Addition IntPtr, Int Equality IntPtr, IntPtr. Just what is an IntPtr exactly? Ask Question. Asked 12 years, 4 months ago. Active 11 months ago. Viewed k times. Improve this question. Callum Rogers Callum Rogers Add a comment. Active Oldest Votes. Improve this answer.

Sam Harwell Sam Harwell A pointer is something that points to an address in memory. In managed languages you have references the address can move around while in unmanaged languages you have pointers the address is fixed — Colin Mackay. In general across programming languages , a pointer is a number that represents a physical location in memory. A null pointer is almost always one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system.

Trap "A platform-specific type that is used to represent a pointer or a handle. IntPtr makes perfect sense, because it's an integer as in mathematical integer and a pointer as in pointer. The Int part has little to do with the int type - it's a concept, not a specific type. Though to be fair, the only thing the CLI spec says about it is that it is indeed an "Integer, native size". Oh well : — Luaan. Show 4 more comments. Daniel Earwicker Daniel Earwicker k 35 35 gold badges silver badges bronze badges.

Good point about detecting the size of address space for the process. Noldorin True, but not necessarily reliable. In the past, there have been plenty of architectures that had multiple pointer types, and on Windows, IntPtr is also used to represent handles which are bit regardless of architecture though Size still says 8 in that case.

The CLI spec only notes that it's an integer that is of "native size", but that doesn't say much, really. Luaan that doesn't really change anything in my answer does it? IntPtr is so called and is used throughout CLR source as a value large enough to hold a memory address. It can hold smaller values of course. Some architectures have multiple pointer types, but must have one that is the largest of the set.

DanielEarwicker Well, it's not a problem with any current. NET implementation, as far as I'm aware. However, the historical issue isn't just about size - the various pointers may be entirely incompatible. In an example closer to today, PAE would use bit addresses even though the "native pointer size" was still bit.

It goes all the way back to the argument "what does 'bit system' mean, really? Even though you can't actually address bits of physical memory with your bit "pointers". It's a mess. Some of it's complicated, but IntPtr is still "a value type large enough to store a memory address".

It's not as large as the largest hardware register, to take one of your examples. That's just not what it's for. It's large enough to represent a memory address.

Then there's stuff like this: stackoverflow. Here's an example: I'm writing a C program that interfaces with a high-speed camera. Pang 8, gold badges 82 82 silver badges bronze badges.

So the IntPtr simple allows you to use an unmanaged pointer like that used in your camera driver in managed code? Well in this case, most likely the camera driver uses unmanaged drivers under the hood, but in order to operate properly in the Managed-only world it provides the IntPtr to allow me to work with the data safely. So why wouldn't it just give you back a stream byte array? Why is it unsafe or unable to return the value?

A direct interpretation An IntPtr is an integer which is the same size as a pointer. An alternative perspective An IntPtr is a pointer with two limitations: It cannot be directly dereferenced It doesn't know the type of the data that it points to.



0コメント

  • 1000 / 1000