Home:ALL Converter>What use are const pointers (as opposed to pointers to const objects)?

What use are const pointers (as opposed to pointers to const objects)?

Ask Time:2008-10-21T05:07:21         Author:Head Geek

Json Formatter

I've often used pointers to const objects, like so...

const int *p;

That simply means that you can't change the integer that p is pointing at through p. But I've also seen reference to const pointers, declared like this...

int* const p;

As I understand it, that means that the pointer variable itself is constant -- you can change the integer it points at all day long, but you can't make it point at something else.

What possible use would that have?

Author:Head Geek,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/219914/what-use-are-const-pointers-as-opposed-to-pointers-to-const-objects
Adam Davis :

When you're designing C programs for embedded systems, or special purpose programs that need to refer to the same memory (multi-processor applications sharing memory) then you need constant pointers.\nFor instance, I have a 32 bit MIPs processor that has a little LCD attached to it. I have to write my LCD data to a specific port in memory, which then gets sent to the LCD controller.\nI could #define that number, but then I also have to cast it as a pointer, and the C compiler doesn't have as many options when I do that.\nFurther, I might need it to be volatile, which can also be cast, but it's easier and clearer to use the syntax provided - a const pointer to a volatile memory location.\nFor PC programs, an example would be: If you design DOS VGA games (there are tutorials online which are fun to go through to learn basic low level graphics) then you need to write to the VGA memory, which might be referenced as an offset from a const pointer.",
2008-10-20T21:16:05
Andrew Johnson :

It allows you to protect the pointer from being changed. This means you can protect assumptions you make based on the pointer never changing or from unintentional modification, for example:\n\nint* const p = &i;\n\n...\n\np++; /* Compiler error, oops you meant */\n(*p)++; /* Increment the number */\n",
2008-10-20T21:19:32
Benedikt Waldvogel :

another example:\nif you know where it was initialized, you can avoid future NULL checks.\nThe compiler guarantees you that the pointer never changed (to NULL)…",
2008-10-20T21:16:07
Adam Rosenfield :

In any non-const C++ member function, the this pointer is of type C * const, where C is the class type -- you can change what it points to (i.e. its members), but you can't change it to point to a different instance of a C. For const member functions, this is of type const C * const. There are also (rarely encountered) volatile and const volatile member functions, for which this also has the volatile qualifier.",
2008-10-20T21:16:57
Michael Carman :

One use is in low-level (device driver or embedded) code where you need to reference a specific address that's mapped to an input/output device like a hardware pin. Some languages allow you to link variables at specific addresses (e.g. Ada has use at). In C the most idiomatic way to do this is to declare a constant pointer. Note that such usages should also have the volatile qualifier.\n\nOther times it's just defensive coding. If you have a pointer that shouldn't change it's wise to declare it such that it cannot change. This will allow the compiler (and lint tools) to detect erroneous attempts to modify it.",
2008-10-20T21:43:35
Tom Ritter :

I've always used them when I wanted to avoid unintended modification to the pointer (such as pointer arithmetic, or inside a function). You can also use them for Singleton patterns.\n\n'this' is a hardcoded constant pointer.",
2008-10-20T21:17:21
James Curran :

Same as a \"const int\" ... if the compiler knows it's not going to change, it can be optimization assumptions based on that.\n\nstruct MyClass\n{\n char* const ptr;\n MyClass(char* str) :ptr(str) {}\n\n void SomeFunc(MyOtherClass moc)\n {\n for(int i=0; i < 100; ++i)\n { \n printf(\"%c\", ptr[i]);\n moc.SomeOtherFunc(this);\n }\n }\n}\n\n\nNow, the compiler could do quite a bit to optimize that loop --- provided it knows that SomeOtherFunc() does not change the value of ptr. With the const, the compiler knows that, and can make the assumptions. Without it, the compiler has to assume that SomeOtherFunc will change ptr.",
2008-10-20T21:10:25
epotter :

I have seen some OLE code where you there was an object passed in from outside the code and to work with it, you had to access the specific memory that it passed in. So we used const pointers to make sure that functions always manipulated the values than came in through the OLE interface.",
2008-10-20T21:16:47
Michael Burr :

Several good reasons have been given as answers to this questions (memory-mapped devices and just plain old defensive coding), but I'd be willing to bet that most instances where you see this it's actually an error and that the intent was to have to item be a pointer-to-const.\n\nI certainly have no data to back up this hunch, but I'd still make the bet.",
2008-10-20T21:38:16
Shadow2531 :

Think of type* and const type* as types themselves. Then, you can see why you might want to have a const of those types.",
2008-10-20T22:19:39
yy