Questions tagged [pointers]

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.

Filter by
Sorted by
Tagged with
1 vote
7 answers
342 views

Is it possible to introduce Pointers to a weakly typed programming language?

Most weakly typed languages do not have a concept of a pointer. Instead, most of them use references. However, is it possible to introduce pointers to such a language. From a naive point of view, to ...
Lily White's user avatar
1 vote
4 answers
359 views

Passing arrays as global variables instead of pointers in C

I'm required to write the Low-Level Requirements of a Software Component which shall perform signal processing over arrays of 200k elements of integers/floats which lives in the main memory of the ...
Sam's user avatar
  • 31
0 votes
1 answer
74 views

Avoiding repeating code when assigning pointer addresses based on branch

I have the following code in C that reads data off a char array and makes decisions on what variables should some pointers point to. char preset1[20]; char preset1Name[20]; int preset1Temp; int ...
HFOrangefish's user avatar
0 votes
1 answer
95 views

Is it safe to make training data and labels as global variables in C?

I'm trying to make this function called walk in C. The idea is that the function takes as parameters a point in space (represented by an array), a function (pointer), and a step size / learning rate ...
Mehdi Charife's user avatar
2 votes
2 answers
804 views

Reference variable vs Alias

When I started programming, I learned what Pointers are in C++. In Java, references are variables which hold the address of an object. So conceptually it's a pointer, without the ability to directly ...
tweekz's user avatar
  • 89
7 votes
1 answer
182 views

What is the term used to describe the number of times a pointer can be dereferenced?

For example, which term completes the sentence "p has 5 ____" in order to describe a situation like int *****p?
vim_overlord's user avatar
0 votes
1 answer
312 views

Implement Dependency Inversion in C with UML diagram

As per Robert C. Martin in Clean Architecture, he gives a simple UML diagram to illustrate Dependency Inversion. To put it simply, HL1 initially referred to ML1 without interface to invoke F() ...
Andy Lin's user avatar
  • 185
-1 votes
5 answers
2k views

In C++, does dereferencing a nullptr itself cause undefined behaviour, or is it the acting upon the dereferenced pointer which is undefined?

I happen to have a reason why I might want to dereference a nullptr. Of course when I do, my program crashes, and from what I gather, this is due to the compiler playing it safe and stopping my ...
Anon's user avatar
  • 3,593
1 vote
4 answers
8k views

C++: Is a pointer to a vector going to cause memory issues?

I started to write a function which had a pointer to a vector as a parameter so that it could modify that vector to output results (as the actual return value was an error code), when I started to ...
I_like_robots's user avatar
2 votes
2 answers
267 views

Can you define node pointers in a base binary tree class?

I've created two separate binary tree classes, with some shared functions/variables and some that are not shared. So I have tried to abstract away the similarities in a base BinaryTree class. class ...
yomag1234's user avatar
0 votes
1 answer
118 views

What design pattern (if so) did I apply? How can I further improve it?

Suppose I have a program.c that needs element_123 to do some operations, and element_123 can be accessed by including agent.h /*program.c*/ #include "agent.h" uint32_t element_123 = 0; ...
Andy Lin's user avatar
  • 185
2 votes
1 answer
1k views

How to store a vector of smart pointers, except some of them are owned by another object?

I'm making a basic platformer game. I have a Game class as well as Level class. The game object holds a pointer to the current Level object. A level currently has a std::vector of GameObject raw ...
Luke B's user avatar
  • 147
8 votes
3 answers
2k views

What are the pros and cons of using a reference/pointer vs an ID

I'm writing in C++, but this problem applies to any language without GC and even to languages with a GC as well. I have a structure in memory in which I create/add objects. The structure takes ...
Helloer's user avatar
  • 253
63 votes
10 answers
19k views

I never use pointers in my C++ code. Am I coding C++ wrong? [closed]

This question may sound strange to you, but I am learning C++ all by myself. I have nobody whom I could ask for mentoring and I would be very glad for some advice. I have started recently to program ...
curiouscupcake's user avatar
-2 votes
2 answers
172 views

