Programming Practice: Data Representation in Memory

  • Memory
    • basic abstraction: array addressable of bytes
    • an area of memory can change to different width
      • width based on context
      • same area of memory can access to different width
    • content of memory represent to 4 possibility:
      • undefined
      • data
      • program instruction
      • address to another memory location
    • what happen when you create a local variable
      • you receive access to a memory space for that variable
      • you can use that local name to access and modify the space
      • if you do not initialize the variable, the memory space store an arbitrary/random/unknown data;  so always initialize it!
      • name of the array is a pointer to the first value storage address
      • add and subtraction can apply to address
        • ex. int x[4], *xprt = x
          • x+2 = &x[2]
          • *(x+2) = x[2]
          • xptr += 2 = x[2]
          • illegal: x+=2
      • sizeof() function—return a int represent how much byte the data type used in memory
      • untitled
      • datatype size vary in size, depending on architecture and compiler
        • solution: use data type with known size
          • ex. <inttypes.h>

留下评论