All Questions
22,934
questions
3911
votes
44
answers
1.2m
views
What are the differences between a pointer variable and a reference variable?
What is the difference between a pointer variable and a reference variable?
3125
votes
12
answers
775k
views
When should static_cast, dynamic_cast, const_cast, and reinterpret_cast be used?
What are the proper uses of:
static_cast
dynamic_cast
const_cast
reinterpret_cast
(type)value (C-style cast)
type(value) (function-style cast)
How does one decide which to use in which specific ...
2164
votes
14
answers
732k
views
What is a smart pointer and when should I use one?
What is a smart pointer and when should I use one?
2046
votes
8
answers
911k
views
Regular cast vs. static_cast vs. dynamic_cast [duplicate]
I've been writing C and C++ code for almost twenty years, but there's one aspect of these languages that I've never really understood. I've obviously used regular casts i.e.
MyClass *m = (MyClass *)...
1894
votes
24
answers
425k
views
Why should I use a pointer rather than the object itself?
I'm coming from a Java background and have started working with objects in C++. But one thing that occurred to me is that people often use pointers to objects rather than the objects themselves, for ...
1809
votes
23
answers
733k
views
What is the difference between const int*, const int * const, and int const *?
I always mess up how to use const int*, const int * const, and int const * correctly. Is there a set of rules defining what you can and cannot do?
I want to know all the do's and all don'ts in terms ...
693
votes
6
answers
808k
views
What does 'dereferencing' a pointer mean in C/C++?
Please include an example with the explanation.
668
votes
14
answers
406k
views
What is the nullptr keyword, and why is it better than NULL?
We now have C++11 with many new features. An interesting and confusing one (at least for me) is the new nullptr.
Well, no need anymore for the nasty macro NULL.
int* x = nullptr;
myclass* obj = ...
570
votes
6
answers
606k
views
Typedef function pointer?
I'm learning how to dynamically load DLL's but what I don't understand is this line
typedef void (*FunctionFunc)();
I have a few questions. If someone is able to answer them I would be grateful.
...
515
votes
5
answers
142k
views
How do I use arrays in C++?
C++ inherited arrays from C where they are used virtually everywhere. C++ provides abstractions that are easier to use and less error-prone (std::vector<T> since C++98 and std::array<T, n> ...
496
votes
11
answers
108k
views
What is array-to-pointer conversion aka. decay?
What is array-to-pointer conversion aka. decay? Is there any relation to array pointers?
476
votes
15
answers
55k
views
How many levels of pointers can we have?
How many pointers (*) are allowed in a single variable?
Let's consider the following example.
int a = 10;
int *p = &a;
Similarly we can have
int **q = &p;
int ***r = &q;
and so on.
...
470
votes
28
answers
92k
views
What are the barriers to understanding pointers and what can be done to overcome them? [closed]
Why are pointers such a leading factor of confusion for many new, and even old, college-level students in C or C++? Are there any tools or thought processes that helped you understand how pointers ...
462
votes
17
answers
250k
views
When should I use pointers instead of references in API-design?
I understand the syntax and general semantics of pointers versus references, but how should I decide when it is more-or-less appropriate to use references or pointers in an API?
Naturally some ...
461
votes
17
answers
382k
views
Why use pointers? [closed]
I know this is a really basic question, but I've just started with some basic C++ programming after coding a few projects with high-level languages.
Basically I have three questions:
Why use ...
449
votes
19
answers
1.1m
views
Passing a 2D array to a C++ function
I have a function which I want to take, as a parameter, a 2D array of variable size.
So far I have this:
void myFunction(double** myArray){
myArray[x][y] = 5;
etc...
}
And I have ...
397
votes
17
answers
547k
views
How to find the size of an array (from a pointer pointing to the first element array)?
First off, here is some code:
int main()
{
int days[] = {1,2,3,4,5};
int *ptr = days;
printf("%u\n", sizeof(days));
printf("%u\n", sizeof(ptr));
return 0;
}
Is there a way to ...
355
votes
12
answers
182k
views
When should I use the new keyword in C++? [duplicate]
I've been using C++ for a short while, and I've been wondering about the new keyword. Simply, should I be using it, or not?
With the new keyword...
MyClass* myClass = new MyClass();
myClass-&...
352
votes
6
answers
332k
views
What is the uintptr_t data type?
What is uintptr_t and what can it be used for?
352
votes
9
answers
215k
views
Is it safe to delete a NULL pointer?
Is it safe to delete a NULL pointer?
And is it a good coding style?
351
votes
4
answers
302k
views
NULL vs nullptr (Why was it replaced?) [duplicate]
I know that in C++ 0x or NULL was replaced by nullptr in pointer-based applications. I'm just curious of the exact reason why they made this replacement?
In what scenario is using nullptr over NULL ...
345
votes
4
answers
319k
views
Differences between unique_ptr and shared_ptr [duplicate]
Possible Duplicates:
pimpl: shared_ptr or unique_ptr
smart pointers (boost) explained
Could someone explain differences between shared_ptr and unique_ptr?
321
votes
19
answers
217k
views
What is a pointer to class data member "::*" and what is its use?
I came across this strange code snippet which compiles fine:
class Car
{
public:
int speed;
};
int main()
{
int Car::*pSpeed = &Car::speed;
return 0;
}
Why does C++ have this ...
296
votes
21
answers
1.1m
views
Return array in a function
I have an array int arr[5] that is passed to a function fillarr(int arr[]):
int fillarr(int arr[])
{
for(...);
return arr;
}
How can I return that array?
How will I use it, say I returned a ...
288
votes
7
answers
161k
views
Are there benefits of passing by pointer over passing by reference in C++?
What are the benefits of passing by pointer over passing by reference in C++?
Lately, I have seen a number of examples that chose passing function arguments by pointers instead of passing by ...
278
votes
12
answers
129k
views
Should I use a pointer or a reference to remotely assign a variable? [duplicate]
What would be better practice when giving a function the original variable to work with:
unsigned long x = 4;
void func1(unsigned long& val) {
val = 5;
}
func1(x);
or:
void ...
273
votes
8
answers
148k
views
How should I pass objects to functions?
I am new to C++ programming, but I have experience in Java. I need guidance on how to pass objects to functions in C++.
Do I need to pass pointers, references, or non-pointer and non-reference values?...
270
votes
9
answers
106k
views
Should I use static_cast or reinterpret_cast when casting a void* to whatever
Both static_cast and reinterpret_cast seem to work fine for casting void* to another pointer type. Is there a good reason to favor one over the other?
256
votes
17
answers
155k
views
Is the sizeof(some pointer) always equal to four?
For example:
sizeof(char*) returns 4. As does int*, long long*, everything that I've tried. Are there any exceptions to this?
245
votes
4
answers
41k
views
Which kind of pointer do I use when?
Ok, so the last time I wrote C++ for a living, std::auto_ptr was all the std lib had available, and boost::shared_ptr was all the rage. I never really looked into the other smart pointer types boost ...
207
votes
14
answers
137k
views
Can I use if (pointer) instead of if (pointer != NULL)?
Is it safe to check a pointer to not being NULL by writing simply if(pointer) or do I have to use if(pointer != NULL)?
207
votes
3
answers
37k
views
Why is 'this' a pointer and not a reference?
I was reading the answers to this question C++ pros and cons and got this doubt while reading the comments.
programmers frequently find it confusing that "this" is a pointer but not a ...
204
votes
2
answers
194k
views
How to cast/convert pointer to reference in C++
How can I pass a pointer (Object *ob) to a function which prototype is void foo(Object &) ?
185
votes
18
answers
118k
views
Is it good practice to NULL a pointer after deleting it?
I'll start out by saying, use smart pointers and you'll never have to worry about this.
What are the problems with the following code?
Foo * p = new Foo;
// (use p)
delete p;
p = NULL;
This was ...
174
votes
6
answers
99k
views
Declaring pointers; asterisk on the left or right of the space between the type and name? [duplicate]
Possible Duplicates:
What makes more sense - char* string or char *string?
Pointer declarations in C++: placement of the asterisk
I've seen mixed versions of this in a lot of code. (This ...
169
votes
10
answers
66k
views
Should I store entire objects, or pointers to objects in containers?
Designing a new system from scratch. I'll be using the STL to store lists and maps of certain long-live objects.
Question: Should I ensure my objects have copy constructors and store copies of ...
162
votes
8
answers
28k
views
Why do C++ libraries and frameworks never use smart pointers?
I read in a few articles that raw pointers should almost never be used. Instead they should always be wrapped inside smart pointers, whether it's scoped or shared pointers.
However, I noticed that ...
159
votes
17
answers
37k
views
What's the point of const pointers?
I'm not talking about pointers to const values, but const pointers themselves.
I'm learning C and C++ beyond the very basic stuff and just until today I realized that pointers are passed by value to ...
158
votes
12
answers
111k
views
Pointer expressions: *ptr++, *++ptr and ++*ptr
Recently I have come across this problem which I am unable to understand by myself.
What do these three Expressions REALLY mean?
*ptr++
*++ptr
++*ptr
I have tried Ritchie. But unfortunately was ...
153
votes
4
answers
71k
views
What is the difference between std::reference_wrapper and a simple pointer?
Why is there a need to have std::reference_wrapper? Where should it be used? How is it different from a simple pointer? How its performance compares to a simple pointer?
152
votes
10
answers
175k
views
Passing references to pointers in C++
As far as I can tell, there's no reason I shouldn't be allowed to pass a reference to a pointer in C++. However, my attempts to do so are failing, and I have no idea why.
This is what I'm doing:
...
149
votes
12
answers
15k
views
Why is x[0] != x[0][0] != x[0][0][0]?
I'm studying a little of C++ and I'm fighting with pointers. I understand that I can have 3 level of pointers by declaring:
int *(*x)[5];
so that *x is a pointer to an array of 5 elements that are ...
148
votes
16
answers
135k
views
How does delete[] know it's an array?
Alright, I think we all agree that what happens with the following code is undefined, depending on what is passed,
void deleteForMe(int* pointer)
{
delete[] pointer;
}
The pointer could be all ...
142
votes
4
answers
222k
views
How to declare std::unique_ptr and what is the use of it?
I try to understand how std::unique_ptr works and for that I found this document. The author starts from the following example:
#include <utility> //declarations of unique_ptr
using std::...
140
votes
15
answers
50k
views
Placement of the asterisk in pointer declarations
I've recently decided that I just have to finally learn C/C++, and there is one thing I do not really understand about pointers or more precisely, their definition.
How about these examples:
int* ...
138
votes
7
answers
122k
views
What can I use instead of the arrow operator, `->`?
What is the arrow operator (->) a synonym for?
138
votes
14
answers
15k
views
Why are function pointers and data pointers incompatible in C/C++?
I have read that converting a function pointer to a data pointer and vice versa works on most platforms but is not guaranteed to work. Why is this the case? Shouldn't both be simply addresses into ...
137
votes
9
answers
31k
views
Why does the use of 'new' cause memory leaks?
I learned C# first, and now I'm starting with C++. As I understand, operator new in C++ is not similar to the one in C#.
Can you explain the reason of the memory leak in this sample code?
class A { ....
135
votes
2
answers
54k
views
Can I call memcpy() and memmove() with "number of bytes" set to zero?
Do I need to treat cases when I actully have nothing to move/copy with memmove()/memcpy() as edge cases
int numberOfBytes = ...
if( numberOfBytes != 0 ) {
memmove( dest, source, numberOfBytes );
}...
135
votes
21
answers
46k
views
Why is address zero used for the null pointer?
In C (or C++ for that matter), pointers are special if they have the value zero: I am adviced to set pointers to zero after freeing their memory, because it means freeing the pointer again isn't ...