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.
-3
votes
0answers
17 views
Declaring Multiple Variables in a For Loop Then assigning those Variable Data [migrated]
I am not sure how to handle this, whether it is a pointer or not.
for (int i = 0; i < 5; i++)
{
QString companyTaskText = "companyTask" + QString::number(i + 1);
QTextEdit *...
7
votes
6answers
453 views
In C, is * an operator, or part of a type in a declaration?
In C, * is called the indirection operator or the dereference operator. I understand how it works when it is used in a statement. It makes sense to write *p or * p, considering that it is a unary ...
0
votes
2answers
83 views
What's the difference between writing to the memory address of a declared int and writing directly to an int*?
I'm writing a program in which I fscanf a file and write to the same memory address repeatedly. I tried the following:
FILE *f = fopen(fname, "r");
int* num;
while(fscanf(f, "%d,", num) != EOF)
{
...
0
votes
2answers
168 views
passing by refernce
I am study c++ and c languages, i was writing in javascript before. Im trying to understand the ideas of pointer and refernces. So im stuck with following, what would be the specific behaviour in this ...
1
vote
0answers
44 views
Shared pointers across threads in UML for class and sequence diagrams
How does one represent shared pointers across threads in UML for class diagrams and sequence diagrams?
I especially want to document the ownership of the memory even though it's accessibility (a ...
2
votes
2answers
239 views
In C++, why shouldn't all function parameters be references?
I am currently learning C++ from the C++ Primer 5th edition. The book's chapter on functions states that only large objects (large being relative as standard library strings count, but "primitive ...
1
vote
1answer
117 views
Static memory idiom
I am on a micro controller (which means I can only have static memory allocation) and I am trying to work with inheritance.....
Suppose I have a abstract class Image and an abstract class Font. An ...
0
votes
1answer
122 views
How to call an unknown member function through an instance of a related class?
I have two classes. The first, called Game_Events, controls the objects and manages the general tasks. The second, called Button, is for one of those object instances.
The example below is wrong but ...
3
votes
4answers
239 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 ...
1
vote
3answers
403 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++ ...
3
votes
2answers
290 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
132 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
72 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
3answers
369 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 ...
23
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
237 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
somePikachu->...
0
votes
0answers
63 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
393 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
577 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
308 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
386 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
595 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
193 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 ...
28
votes
10answers
9k 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
367 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
215 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
169 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 ...
13
votes
1answer
8k 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++ programming (specifically with the Qt Framework) I only use the old raw pointer:
SomeKindOfObject *someKindOfObject = new ...
4
votes
1answer
873 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, ...
2
votes
2answers
2k 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
271 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 ...
1
vote
2answers
618 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
674 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 ...
3
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 ...
22
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 ...
0
votes
1answer
131 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
8k 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
6k 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 ...
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
636 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 ...
1
vote
3answers
8k 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
291 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
292 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
288 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
384 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
761 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 ...