Effective types, aliases aur access
Permitted access types, representation copies, allocated storage, alignment aur restrict contracts connect karein.
Is chapter mein8 sections
Address par enough bytes hone se har typed access valid nahi ho jata. Object ka type, alignment, lifetime aur access rules phir bhi matter karte hain. Is chapter mein samjhenge ki cast arbitrary storage ko har pointer se padhne layak kyun nahi banata.
1. Multiple valid views same object refer kar sakte hain
int value = 7;
int *writer = &value;
const int *reader = &value;
*writer = 9;
Dono pointers same int object designate karte hain. Const-qualified view compatible type read karne deta hai bina us view se modify karne ki permission ke. Doosre valid access paths freeze nahi hote. Yeh ordinary aliasing hai, violation nahi. Alias free ya scope exit ke baad lifetime extend bhi nahi karta.
2. Effective type allowed stored-value access govern karta hai
Declared-type object ki effective type uski declared type hai. C17 compatible type, appropriate qualified versions, corresponding signed/unsigned types, certain containing aggregates/unions aur character type se stored-value access permit karta hai. Yeh permissions ki structured list hai; pointer cast har new type valid nahi banata.
Int pointer ko float pointer cast karke read karna sizes match hone se valid nahi hota. Misalignment conversion ko hi invalidate kar sakti hai; suitable alignment bhi permitted type access grant nahi karti. Earlier union lesson C ki separate direct union-member rules explain karti hai; woh unrelated pointer punning ki general permission nahi.
3. Character access representation view deta hai
int original = 73;
unsigned char bytes[sizeof original];
memcpy(bytes, &original, sizeof bytes);
int restored = 0;
memcpy(&restored, bytes, sizeof restored);
Object representation unsigned-char array mein copy aur original type ke suitable object mein back copy ho sakti hai. Example original int recover karta hai; bytes ko portable byte order ya external format assign nahi karta. C ki character-type permission mein char, signed char aur unsigned char hain; representation view ke liye unsigned char usual choice hai.
Permission asymmetric hai: int representation character access se read hone ka matlab arbitrary declared char array ko int * se read karna allowed nahi. Array ki alignment add karne se declared type change nahi hoti. Arbitrary bytes ko typed value read karna trap-representation wale implementations/types par trap representation bhi de sakta hai.
4. Allocated storage ki apni declared type nahi hoti
#include <assert.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int original = 73;
void *storage = malloc(sizeof original);
if (storage == NULL) return EXIT_FAILURE;
memcpy(storage, &original, sizeof original);
const int *view = storage;
assert(*view == 73);
free(storage);
return 0;
}
Receiving pointer variable int * hone se allocation automatically int type acquire nahi karta. Non-character lvalue ke through modifying store C17 rule ke under us access aur later nonmodifying accesses ke liye effective type establish karta hai. Memcpy/memmove source ki effective type allocated storage mein carry kar sakte hain, jaisa yahan hai. Other accesses ki apni rule hai; ise permanent unchangeable type tag na samjhein.
Allocation intended object ke liye large enough aur suitably aligned honi chahiye. Malloc is int example ki required fundamental alignment provide karta hai. Already declared array mein bytes copy karna declared type erase nahi karta. Allocated storage, declared objects aur external byte encodings separate rakhein.
5. Restrict scope wala access promise hai
#include <assert.h>
#include <stddef.h>
static void copy_values(int *restrict destination, const int *restrict source, size_t count)
{
for (size_t i = 0; i < count; ++i) destination[i] = source[i];
}
int main(void)
{
const int source[] = { 2, 4, 6 };
int destination[3];
copy_values(destination, source, 3);
assert(destination[0] == 2 && destination[2] == 6);
return 0;
}
Restrict execution-time access contract add karta hai, runtime overlap test nahi. Separate arrays helper ka intended contract satisfy karti hain. Independent restricted destination se modified overlapping elements ko source se read karna contract violate kar sakta hai. Relevant block execution mein modified objects ke accesses permitted restricted pointer family follow karein.
Restrict ka matlab all-pointer-values-different nahi. Read-only accesses, derived pointers, block structure aur modified objects matter karte hain. Overlapping movement chahiye toh suitable byte regions ke liye memmove jaisa designed interface choose aur contracts satisfy karein. Compiler aliasing options/extensions ki separate documentation chahiye; ISO C portable rules rewrite nahi hote.
Primary references aur aage ki reading
- WG14 N1570 — public C11 committee draft: 6.2.6.1, 6.3.2.3p7, 6.5p6–p7, 6.7.3.1, 7.22.3, 7.24.2. C17 baseline ke stable core rules ke liye; final ISO publication nahi.
- WG14 N3096 — public C23 working draft: 6.5, 6.7.3.1. C23 notes C17 example checks se separately source-reviewed hain.
Aage badhne se pehle
- Typed access object, lifetime, alignment aur allowed access type par depend karta hai.
- Character representation view arbitrary typed reinterpretation allow nahi karta.
- restrict relevant accesses ka contract hai; sirf pointers different hone ka synonym nahi.
In ideas ko alag rakhein
- Pointer cast / Valid access
- Cast expression ka type badalta hai; alignment, lifetime ya effective-type requirements automatically establish nahi karta.
Enough bytes wala buffer converted typed pointer ke access ke liye phir bhi unsuitable kyun ho sakta hai?
Optional: bolkar samjhein ya note rakhein. Tayyar hon toh aage badh sakte hain.Apni understanding check karein
Optional hai. Answers save ya send nahi hote; chapter kabhi lock nahi hota.
Har choice ki explanation padhein
- Cast automatically destination type ka naya object bana deta hai.
Pointer ka type badalna generally naya pointed-to object create nahi karta.
- Cast har possible access type valid bana deta hai.
Language restrict karti hai ki kaunse types object ki stored value access kar sakte hain.
- Lifetime, access-type rules aur bounds ko bhi operation permit karna chahiye.
✓ Sahi reasoning. Character-type representation access arbitrary typed reads ki unrestricted permission nahi deta.
C17
Agla idea padhne ke liye tayyar?
Mark this chapter as read whenever you feel ready.