A pointer is a data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address.

learn more… | top users | synonyms

3
votes
4answers
223 views

When should a function take a pointer for a collection to fill vs returning a pointer with a filled collection?

In C++ I frequently see these two signatures used seemingly interchangeably: void fill_array(Array<Type>* array_to_fill); Array<Type>* filled_array(); I imagine there is a subtle ...
106
votes
25answers
22k views

Are null references really a bad thing?

I've heard it said that the inclusion of null references in programming languages is the "billion dollar mistake". But why? Sure, they can cause NullReferenceExceptions, but so what? Any element of ...
1
vote
3answers
301 views

Do I have to fork a server program as an “C” child process in order for correct interprocess communication to take place?

I would like to implement interprocess communication between an Ubuntu Linux 15.10 mono 4.1.2 C# recorder client and Ubuntu Linux 15.10 mono 4.1.2 C# video server using a C++ mutex class and C++ ...
2
votes
2answers
5k views

Assigning strings to pointer in C

My question is about pointers in C. As far as I've learned and searched, pointers can only store addresses of other variables, but cannot store the actual values (like integers or characters). But in ...
24
votes
12answers
21k views

int* i; or int *i; or int * i; [closed]

What is your favorite method to declare a pointer? int* i; or int *i; or int * i; or int*i; Please explain why. see also: http://www.stroustrup.com/bs_faq2.html#whitespace
3
votes
2answers
268 views

Is it possible to have pointers recursively point into themselves?

I have been learning C++ recently, and upon reading up on pointers I had a moment of thought. I'm still attempting to grasp the very idea of pointers so excuse me if this doesn't make sense beyond ...
1
vote
2answers
127 views

Declaration confusion in pointers [closed]

I was just confused about the following declaration in C: char **p[5] I understand the char *p[] as an array of character pointers, but this one is puzzling me. Based on the precedence of [] over ...
0
votes
1answer
68 views

Containers and shared ownership within a class instance

I have two objects as member variables of a class. std::unique_ptr<Object> specificObject; std::vector<std::unique_ptr<Object>> objects; I know that specificObject will always be ...
25
votes
10answers
5k views

Isn't the use of pointer variables a memory overhead?

In languages like C and C++, while using pointers to variables we need one more memory location to store that address. So isn't this a memory overhead? How is this compensated? Are pointers used in ...
4
votes
6answers
1k views

Is it best to minimize using pointers in C?

I think most people would agree that pointers are a major source of bugs in C programs (if not the greatest source of bugs). Other languages drop pointers entirely for this reason. When working in C, ...
7
votes
1answer
465 views

Key / Value store development porting to modern C++

I am developing a database server similar to Cassandra. Development were started in C, but things became very complicated without classes. Currently I ported everything in C++11, but I am still ...
4
votes
3answers
266 views

C++ returning persistent objects

I'm currently trying to learn best practices in C++ after coming from a C# background. I understand that there are three ways of handling objects: By value (objects are copied or moved when passed ...
31
votes
10answers
17k views

Why are pointers not recommended when coding with C++?

I read from somewhere that when using C++ it is recommended not to use pointers. Why is pointers such a bad idea when you are using C++. For C programmers that are used to using pointers, what is the ...
22
votes
4answers
4k views

Is a memory of all possible permutations of a kilobyte block and pointers possible?

This is a hard enough idea to wrap my head around and I would greatly appreciate any edits/help to get it more readable for those in-the-know. Is it theoretically possible to have a hard drive that ...
3
votes
2answers
233 views

When is it ok to assert for a pointer being non-null?

This came up as part of a code review for a code segment which resembled this: auto somePikachu = GetMeAPikachu(); NT_ASSERT(somePikachu != nullptr); // this only fires on debug build ...
0
votes
0answers
62 views

Checking for null pointers or allowing a math library to segfault?

I've been going through the GNU GSL design document and came across this interesting line in the Test suites section: N.B. Don't bother to test for null pointers -- it's sufficient for the library ...
76
votes
4answers
43k views

How is a Java reference different from a C pointer?

