Re: void *malloc(size_t num_bytes);



>Chris Torek wrote:
>> #include <stdio.h>
>> struct S { char a[1]; };
>> int main(void) {
>> struct T { struct S { char a[1000]; } a; };
>> printf("sizeof(struct S) = %lu\n", (unsigned long)sizeof(struct S));
>> return 0;
>> }
>> (Exercise: what is the most likely output of the above, as both
>> C and C++? Why is it different?)

In article <1119206593.085547.42110@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>
forayer <theforayer@xxxxxxxxxxx> wrote:
>1000 using C, and 1 using C++. My money is on scope-resolution
>differences between the languages. Please correct me if I am wrong.

Got it in one. Of course, neither 1 nor 1000 are guaranteed
(because the structures may include padding), but they are at
least extremely likely to be different. :-)

>> ... There are machines on which [(*f)(), where f==NULL,] turns
>>out to be a recursive call to main().

>I really didn't know that, thank you. :-)

I saw in another thread that you were wondering where this was the
case. The case I was thinking of in particular occurred on the
VAX running 4.xBSD, but it could occur on any similar machine.
The most basic requirement is that the NULL function pointer must
point to executable code. This was true since NULL on that machine
was 32-zero-bits, and 4BSD had the program starting on page zero
(VMS, by contrast, made page-zero off limits by default, so that
*(char *)0 got a runtime fault, and attempts to call through NULL
also faulted).

The second tricky bit was that location 0 began with a 16-bit
register mask, even though the kernel launched executables by
pointing the program counter to the startup code rather than
executing the VAX's normal subroutine-call instruction, "CALLS".
(The startup code was thus at address 2. As I recall, 0x00 is a
VAX "HALT" instruction, so the two zero bytes at location zero
would have caused trouble if the binary were launched at address
0. However, having the register-save-mask there enables one to
use the VAX's CALLS instruction.)

The final tricky bit was that this recursive call to main()
would only work if the registers were set up properly. I have
forgotten what the register setup was supposed to be, and just
how tolerant this all was (whether by design or coincidence).
I do, however, remember having one program blow up with a stack
overflow, and in the debugger, seeing a seemingly-infinite
sequence of calls starting with the startup code calling main()
which called some functions which eventually called through the
NULL function-pointer which called main(), etc.

One could get the same effect on other machines, for the same
reasons.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
.



Relevant Pages

  • [OT] Re: choice of operation (-- or =-1)
    ... (resulting with a value of zero in that register), ... than it was to assign a value of zero to that register, ... If the instruction refers solely to registers, ... This is common to the point that seeing "mov reg, ...
    (comp.lang.c)
  • Re: cl.exe (x86 for amd64) bugs
    ... extend if the lower 32 bits are modified by an instruction. ... of some register and when your process is resumed, ... an interesting mix of promotions, non-promotions, default operand size, zero ... I just looked at the AMD web site and see some updates and new info on the ...
    (microsoft.public.development.device.drivers)
  • Re: choice of operation (-- or =-1)
    ... to bitwise xor a register with itself ... (resulting with a value of zero in that register), ... than it was to assign a value of zero to that register, ... had the advantage of being a two-byte instruction rather than ...
    (comp.lang.c)
  • Re: How does this make you feel?
    ... >> XOR instruction that applies to a 1M range of VM; ... > register gives a memory address, and the second gives a byte-count (up ... in a CPU that has one execution pathway. ... instruction set and the specifics of its addressing modes; ...
    (comp.arch)
  • Re: Designing my own architecture to be simulated in software - need help with the ISA
    ... > memory address range is limited to 16 bits. ... > I'm reserving the HO byte of the word for the instruction type, ... The register operands are half-bytes in length, ... > sub - store the difference of two registers in a register ...
    (comp.arch)