What is the correct use of -> operator when working with pointers?

I'm pretty new with C so I have encountered many doubts with pointers. I've already search a lot about this but there are some things that still are not clear for me, and I also think this will help ...
Nicolas Bazan's user avatar
2 votes
1 answer
824 views

How to efficiently access public static variables/methods of the correct derived class?

I have an arbitrary number of derived classes all inheriting from the same base class. These derived classes all have the same static variables and static methods, although the implementations may ...
Inertial Ignorance's user avatar
1 vote
2 answers
1k views

Should I always allocate QObject and derived classes to the heap?

I was in #Qt irc channel, and I showed a small snippet of my code in a style that I heavily rely upon. It looks like this: /* Get Reply from Server */ QPointer<QNetworkReply> reply; { ...
Anon's user avatar
  • 3,593
6 votes
3 answers
1k views

From a language design perspective, is the reference type in C++ mis-designed

Besides the fact that all primitive types of C++ are copy assignable except the reference type, it also doesn't play well with containers or any other parts of the language where copy-assignable ...
John Z. Li's user avatar
5 votes
1 answer
7k views

Why do we need to specify the type of data a pointer will hold, if all pointers are the same [duplicate]

Why do we need to specify the type of the data whose address, a pointer will hold, if all pointers are the same. Since all pointers store addresses. Also, the amount of space a pointer will require in ...
amd's user avatar
  • 59
-1 votes
1 answer
179 views

Pointers and Values in C

I do a lot of work in various languages, most of which are scripting languages such as JavaScript, Shell Scripting, PHP and so on. But I do also work a lot with Java, which is closer to a more "real" ...
Daniel B's user avatar
  • 109
0 votes
2 answers
2k views

What is the safest practice in handling QWidget pointer lifespan in a QObject oriented environment?

