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
14
votes
6 answers

What's the difference between void* and void**?

It's the special property that void* can also be assigned a pointer to a pointer and cast back and the original value is received. I read this line somewhere. Does it means void* and void** are same? What is the difference? Edit void* can hold any…
Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
14
votes
3 answers

Trying to assign vector of Base* from vector of Derived*

This seems like a pretty basic problem, but I can't figure it out. I have a std::vector of raw pointers to Derived objects, and I just want to copy it to another vector of Base pointers using the assignment operator. With VC++ I get error C2679…
Carlton
  • 4,217
  • 2
  • 24
  • 40
14
votes
5 answers

Why can operator-> be overloaded manually?

Wouldn't it make sense if p->m was just syntactic sugar for (*p).m? Essentially, every operator-> that I have ever written could have been implemented as follows: Foo::Foo* operator->() { return &**this; } Is there any case where I would want…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
14
votes
5 answers

How do game trainers change an address in memory that's dynamic?

Lets assume I am a game and I have a global int* that contains my health. A game trainer's job is to modify this value to whatever in order to achieve god mode. I've looked up tutorials on game trainers to understand how they work, and the general…
GameTrainersWTF
  • 235
  • 1
  • 2
  • 6
14
votes
3 answers

Passing member function pointer to the c-style function

I am trying to pass member function pointer to the c-style function (as it's lib in C) The pointer it wants is defined as: void (*)(int, const char*) So the function I am trying to pass is: void Application::onError(int error, const char…
petomalina
  • 2,020
  • 2
  • 19
  • 25
14
votes
1 answer

X11: How do I REALLY grab the mouse pointer?

I've implemented a horizontal splitter widget in Xlib. I'm trying to grab the mouse when the user clicks & drags on the splitter bar (so that the user can dynamically move the split & thus resize the windows on either side of the splitter…
Drew Hall
  • 28,429
  • 12
  • 61
  • 81
14
votes
13 answers

At What point should you understand References?

I asked a question like this in an interview for a entry level programmer: var instance1 = new MyObject{Value = "hello"} var instance2 = instance1; instance1.Value =…
Vaccano
  • 78,325
  • 149
  • 468
  • 850
14
votes
5 answers

Reference to Array vs reference to array pointer

void check(void* elemAddr){ char* word = *((char**)elemAddr); printf("word is %s\n",word); } int main(){ char array[10] = {'j','o','h','n'}; char * bla = array; check(&bla); check(&array); } Output: word is john RUN…
Ioane Sharvadze
  • 2,118
  • 21
  • 35
14
votes
1 answer

Memory allocator with custom pointer type

I tried to create a custom memory allocator which uses a smart pointer. I do not post the code because it's too big and doesn't add much of information. Then I tested it with a std::vector. It works perfectly well on Xcode. But when I tried to build…
MaksymB
  • 1,267
  • 10
  • 33
14
votes
6 answers

Why is using vector of pointers considered bad?

Recently I've met with opinion that I shouldn't use vector of pointers. I wanted to know - why I cant? For example if I have a class foo it is possible to do this: vector v; v.push_back(new foo()); I've already seen some people down voting…
Patryk Krawczyk
  • 1,342
  • 1
  • 14
  • 25
14
votes
3 answers

Misunderstanding of atomic structs and pointers

My first question is: Is there any way to access the members of struct in an atomic object? For example, I get the compiler error: struct std::atomic’ has no member named ‘data’ a.data = 0; in this segment struct node{ int data; …
Matt Pennington
  • 560
  • 1
  • 8
  • 21
14
votes
3 answers

When should I provide a destructor for my class?

This seems like a rather trivial or at least common question, but I couldn't find a satisfying answer on google or on SO. I'm not sure when I should implement a destructor for my class. An obvious case is when the class wraps a connection to a file,…
Aviv Cohn
  • 15,543
  • 25
  • 68
  • 131
14
votes
4 answers

C: Extrapolating type from void pointer

Say a function takes a void pointer as an argument, like so: int func(void *p); How can we determine or guess the type of what p is pointing to?
Yktula
  • 14,179
  • 14
  • 48
  • 71
14
votes
6 answers

Modifying a const through a non-const pointer

I'm a bit confused what happened in the following code: const int e = 2; int* w = ( int* ) &e; // (1) cast to remove const-ness *w = 5; // (2) cout << *w << endl; // (3) outputs 5 cout << e…
jasonline
  • 8,646
  • 19
  • 59
  • 80
14
votes
4 answers

Declare and initialize pointer concisely (i. e. pointer to int)

Given pointers to char, one can do the following: char *s = "data"; As far as I understand, a pointer variable is declared here, memory is allocated for both variable and data, the latter is filled with data\0 and the variable in question is set to…
user3849273
  • 221
  • 1
  • 2
  • 7