Tag Info

New answers tagged

1

String literals like "name" are stored as arrays of char (const char in C++) such that they are allocated when the program starts and held until the program terminates. The type of the expression "name" is "5-element array of char" (5th element for the 0 terminator). Except when it is the operand of the sizeof or unary * operators, or is a string literal ...


4

This is just the way string literals work in C. String literals like "name" are arrays of characters, it is equivalent to the five element array {'n', 'a', 'm', 'e', '\0'}. For the code char *c; c="name"; the environment reserves memory for the above array already at initialization time, when the program is loaded from disk into memory. At run time, the ...


3

First, it's undefined behaviour. Some optimising compilers nowadays get very aggressive about undefined behaviour. For example, since a-- in this case is undefined behaviour, the compiler could decide to save an instruction and a processor cycle and not decrement a. Which is officially correct and legal. Ignoring that, you might subtract 1, or 2, or 1980. ...


1

...simply decrementing a pointer outside of the allocated range seems highly sketchy to me. Is this "allowed" behavior in C? Allowed? Yes. Good idea? Not Usually. C is a shorthand for assembly language, and in assembly language there are no pointers, just memory addresses. C's pointers are memory addresses that have a side behavior of incrementing ...


10

Officially, it's undefined behavior to have a pointer point outside the array (except for one past the end), even if it's never dereferenced. In practice, if your processor has a flat memory model (as opposed to weird ones like x86-16), and if the compiler doesn't give you a runtime error or incorrect optimization if you create an invalid pointer, then the ...


14

You are right that code such as float a = malloc(size); a--; yields undefined behavior, per the ANSI C standard, section 3.3.6: Unless both the pointer operand and the result point to a member of the same array object, or one past the last member of the array object, the behavior is undefined For code like this, the quality of the C code in the ...


2

It's perfectly doable, even in Python. The trick is in returning a wrapper instead of the original stored object. in your example: d['xx'] = range(4) d['xx'].append(5) the second line can be extended into a retrieve and an operation: d['xx'] = range(4) t = d['xx'] t.append(5) and it's obvious that t doesn't notify d about the .append() operation. ...


3

No, this is not just a Python problem. Resolving the question of "has this object been modified since X happened?" (in this case, the object being added to the dictionary) would require one of two things. Either you use immutable objects, in which case the answer is always "no" and the question is rather meaningless, or you have some way to note when an ...



Top 50 recent answers are included