Consider the following constructor: NetworkTools::NetworkTools(QObject *parent) : QObject(parent) { view = new QWebEngineView(); view->setParent(parent); // Produces an error, shown below. ...
Anon's user avatar
  • 3,593
4 votes
1 answer
853 views

C++ Are members of a class pointer automatically on heap?

Let's say we have a struct Vector2i { int x = 0, int y = 0 }; And create a Pointer to it via Vector2i* pointer = new Vector2i; Where would int x and int y be stored? Heap or stack? Are all members ...
Farrrbi's user avatar
  • 51
1 vote
2 answers
292 views

Setting a pointer to NULL on failure?

I've been modifying some code written by a previous employee and came across a function with the following signature: BOOL WINAPI PrependPadding( _In_ SIZE_T cbPadding, _In_ SIZE_T cbRow, ...
Govind Parmar's user avatar
19 votes
3 answers
2k views

When & why did pointers start to become viewed as risky?

It seems that there has been a gradual shift in thinking about the use of pointers in programming languages such that it became generally accepted that pointers were considered risky (if not outright "...
StayOnTarget's user avatar
-1 votes
1 answer
2k views

Why is it possible to access an array out of bounds with negative indexes much further than with positive indexes?

I have written two small programs in which I declare a very small array. Then I try to access values out of bounds. The interesting thing I noticed that when I try to decrement the index I can ...
yoyo_fun's user avatar
  • 2,297
26 votes
3 answers
8k views

Why does a long int take 12 bytes on some machines?

I noticed something strange after compiling this code on my machine: #include <stdio.h> int main() { printf("Hello, World!\n"); int a,b,c,d; int e,f,g; long int h; ...
yoyo_fun's user avatar
  • 2,297
1 vote
1 answer
130 views

What kind of pointer bug is this specifically?

UncommentatedPannen's (pannenkoek2012) newest video describes a glitch in the Nintendo 64 game Super Mario 64, which allows an object to push Mario out of bounds, which isn't normally possible, by ...
0xFFF1's user avatar
  • 165
1 vote
2 answers
1k views

How to avoid lots of ugly pointer casting when using a container in C?

Let's say I have a container in C, for example something similar to C++' std::deque: struct deque { // blah }; struct deque* deque_create(size_t element_size, size_t init_deq_size); void* ...
gaazkam's user avatar
  • 3,883
4 votes
1 answer
2k views

Should opaque pointers be pointers or types?

A common way to implement "PIMPL" in C is to do this: typedef struct _Opaque Opaque; Opaque* createOpaque(); void doSomething(Opaque *opaque); Or: typedef struct _Opaque* Opaque; Opaque ...
user112513312's user avatar
6 votes
4 answers
856 views

Refactor big C++ application to prevent from bad pointer

In application which has about 1.5 mln lines of code there is using many pointers as a class members and in other places in code. Classes are usually very huge. It is possible to change to make it ...
marcin's user avatar
  • 79
-1 votes
1 answer
526 views

GO - How to define methods of named type?

In GO, rule is, methods can be defined only on named type and pointer to named type. In C, below code, operations are defined on type(say List), typedef struct List List; //list.h typedef struct { ...
overexchange's user avatar
  • 2,255
5 votes
2 answers
4k views

Type safety - GO vs C pointers

C is a static-typed language that is not type-safe, because pointers(void *y) let you do pretty much anything you like, even things that will crash your program. GO is also a static typed language ...
overexchange's user avatar
  • 2,255
1 vote
1 answer
56 views

How to avoid fetching additional informations when instantiating objects

I'm creating an HTML5 game using javascript and have got some problems during the first instantiation of the objects of the scene. Scenario Self-written 2d game engine that supports multiple ...
user avatar
1 vote
3 answers
2k views

Is it possible to deliberately break RAM using pointers?

Is it possible to intentionally stress and break part of a computer on a mechanical level using the specificity of C/C++ in targeting addresses? All of this talk of addresses and pointers is not a ...
Pipsqweek's user avatar
  • 129
2 votes
5 answers
2k views

What value does the byte pointed by null pointer have?

I know that long time ago computer scientists decided to treat all pointers to memory cell of address 0 as NULL. However, the memory cell at that address does exists after all, right? In that case, ...
alagris's user avatar
  • 139
2 votes
2 answers
5k views

Struct "prototypes" in (plain)C?

As the title says, can it be done? struct Room{ char *type; //Lecture hall, laboratory, etc. char *name; int *capacity; //How may people it can hold struct Building *building; }; ...
Rares Dima's user avatar
8 votes
6 answers
4k views

In C++; How big should an object [that will be transferred between functions] be before I consider delegating it to the heap?

In my day to day programming, I tend to use very few pointers, not only because I want to keep my code simple and error free, but because I assume that the programming that I do does not have any ...
Anon's user avatar
  • 3,593
3 votes
1 answer
127 views

Why are pointers of structs not printed like pointers of variables? [closed]

Consider the following code: package main import "fmt" type Vertex struct { X, Y int } var ( i = 10 p = &i v = Vertex{1,2} q = &v ) func main() { fmt.Println(p) // ...
kdnooij's user avatar
  • 41
9 votes
6 answers
2k 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 ...
Torm's user avatar
  • 151
0 votes
2 answers
217 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) { ...
user1367123's user avatar
1 vote
2 answers
1k views

Passing by refernce

I am trying to understand the ideas of pointers and references in C++. I am stuck with the following, what would be the specific behaviour in this case? I have a class like this: class MyClass{ ...
Vlad's user avatar
  • 137
10 votes
4 answers
15k 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 ...
john01dav's user avatar
  • 879
1 vote
1 answer
196 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 ...
DarthRubik's user avatar
0 votes
1 answer
980 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 ...
Andre Marques's user avatar
4 votes
4 answers
1k 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 ...
calben's user avatar
  • 611
2 votes
3 answers
1k 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++ ...
Frank's user avatar
  • 215
3 votes
2 answers
3k 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 ...
downrep_nation's user avatar
1 vote
2 answers
255 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 *, ...
user1369975's user avatar
  • 1,269
1 vote
1 answer
324 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 ...
sydan's user avatar
  • 373
27 votes
11 answers
16k 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 ...
Sudip Bhandari's user avatar