:: Re: [DNG] Making sense of C pointer…
Top Page
Delete this message
Reply to this message
Author: Rainer Weikusat
Date:  
To: dng
Subject: Re: [DNG] Making sense of C pointer syntax.
Steve Litt <slitt@???> writes:
> On Mon, 28 Mar 2016 13:57:08 -0300
> Emiliano Marini <emilianomarini82@???> wrote:
>
>> char *p;
>> p="01234"; /* skeezy, but makes the point */
>>
>> Warning! Here "p" is pointing to nowhere, you don't know which memory
>> locations are writing to.
>
> Yeah, that's why I said "skeezy". But on some of compilers, you can
> actually strcpy(p, "43210") and you will neither get a compile time
> error nor a runtime one, because when p is in scope, it points to 6
> bytes *somewhere*, even if on the stack.


That's not necessarily true. Eg, on a 64-bit Intel machine using gcc
4.7.2, this program

--------
static long long *llp(void)
{
    long long x;


    x = -2;
    return &x;
}


static void *ouch(void)
{
    char *p;


    *p = 3;
    return &p;
}


int main(void)
{
    llp();
    ouch();


    return 0;
}
--------


will segfault because the pointer gets the value assigned to the long
long used earlier.