Re: Strange code generated by compiler

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



autumn48@xxxxxxxxx wrote:
why does the program randomly copy the value to be assigned, to
a register before moving it to the actual memory destination (like
when it assigns 22 to v[7], 55 to v[8] and 12 to v[10]). this doesn't
happen when using a normal c style array.

In case of a C array allocated on the stack:

int x[10];
x[0] = 22;

The compiler knows the address of the memory where the elements are located, so it can hard code it for you:

mov [esp+14h], 22

A C++ vector internally uses a C array too, but the memory is allocated dynamically (runtime) on the heap. The address of a vector object doesn't directly point to its internal array. A pointer must be dereferenced first:

vector<int> x(10);
x[0] = 22;

What happens here is that vector<T>::operator[] gets called, where an internal pointer gets dereferenced, like this:

x.internal_pointer[0] = 22;

The compiler can not hard code the address of anything allocated at runtime, it has to load it from a pointer into a register first, at least on the x86 processor family:

mov eax, [esp+14h] ; operator*: resolve pointer
mov [eax], 22 ; operator=: assign value

Try to compile the following piece of code:

int* ptr = (int*)malloc(10 * sizeof(int));
ptr[0] = 22;
ptr[1] = 55;
[...]
free(ptr);

The resulting assembly code should be very similar in nature to your vector example. In the code above, 'ptr' is not the direct address of your array, but a pointer that contains the address, so it essentially generates the same machine instructions as vector did (with physical memory offsets differring, of course).

Tom
.



Relevant Pages

  • Re: The value of a CS education
    ... multidimensional arrays badly as a pointer to a pointer. ... demand paged virtual memory machines to their knees. ... Fast algorithms to do this array transpose efficiently have to be cache ... Its interesting to watch how the performance of many applications ...
    (sci.electronics.design)
  • Re: Mex Overflow Error Using Free Borland Compiler
    ... >>>in the Borland compiler. ... > handles memory questions, so I prefer to use the C functions I know. ... >>1- You declare the variables of type double and allocate of size long double. ... You'll get exceptions a lot quicker dereferncing a NULL pointer than a "wild ...
    (comp.soft-sys.matlab)
  • Re: Need help to port VAX code to Alpha and to Itaninum
    ... Not really, the original code was wrong, and the compiler was not ... pointer to the start of the array. ... So you are trying to pass a pointer to a pointer to an array where you ... Also start looking at where you can add the "const" modifier to function ...
    (comp.os.vms)
  • Re: Mex Overflow Error Using Free Borland Compiler
    ... >>Borland compiler handles overflow errors. ... >>The first thing I tried is dynamic memory allocation. ... 1- You declare the variables of type double and allocate of size long double. ... would clean up a lot of code by eliminating the pointer dereferences. ...
    (comp.soft-sys.matlab)
  • Re: OT: Requesting C advice
    ... some behind the scenes action of the compiler. ... In fact the memory could ... Proper initialization means that floats and doubles must be initialized to 0.0 and pointers must be initialized to the null pointer value, even if those bit patterns differ from all-bits-zero. ...
    (Fedora)