Questions tagged [pointers]

Data types for "pointing" at other values: A pointer's value is a memory address where the pointed-to value is stored. This tag should be used for questions involving the use of pointers, not references. Common programming languages using pointers are C, C++, Go, and assembly and intermediate-representation languages; use a specific language tag. Other helpful tags should describe what is being pointed-to (e.g. a function, a struct etc.)

A pointer is a data type that "points to" another value stored in memory using its address. Using a pointer rather than the pointed-to entity often holds performance benefits in repetitive operations. For example, copying a pointer is very often "cheaper" than copying the value that it points to: The pointee may be a large data structure, or its copying may be non-trivial, involving memory-location dependent values, while with a pointer - only the address must be copied.

Pointers are an important concept in many high-level programming languages, including C and C++. The Wikipedia page on pointers has a fairly in-depth introduction to the concept.

Please consult some of the following content for a more in-depth explanation of pointers.

Books

See also

56155 questions
13
votes
3 answers

How do the operators < and > work with pointers?

Just for fun, I had a std::list of const char*, each element pointing to a null-terminated text string, and ran a std::list::sort() on it. As it happens, it sort of (no pun intended) did not sort the strings. Considering that it was working on…
user1481860
13
votes
13 answers

Determining Whether Pointer is Valid

It has been my observation that if free( ptr ) is called where ptr is not a valid pointer to system-allocated memory, an access violation occurs. Let's say that I call free like this: LPVOID ptr = (LPVOID)0x12345678; free( ptr ); This will most…
Jim Fell
  • 13,750
  • 36
  • 127
  • 202
13
votes
2 answers

Is it safe to convert a pointer to typed/sized enum to a pointer to the underlying type?

The following code: void f(const uint8_t* a) {} // <- this is an external library function enum E : uint8_t { X, Y, Z }; int main(void) { E e = X; f(&e); // <- error here } Produces the following error: /tmp/c.cc:10:3: error: no matching…
Luis
  • 1,210
  • 2
  • 11
  • 24
13
votes
5 answers

Where does rend point to?

To support STL's notion of half-open ranges, we are allowed to point one-past-the-end of an array. Suppose we have a vector of three elements. If std::vector::iterator is implemented as a pointer, as is usually the case in release builds, then begin…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
13
votes
4 answers

What is a `char*`?

What is a char*, exactly? Is it a pointer? I thought pointers had the asterisk before the identifier, not the type (which isn't necessarily the same thing)...?
Maxpm
  • 24,113
  • 33
  • 111
  • 170
13
votes
3 answers

Address of variable needs to be loaded into memory?

I have an issue - with the following code I am trying to find out what is stored at a certain address and how long my static variable is stored at this specific position. (I read that static variables are stored infinitely and was quite surprised -…
Maik Ro
  • 320
  • 1
  • 11
13
votes
1 answer

How to prevent returning a pointer to a temporary variable?

On a recent bug hunt, I found an issue with returning a pointer to a member of a temporary variable. The offending (simplified) code was: struct S { S(int i) : i(i) {} int i; int* ptr() { return &i; } }; int* fun(int i) { return…
13
votes
5 answers

How to initialize a pointer to a specific memory address in C++

An interesting discussion about this started here but no one have been able to provide the C++ way of doing: #include int main(void) { int* address = (int *)0x604769; printf("Memory address is: 0x%p\n", address); *address =…
karlphillip
  • 92,053
  • 36
  • 243
  • 426
13
votes
7 answers

3D array C++ using int [] operator

I'm new to C/C++ and I've been cracking my head but still got no idea how to make an "structure" like this It's supposed to be a 3D dynamic array using pointers. I started like this, but got stuck there int x=5,y=4,z=3; int ***sec=new int…
Marco Aviles
  • 5,516
  • 6
  • 33
  • 47
13
votes
5 answers

The nullptr and pointer arithmetic

Considering the following code, is it safe to do pointer arithmetic on nullptr? I assume adding any offsets to a nullptr results in another nullptr, so far MSVC produce results as I expected, however I am a bit unsure about whether using nullptr…
user2188453
  • 1,105
  • 1
  • 12
  • 26
13
votes
5 answers

Confusion with == EOF vs feof

I opened a file, the stream is found at the address of pointer ptr. I am attempting to see whether or not a file is blank. Using the following if (fgetc(ptr) != EOF) works as expected. When the file is blank, the statement is not executed. When the…
user2587754
13
votes
5 answers

Why does the compiler assume that these seemingly equal pointers differ?

Looks like GCC with some optimization thinks two pointers from different translation units can never be same even if they are actually the same. Code: main.c #include #include int a __attribute__((section("test"))); extern int…
Yuri Syro
  • 549
  • 2
  • 16
13
votes
4 answers

What makes Swift's "Optional" safer than Objective-C's "nil"?

Apple seems to claim that the Optional type in Swift is safer than nil in Objective-C, but I don't understand why this is so. What are the salient differences in implementation that make Optionals safer than nil, and how will this affect my code?
Pratap Vhatkar
  • 691
  • 10
  • 21
13
votes
1 answer

Why would it be necessary to perform two casts to a mutable raw pointer in a row?

When looking at unix-socket, I came across this code: let timeout = unsafe { let mut timeout: libc::timeval = mem::zeroed(); let mut size = mem::size_of::() as libc::socklen_t; try!(cvt(libc::getsockopt(self.0, …
DeBe
  • 491
  • 3
  • 15
13
votes
1 answer

Understanding code in strlen implementation

I have two questions regarding the implementation of strlen in string.h in glibc. The implementation uses a magic number with 'holes'. I am not able to understand how this works. Can someone please help me understand this snippet: size_t strlen…
Neo
  • 381
  • 3
  • 15