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
1 answer

Can I have a weak static pointer?

Can I have a static pointer that is weak in objective-c? I know it compiles, but I want to know if it will behave as I expect a weak pointer to behave. __weak static HMFSomeClass *weakStaticPointer;
Hackmodford
  • 3,901
  • 4
  • 35
  • 78
13
votes
5 answers

Difference between (*++argv)[0] and while(c = *++argv[0])

I have the following snippet of code: int main(int argc, char *argv[]) { char line[MAXLINE]; long lineno = 0; int c, except = 0, number = 0, found = 0; while(--argc > 0 && (*++argv)[0] == '-') //These two lines …
Tool
  • 12,126
  • 15
  • 70
  • 120
13
votes
3 answers

How exactly are interface variables implemented in Go?

In the below code snippet, I'd like to understand what exactly gets stored in iPerson when its contents are still uninitialized: just a value of 0-bytes? Or is it actually a pointer under the hood (and also initialized to 0-bytes of course)? In any…
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
13
votes
2 answers

C#: Wrapping one Enum inside another (ie. mirroring another enum/copying it...)

Here's my problem: I have an object that's referencing a DLL. I would like other objects to reference my object, without having to also include a reference to the DLL itself. This is fine for the most part except there is an enum in the DLL that I…
AlishahNovin
  • 1,904
  • 3
  • 20
  • 37
13
votes
3 answers

C++ smart pointers address

I am a little confused with smart pointers. In the following code, should the & operator return the adress of the smart pointer allocation or the address of the pointer it's controlling? main() { std::shared_ptr i = std::shared_ptr(new…
Lucas Kreutz
  • 369
  • 2
  • 4
  • 12
13
votes
6 answers

About sizeof of a class member function pointer

Let's say we have a class A class A; and these typedefs typedef void (A::*a_func_ptr)(void); typedef void (*func_ptr)(void); My question is why sizeof(a_func_ptr) returns 16, while sizeof(func_ptr) returns 4 (as for any pointer on x86 system)? For…
Alex G
  • 307
  • 3
  • 7
13
votes
8 answers

Getter and setter, pointers or references, and good syntax to use in c++?

I would like to know a good syntax for C++ getters and setters. private: YourClass *pMember; the setter is easy I guess: void Member(YourClass *value){ this->pMember = value; // forget about deleting etc } and the getter? should I use references…
Jonathan
  • 4,724
  • 7
  • 45
  • 65
13
votes
2 answers

Deleting all values from a QMap

I have a QMap consist of pointers to class objects, allocated using new. I need to delete all these pointers. What is the proper way of doing this with QMap ? I can do it this way: QList allVals = map.values(); for…
Littlebitter
  • 671
  • 3
  • 10
  • 19
13
votes
4 answers

Deletion of pointer to incomplete type 'Point'; no destructor called

I have 2 files: Point.h: class Point { int x; int y; char* name; public: Point() { name = new char[5]; } ~Point() { delete[] name; } }; and: Line.h: class Point; class Line { Point* p; public: Line() { p =…
Alon Shmiel
  • 6,753
  • 23
  • 90
  • 138
13
votes
2 answers

Will a modern processor (like the i7) follow pointers and prefetch their data while iterating over a list of them?

I want to learn how to write better code that takes advantage of the CPU's cache. Working with contiguous memory seems to be the ideal situation. That being said, I'm curious if there are similar improvements that can be made with non-contiguous…
Jonathan
  • 752
  • 1
  • 9
  • 19
13
votes
10 answers

Calling a C++ function pointer on a specific object instance

I have a function pointer defined by: typedef void (*EventFunction)(int nEvent); Is there a way to handle that function with a specific instance of a C++ object? class A { private: EventFunction handler; public: void SetEvent(EventFunction…
fryguybob
  • 4,390
  • 2
  • 28
  • 36
13
votes
3 answers

What are the semantics of C99's "restrict" with regards to pointers to pointers?

I am doing lots of matrix arithmetic and would like to take advantage of C99's restrict pointer qualifier. I'd like to setup my matrices as pointers to pointers to allow for easy subscripting, like so: int **A = malloc (ncols * sizeof(int *)); A[0]…
mbauman
  • 30,958
  • 4
  • 88
  • 123
13
votes
6 answers

Passing a string literal as a function parameter defined as a pointer

I am reading the chapter on arrays and pointers in Kernighan and Richie's The C Programming Language. They give the…
Dmitry Minkovsky
  • 36,185
  • 26
  • 116
  • 160
13
votes
2 answers

C pointers and arrays/ 'sizeof' operator

Possible Duplicate: Stack pointer difference for char pointer and array To illustrate my question: int main(void){ int myary[20]; int *myaryPtr; myaryPtr = myary; sizeof(myary); // Will it return 80? Correct? sizeof(myaryPtr);…
SystemFun
  • 1,062
  • 4
  • 11
  • 21
13
votes
3 answers

Delphi double to byte array

Is simple way to convert double value to its bytes representation? I tried using pointers like: var double_v:double; address:^double; .... double_v:=100; address:=@double_v; but every my concepts: how to read thise 8 bytes from address, end…
Artik
  • 803
  • 14
  • 36