Skip to main content
added 2 characters in body
Source Link
Adrian Mole
  • 52.1k
  • 193
  • 61
  • 101

Is this valid C code without undefined behaviour?

Yes – Once the a variable has been declared in a given scope (like a function or other { ... } delimited block), it is valid to take its address and access the variable using that address within that scope (as your memset call does). An attempt to use that address from outsidewhen that scope has ended (i.e. is no longer 'active') will cause undefined behaviour; for example, the following is UB:

int main()
{
    int* p;
    { // New scope ...
        int a;
        p = &a; // Pointer to "a" is valid HERE
    } // The scope of "a" (and its 'lifetime') ends here
    memset(p, 5, sizeof(int)); // INVALID: "p" now points to a dead (invalid) variable
}

However, there's a major caveat in your code sample …

I'm assuming this is equal to just doing int a = 5.

There's the rub: It's assigning 5 to each component byte of the a variable, so it's doing this (assuming a 4-byte int):

int a = 0x05050505;

Which is the same as:

int a = 84215045;

Is this valid C code without undefined behaviour?

Yes – Once the a variable has been declared in a given scope (like a function or other { ... } delimited block), it is valid to take its address and access the variable using that address within that scope (as your memset call does). An attempt to use that address from outside that scope will cause undefined behaviour; for example, the following is UB:

int main()
{
    int* p;
    { // New scope ...
        int a;
        p = &a; // Pointer to "a" is valid HERE
    } // The scope of "a" (and its 'lifetime') ends here
    memset(p, 5, sizeof(int)); // INVALID: "p" now points to a dead (invalid) variable
}

However, there's a major caveat in your code sample …

I'm assuming this is equal to just doing int a = 5.

There's the rub: It's assigning 5 to each component byte of the a variable, so it's doing this (assuming a 4-byte int):

int a = 0x05050505;

Which is the same as:

int a = 84215045;

Is this valid C code without undefined behaviour?

Yes – Once the a variable has been declared in a given scope (like a function or other { ... } delimited block), it is valid to take its address and access the variable using that address within that scope (as your memset call does). An attempt to use that address when that scope has ended (i.e. is no longer 'active') will cause undefined behaviour; for example, the following is UB:

int main()
{
    int* p;
    { // New scope ...
        int a;
        p = &a; // Pointer to "a" is valid HERE
    } // The scope of "a" (and its 'lifetime') ends here
    memset(p, 5, sizeof(int)); // INVALID: "p" now points to a dead (invalid) variable
}

However, there's a major caveat in your code sample …

I'm assuming this is equal to just doing int a = 5.

There's the rub: It's assigning 5 to each component byte of the a variable, so it's doing this (assuming a 4-byte int):

int a = 0x05050505;

Which is the same as:

int a = 84215045;
added 356 characters in body
Source Link
Adrian Mole
  • 52.1k
  • 193
  • 61
  • 101

Is this valid C code without undefined behaviour?

Yes – Once the a variable has been declared in a given scope (like a function or other { ... } delimited block), it is valid to take its address and access the variable using that address within that scope (as your memset call does). An attempt to use that address from outside that scope will cause undefined behaviour; for example, the following is UB:

int main()
{
    int* p;
    { // New scope ...
        int a;
        p = &a; // Pointer to "a" is valid HERE
    } // The scope of "a" (and its 'lifetime') ends here
    memset(p, 5, sizeof(int)); // INVALID: "p" now points to a dead (invalid) variable
}

However, there's a major caveat in your code sample …

I'm assuming this is equal to just doing int a = 5.

There's the rub: It's assigning 5 to each component byte of the a variable, so it's doing this (assuming a 4-byte int):

int a = 0x05050505;

Which is the same as:

int a = 84215045;

Is this valid C code without undefined behaviour?

Yes – Once the a variable has been declared in a given scope (like a function or other { ... } delimited block), it is valid to take its address and access the variable using that address (as your memset call does).

However, there's a major caveat in your code sample …

I'm assuming this is equal to just doing int a = 5.

There's the rub: It's assigning 5 to each component byte of the a variable, so it's doing this (assuming a 4-byte int):

int a = 0x05050505;

Which is the same as:

int a = 84215045;

Is this valid C code without undefined behaviour?

Yes – Once the a variable has been declared in a given scope (like a function or other { ... } delimited block), it is valid to take its address and access the variable using that address within that scope (as your memset call does). An attempt to use that address from outside that scope will cause undefined behaviour; for example, the following is UB:

int main()
{
    int* p;
    { // New scope ...
        int a;
        p = &a; // Pointer to "a" is valid HERE
    } // The scope of "a" (and its 'lifetime') ends here
    memset(p, 5, sizeof(int)); // INVALID: "p" now points to a dead (invalid) variable
}

However, there's a major caveat in your code sample …

I'm assuming this is equal to just doing int a = 5.

There's the rub: It's assigning 5 to each component byte of the a variable, so it's doing this (assuming a 4-byte int):

int a = 0x05050505;

Which is the same as:

int a = 84215045;
edited body
Source Link
Adrian Mole
  • 52.1k
  • 193
  • 61
  • 101

Is this valid C code without undefined behaviour?

Yes – butOnce the a variable has been declared in a given scope (like a function or other { ... } delimited block), it is valid to take its address and access the variable using that address (as your memset call does).

However, there's a major caveat in your code sample

I'm assuming this is equal to just doing int a = 5.

There's the rub: It's assigning 5 to each component byte component byte of the a variable to 5, so it's doing this (assuming a 4-byte int):

int a = 0x05050505;

Which is the same as:

int a = 84215045;

Is this valid C code without undefined behaviour?

Yes – but there's a caveat …

I'm assuming this is equal to just doing int a = 5.

There's the rub: It's assigning each component byte of the a variable to 5, so it's doing this (assuming a 4-byte int):

int a = 0x05050505;

Which is the same as:

int a = 84215045;

Is this valid C code without undefined behaviour?

Yes – Once the a variable has been declared in a given scope (like a function or other { ... } delimited block), it is valid to take its address and access the variable using that address (as your memset call does).

However, there's a major caveat in your code sample

I'm assuming this is equal to just doing int a = 5.

There's the rub: It's assigning 5 to each component byte of the a variable, so it's doing this (assuming a 4-byte int):

int a = 0x05050505;

Which is the same as:

int a = 84215045;
added 6 characters in body
Source Link
Adrian Mole
  • 52.1k
  • 193
  • 61
  • 101
Loading
Source Link
Adrian Mole
  • 52.1k
  • 193
  • 61
  • 101
Loading