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.
25
votes
10answers
4k 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
3answers
199 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 ...
20
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
220 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
54 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 ...
3
votes
3answers
332 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 ...
7
votes
1answer
366 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 ...
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 ...
1
vote
1answer
131 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 ...
2
votes
1answer
203 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 ...
3
votes
2answers
297 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 ...
1
vote
2answers
170 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 ...
26
votes
10answers
7k 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
0answers
346 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 ...
1
vote
2answers
151 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
132 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 ...
5
votes
1answer
3k 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
225 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, ...
1
vote
2answers
908 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 ...
7
votes
2answers
263 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
461 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
946 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
576 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 ...
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 ...
2
votes
6answers
927 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 ...
21
votes
12answers
6k 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 ...
0
votes
1answer
121 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
6k 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 ...
-2
votes
2answers
4k views
Assigning strings to pointer in C Language
I am a new learner of C language, my question is about pointers. As far i learned and searched pointers can only store addresses of other variables, but cannot store the actual values(like integers or ...
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
476 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 ...
0
votes
3answers
4k 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
282 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 ...
0
votes
5answers
237 views
What conclusion to be drawn from no difference in generated assembly from 2 rather different programs?
I might not have had a counterexample when I got the reply "A pointer is just an address, what's the difficulty?" but I didn't really buy such a simple explanation and at assembly code it's not ...
-1
votes
2answers
241 views
Difference between a pointer and a reference? [duplicate]
In Java and other high-level languages, a reference leads to an object.
In C++, as far as I know, a pointer can also lead to an object.
So what's the difference between a reference leading to an ...
0
votes
3answers
334 views
ANSI C pointers corrupted values [closed]
I am working on ANSI C and having some issues with the pointers.
That is that after a point in my program the pointer's values change without me interfering, is something like overwriting them.
I ...
2
votes
3answers
517 views
Why are pointers to literals not possible?
Reference to a literal is possible only if the reference is declared as constant.
But why is a pointer to a const object not possible in case of literals?
i.e.
int const& ref = 5;//
But why ...
24
votes
4answers
4k views
Why do C++ and Java both use the notion of “reference” but not in the same sense?
In C++ a reference argument to a function allows the function to make the reference refer to something else:
int replacement = 23;
void changeNumberReference(int& reference) {
reference = ...
1
vote
3answers
486 views
How do you read this line of code?
Forgive me for my poor English, me and my friend were doing school homework, suddenly he asked me to read this line of code ptr = &array[1][1][1] (ptr is a pointer to an integer).
He said
...
4
votes
2answers
555 views
Is it better to use an external variable or to pass around a pointer?
While writing in C, I have always wondered about when is the best time to use an external variable. I generally prefer to pass a pointer into a method. Is there a correct time to use an external ...
4
votes
3answers
249 views
adding array pointer in c
I am having problems understanding how this equation works in c:
char *sum(char *a, int b) {
return &a[b];
}
printf("%d", sum(5, 4));
I understand how arrays work, and I understand how to ...
3
votes
2answers
3k views
Storing a pass-by-reference parameter as a pointer - Bad practice?
I recently came across the following pattern in an API I've been forced to use:
class SomeObject
{
public:
// Constructor.
SomeObject(bool copy = false);
// Set a value.
void ...
5
votes
1answer
796 views
What are the real life use cases for tagged pointers? [closed]
What are the real life use cases for tagged pointers?
This is mostly coming from reading about small 64-bit systems and possible uses of 64-bit word pointers.
To my understanding tagged pointers are ...
1
vote
1answer
724 views
Pointer access and cache coherency
To my understanding, when you access a variable, that variable and the surrounding area of memory is put into the L1 cache. If I'm wrong here, please tell me.
Now my question is, say I have an array ...
3
votes
5answers
1k views
What is the definition of pointer? [duplicate]
Conceptually a "pointer" is just something that "points" to something else;Is this definition is sufficient to tell exactly what a pointer is in programing languages?
Does it need to have any other ...
2
votes
2answers
11k views
Do pointers really exist in Java? [duplicate]
I read quite a few resources on the internet and now I am quite confused about the existence of pointers in Java.
Some claim that there are pointers and some claim that there is no such concept. So ...
1
vote
3answers
334 views
Garbage collectors and pointers/ reference
I am a little confused on both pointers and reference. From my understanding pointers are addresses in memory. If I pass a variable using pointers and reference to a function, any manipulations of ...
0
votes
1answer
582 views
Is there a proper way to allow access to private list by reference?
I'm trying to provide a by-reference getter to a list of objects in a class. My setup looks something roughly like this:
class c_Container
{
public:
c_Item* Get(int uid);
private:
c_Item ...
6
votes
1answer
250 views
Hardware that accelerates pointer dereferencing?
Most modern languages make a heavy use of pointers / references: a typical OOP language uses VMT lookups, a typical functional language builds key data structures out of pointers, etc. Even typical C ...
7
votes
1answer
4k views
Is this a valid example of a dangling pointer?
The book "Data Structures in C" (Horowitz and Sahni) suggests that in the following code the pointer pf is behaving as a dangling reference:
float f,*pf;
pf=(float*) malloc(sizeof(float));
*pf=2.6;
...