C has pointers and Java has what is called references. They have some things in common in the sense that they all point to something. I know that pointers in C store the addresses they point to. Do ...
3
votes
3answers
357 views

Possible alternatives to copy constructors

In my C++ project I am relying on some libraries that do memory management for me. I make wrapper classes, for ease of use and memory safety, for example the class below. Note that this is a much ...
5
votes
3answers
1k views

Is it a common practice among professional programmers to avoid the use of raw pointers? [duplicate]

C++ tools and libraries have become more powerful than ever. For instance, we can replace arrays by vectors. We can replace pointers by references. We can use smart-pointers. Is it a common practice ...
3
votes
2answers
461 views

Why dynamic memory allocation functions in C returns void*?

Consider the prototypes of C's dynamic allocation functions malloc - void* malloc(size_t size); calloc - void* calloc(size_t n,size); realloc - void* realloc(void* ptr,size_t newsize); Now a ...
66
votes
32answers
17k views

What's a nice explanation for pointers? [closed]

In your own studies (on your own, or for a class) did you have an "ah ha" moment when you finally, really understood pointers? Do you have an explanation you use for beginner programmers that seems ...
1
vote
1answer
211 views

What does using pointer in Linux kernel imply?

So far I was under the perspective that while working in the kernel code, working with memory implies working with long integers and not pointers because dereferencing pointers shouldn't be done in ...
3
votes
1answer
274 views

What is the motivation for casting a pointer into a integer?

I'm doing some changes in the Linux kernel code and have noticed a pointer being cast into integer. Check out buf below (full code): snd_pcm_sframes_t snd_pcm_lib_read(struct snd_pcm_substream ...
21
votes
12answers
7k views

Why does void in C mean not void?

In strongly-typed languages like Java and C#, void (or Void) as a return type for a method seem to mean: This method doesn't return anything. Nothing. No return. You will not receive anything from ...
1
vote
2answers
183 views

shared or raw pointer c++

I am developing a noSQL database and currently moving the code from C to C++. It have class Pair, which contains a raw pointer to a memory blob. class Pair { Blob* blob; ... }; Then if you add ...
17
votes
4answers
15k views

In C++ why and how are virtual functions slower?

Can anyone explain in detail, how exactly the virtual table works and what pointers are associated when virtual functions are called. If they are actually slower, can you show the time that the ...
1
vote
0answers
352 views

Why C doesn't have better notation for pointers? [duplicate]

The * symbol is used for three different purposes multiplication Pointer declaration Pointer dereferencing. Why designers of C kept this notation? In other words, wouldn't it be nice if there ...
27
votes
10answers
8k views

What is the “type” of data that pointers hold in the C language?

I know that pointers hold addresses. I know that pointers' types are "generally" known based on the "type" of data they point to. But, pointers are still variables and the addresses they hold must ...
1
vote
2answers
182 views

“Sweep” a vector of pointers

I'm using a vector to store pointers to objects. In some cases I destroy one or more of these objects (setting the vector spaces to NULL after each delete call), which are externally selected: for ...
0
votes
2answers
152 views

Idiomatic C API with regards to pointers

I am trying to get a better understanding of how one would structure an API in C. I create a struct Person I have a init function that sets data on that struct I have multiple "helper" functions ...
6
votes
1answer
5k views

raw, weak_ptr, unique_ptr, shared_ptr etc… how to choose them wisely

There is a lot of pointers in C++ but to be honest in 5 years or so in c++ programmation (specifically with the Qt Framework) I only use the old raw pointer : SomeKindOfObject *someKindOfObject = ...
4
votes
1answer
449 views

The right way to remove an item from a linked list

In this Slashdot interview Linus Torvalds is quoted as saying: I've seen too many people who delete a singly-linked list entry by keeping track of the "prev" entry, and then to delete the entry, ...
3
votes
5answers
1k views

Storing a pointer to an argument passed by (non-const) reference

When designing an interface for passing objects which are meant to be stored for later use and which should not be 'null', I am always a bit uncertain if the argument should be passed by reference or ...
11
votes
4answers
1k views

If you favor “T *var”, do you ever write “T*”? [duplicate]

