problem about pointer-arithmetic-Collection of common programming errors


  • Steve314
    c++ pointers pointer-arithmetic
    Careless use of templates can cause bloat. One way to avoid that bloat is to have a thin typesafe template that wraps non-typesafe non-template code. To do this, the wrapper needs to provide some way for the non-template code to access things it knows nothing about.For example, in a data structure, the wrapper defines the node structs. The unsafe code needs to read and write to the nodes, but must do so indirectly, through some kind of interface which is specified by the wrapper.One way to implement this interface is to fill in a struct (defined by the unsafe code) with details such as function-pointers and constants determined by the wrapper. And one relevant kind of constant is the offset (within some structure) of a particular field. The unsafe code can use that offset (and some pointer arithmetic) to access that field directly.This is getting problematic, though – as optimisers get more aggressive, this can lead to pointer alias issues. This is particularly the case if nodes can escape the library. For example, nodes may be extracted from a binary tree and relinked to form a linked list. Another example, annoyingly, happens when unit testing.I currently have a container library written along these lines, and it causes none of these problems at present – but it will shortly. The reason it avoids these problems is because all the unit testing is applied to containers (not the underlying code), and because the nodes never escape the containers. That is, nodes are always accessed the same pointer-arithmetic way, so the pointer alias optimization issue never arises.Unfortunately, I’m shortly going to need to allow nodes to be extracted from the containers, and I’m probably going to need unit tests on the underlying unsafe code as well.Rather than deal with this specific library, I have a much simpler extract from an old binary tree library here that suffers the same problems. In VC++9 it just works. Using MinGW GCC 4.4.0, a debug build works, but a release build fails. The problem is a mixture of inlining and failure of the optimizer to spot pointer aliasses.Just to be clear – I don’t want to here “WTF – GOTO!!!” or whatever. The issue is resolving the optimization/pointer problem. Though if you can find a way to write Tree_To_List that is properly structured and doesn’t use hidden/disguised gotos to achieve it, I’m interested.Also, a layer of template-based abstraction is missing (the template c_Bin_Tree_Tool doesn’t do the whole job – c_Tool finishes the wrapping, but in a per-use way rather than i

  • starblue
    c pointers pointer-arithmetic
    Does anyone have any good articles or exp

  • user1131467
    c++ c++11 language-lawyer constexpr pointer-arithmetic
    The following C++11 program:int x = 42;void f() {int y = 43;static_assert(&x < &y, “foo”); }int main() {f(); }Doesn’t compile with gcc 4.7 as it complains:error: ‘&y’ is not a constant expressionThis would agree with my intuition. The address of y potentially changes with each invocation of f, so of course it cannot be calculated during translation.However none of the bullet points in 5.19 [expr.const] seem to preclude it from being a constant expression.The only two contenders I see are:an lvalue-to-rvalue

  • 21 revs, 2 users 100%Stephen Lin
    c++ pointers c++11 language-lawyer pointer-arithmetic
    Does the following code (which performs pointer arithmetic across subobject boundaries) have well-defined behavior for types T for which it compiles (which, in C++11, does not not necessarily have to be POD) or any subset thereof?#include #include template struct Base {// ensure alignmentunion{T initial;char begin;}; };template struct Derived : public Base {T rest[N – 1];char end; };int main() {Derived d;assert(&d.rest[9] – &d.initial == 10);assert(&d.end – &d.begin == sizeof(float) * 10);return 0; }LLVM uses a variation of the above technique in the implementation of an internal vector type which is optimized to initially use the stack for small arrays but switches to a heap-allocated buffer once over initial capacity. (The reason for doing it this way is not clear from this example but is apparently to reduce template code bloat; this is clearer if you look through the code.) NOTE: Before anyone complains, this is not exactly what they are doing and it might be that their approach is more standards-compliant than what I have given here, but I wanted to ask about the general case.Obviously, it works in practice, but I’m curious if anything in the standard guarantees for that to be the case. I’m inclined to sa

  • Dan Nestor

  • Christophe AGUETTAZ
    c++ pointers pointer-arithmetic
    I’m having trouble understanding the behavior of the MS VC compiler on this one. This line compiles fine, but the result I get is not what I’d expect at all:this->Test((char *)&CS2 – (char *)&CS1 == sizeof(void *));The CS1 and CS2 arguments are declared as follows:myFunction(tCS1* CS1, tCS2* CS2) {…tCS1 and tCS2 are structur

  • Qxtrml
    c pointers void-pointers pointer-arithmetic
    I want to take some fields from packet struct using pointer arithmetic.But what is wrong with the code below ? In first condition i think if i go 4 byte(2 short field) from beginning of

  • Muggen
    c gcc pointer-arithmetic
    I have a C code in long file that is compiled using cc. But when I tried to compile on gcc it gives error. I took that particular code in small program and try to compile on cc but it failed over there.Here is source:#include int main (int argc, char **argv) {cha

  • cpx
    c++ pointers pointer-arithmetic
    If I say,int a[] = {1, 2, 3, 4, 5}; in

  • Zaibis
    c c99 undefined-behavior pointer-arithmetic strict-aliasing
    I’m working now for some weeks with c99 focusing undefined behaviour. I wanted to test some strange code while trying to respect the rules. The result was this code:(plz forgive me the variable names, i had eaten a clown)int main(int arg, char** argv) {unsigned int uiDiffOfVars;int LegalPointerCast1, LegalPointerCast2, signedIntToRespectTheRules;char StartVar;//Only use to have an adress from where we can move onchar *TheAccesingPointer;int iTargetOfPointeracces;iTargetOfPointeracces= 0x55555555;TheAccesingPointer = (char *) &StartVar;LegalPointerCast2 = (int) &StartVar;LegalPointerCast1 = (int) &iTargetOfPointeracces;if ((0x80000000 & LegalPointerCast2) != (0x80000000 & LegalPoi

  • dasblinkenlight
    c++ for-loop pointer-arithmetic
    I’m trying to understand some code that uses pointer arithmetic in a way I’m not used to. At one point in the code I encounter this:complex **P, *p_row, result=complex<

  • Jay
    c pointer-arithmetic
    Incase of pointer arithmetic, are the integers automatically converted to their signed variants? If yes, why

  • Thokchom

  • Mat
    c pointer-arithmetic
    I am trying to understand paragraph 8 and 9 of C99 sect 6.5.6 (Additive operators)Does para 8 mean:int a [4];int *p = a; p –; /* undefined behaviour */p = a + 4; /* okay */ p

  • unwind
    c pointer-arithmetic
    Given this code:int *

  • Bernhard Kausler

  • Joe

  • rampion
    c arrays pointers compiler pointer-arithmetic
    So, as I learned from Michael Burr’s comments to this answer, the C standard doesn’t support integer subtraction from pointers past the first element in an array (which I suppose includes any allocated memory).From section 6.5.6 of the combined C99 + TC1 + TC2 (pdf):If both the po

  • Lefteris

  • Matt Joiner
    c visual-studio gcc void-pointers pointer-arithmetic
    In Visual Studio C++ version 9 (and probably other versions too), the following code:int a = sizeof(void); void const *b = static_cast(“hello world”); b += 6;Generates these errors:error C2070: ‘void’: illegal sizeof operand error C2036: ‘const void *’ : unknown sizeThis code works under GCC, which treats sizeof(void) as 1.Is there some way around this limitation, as casting explicitly to char * for purposes of pointer arithmeti

Originally posted 2013-11-10 00:17:19.