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
57
votes
5 answers

What is the use of intptr_t?

I know it is an integer type that can be cast to/from pointer without loss of data, but why would I ever want to do this? What advantage does having an integer type have over void* for holding the pointer and THE_REAL_TYPE* for pointer…
Baruch
  • 20,590
  • 28
  • 126
  • 201
57
votes
1 answer

Why would code explicitly call a static method via a null pointer?

I've seen code like this in a couple of old projects: class Class { static void Method() {} }; ((Class*)0)->Method(); This code contains undefined behavior because it includes dereferencing a null pointer (no matter what happens afterwards).…
sharptooth
  • 167,383
  • 100
  • 513
  • 979
57
votes
4 answers

clearing a vector of pointers

Assume I have defined a class like this: class foo { private: std::vector< int* > v; public: ... void bar1() { for (int i = 0; i < 10; i++) { int *a = new int; v.push_back( a ); } }; void…
mahmood
  • 23,197
  • 49
  • 147
  • 242
57
votes
11 answers

Simulating Pointers in Python

I'm trying to cross compile an in house language(ihl) to Python. One of the ihl features is pointers and references that behave like you would expect from C or C++. For instance you can do this: a = [1,2]; // a has an array b = &a; // b points…
chollida
  • 7,834
  • 11
  • 55
  • 85
56
votes
4 answers

How do I create an array of pointers?

I am trying to create an array of pointers. These pointers will point to a Student object that I created. How do I do it? What I have now is: Student * db = new Student[5]; But each element in that array is the student object, not a pointer to the…
chustar
  • 12,225
  • 24
  • 81
  • 119
56
votes
10 answers

Is there any reason to check for a NULL pointer before deleting?

I often see legacy code checking for NULL before deleting a pointer, similar to, if (NULL != pSomeObject) { delete pSomeObject; pSomeObject = NULL; } Is there any reason to checking for a NULL pointer before deleting it? What is the reason…
yesraaj
  • 46,370
  • 69
  • 194
  • 251
56
votes
5 answers

Pointers as keys in map C++ STL

I have a question on how pointers to a custom object are handled when used as Keys in an map. More specifically if I define std::map< CustomClass*, int > foo; Would the default C++ implementation work to handle these pointers? Or do I need to…
Ammar Husain
  • 1,789
  • 2
  • 12
  • 26
56
votes
14 answers

Placement of the asterisk in Objective-C

I have just begun learning Objective-C, coming from a VB .Net and C# .Net background. I understand pointer usage, but in Objective-C examples I see the asterisk placed in several different places, and search as I might, I have not been able to find…
john_5101
55
votes
4 answers

Should I explicitly cast malloc()'s return value?

I wanted to ask about the following case: char *temp; temp = malloc(10); Since the return type of malloc is void*, will the pointer returned by the malloc be implicitly cast to char* type before being assigned to temp? What does the standard say…
mawia
  • 9,169
  • 14
  • 48
  • 57
55
votes
10 answers

Do all pointers have the same size in C++?

Recently, I came across the following statement: It's quite common for all pointers to have the same size, but it's technically possible for pointer types to have different sizes. But then I came across this which states that: While pointers are…
Jason
  • 36,170
  • 5
  • 26
  • 60
55
votes
10 answers

Pointer vs Variable speed in C++

At a job interview I was asked the question "In C++ how do you access a variable faster, though the normal variable identifier or though a pointer". I must say I did not have a good technical answer to the question so I took a wild guess. I said…
Jerry
  • 789
  • 1
  • 6
  • 10
55
votes
2 answers

Why do zero-sized types cause real allocations in some cases?

I was playing with zero-sized types (ZSTs) as I was curious about how they are actually implemented under the hood. Given that ZSTs do not require any space in memory and taking a raw pointer is a safe operation, I was interested what raw pointers I…
Danila Kiver
  • 3,418
  • 1
  • 21
  • 31
55
votes
8 answers

Array-syntax vs pointer-syntax and code generation?

In the book, "Understanding and Using C Pointers" by Richard Reese it says on page 85, int vector[5] = {1, 2, 3, 4, 5}; The code generated by vector[i] is different from the code generated by *(vector+i) . The notation vector[i] generates machine…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
55
votes
1 answer

initialize string pointer in struct

Go Newbie question: I am trying to init the following struct, with a default value. I know that it works if "Uri" is a string and not pointer to a string (*string). But i need this pointer for comparing two instances of the struct, where Uri would…
Mandragor
  • 4,684
  • 7
  • 25
  • 40
55
votes
4 answers

Golang basics struct and new() keyword

I was learning golang, and as I was going through the chapter that describes Structures, I came across different ways to initialize structures. p1 := passport{} var p2 passport p3 := passport{ Photo: make([]byte, 0, 0), Name: "Scott", …
scott
  • 1,557
  • 3
  • 15
  • 31