Possible Duplicate: int* i; or int *i; or int * i; Thinking about where we place our asterisks; how do those that prefer to keep the "pointerness" away from the type and with the identifier ...
2
votes
2answers
1k views

Disadvantages of Pointers [closed]

I was wondering why pointers are not included in modern languages now-a-days. I already did research on this on internet, and found out few theories/reasons: Memory leakage is the biggest concern ...
55
votes
17answers
7k views

What programming problems are best solved by using pointers? [closed]

Well, I basically understand how to use pointers, but not how best to use them in order to do better programming. What are good projects or problems to resolve involving the use of pointers so I can ...
7
votes
2answers
267 views

Autoreleasing objects in Reference Counting Systems

I'm experimenting a bit in C and I'm trying to implement my own Reference Counting System. I've mainly worked with Objective-C in the past but AFAIK autoreleasing objects is something that is unique ...
0
votes
2answers
576 views

C Linked List Implementation Pointer vs. Pointer-to-Pointer Consistency

To get some practice in C, I'm writing some basic functions for operating on a linked list of ints. I started out with functions that accepted as a "list" a pointer to the head node. Now, I find ...
0
votes
3answers
1k views

C++ Chess board design and smart pointers [closed]

I wrote a Chess engine in Java and I am porting it over to C++. I am new to C++. The idea: I have a Board object which holds a 2-dimensionnal array of Piece objects. Queen, Rook, Bishop, etc are ...
1
vote
4answers
629 views

How to work around Java's lack of pointers to pointers when working with linked data structures? [closed]

I've learned from a textbook how to implement binary search trees recursively in Java, and am working on implementing them nonrecursively. I've found a simple and elegant way to implement an insert ...
2
votes
6answers
1k views

What are memory addresses? [closed]

I have more or less 0 knowledge in low-level topics, so forgive my possible ignorance. I know that in languages such as C, pointers hold 'memory addresses', i.e. strings (or binary data?) written in ...
0
votes
1answer
127 views

In C++11 can Auto be used to get around mis-casting of void * parameters in functions?

If I have a generic function that takes a void* as a parameter, can Auto in C++11 help stop any bad casting of that parameter? For instance, we may have an event system that sends events and a ...
21
votes
3answers
7k views

Why Increment Pointers?

I just recently started learning C++, and as most people (according to what I have been reading) I'm struggling with pointers. Not in the traditional sense, I understand what they are, and why they ...
11
votes
4answers
2k views

Pointer indexing

I am currently reading a book titled "Numerical Recipes in C". In this book, the author details how certain algorithms inherently work better if we had indices starting with 1 (I don't entirely follow ...
1
vote
2answers
570 views

Can a version of Python's shelve module that knows when its entries have been modified be written in any programming language?

shelve is a Python module that makes it easy to persist a Python dictionary to disk (under the right conditions). The documentation for shelve gives this example to demonstrate one of the pitfalls of ...
8
votes
8answers
22k views

What are use cases and advantages of pointers? [closed]

I often struggle to see the advantages of pointers (except for low level programming). Why use of char* instead of a String or char[] or what advantages pointer arithmetic brings. So what are the ...
1
vote
3answers
6k views

usage of double pointers and n pointers?

I am familiar with basic C pointers. Just wanted to ask what is the actual use of double pointers or for that matter n pointer? #include<stdio.h> int main() { int n = 10 , *ptr , **ptr_ptr ; ...
0
votes
2answers
283 views

Usage of raw pointers

So from the top-voted answer to this question, it appears that using raw pointers for storage isn't frowned upon so much. However, then what is the point of a std::weak_ptr? I thought that storage was ...
5
votes
5answers
2k views

What's so bad about pointers in C++?

To continue the discussion in Why are pointers not recommended when coding with C++ Suppose you have a class that encapsulates objects which need some initialisation to be valid - like a network ...
18
votes
10answers
9k views

What is so difficult about pointers/recursion? [closed]

In the perils of java schools Joel discusses his experience at Penn and the difficulty of "segmentation faults". He says [segfaults are difficult until you] "take a deep breath and really try ...