Showing posts with label Pointers. Show all posts
Showing posts with label Pointers. Show all posts

Tuesday, September 14, 2010

Pointers to pointers, and arrays and strings II

  1. (*ptoStruct).member; is same as ptoStruct->member
  2. char name[40] = “mithun”, allocate 40 bytes on static memory block, and occupy the first 7 bytes (6 + ‘\0’), and “name” is a array constant, un modifiable lvalue
  3. char name[] = “mithun” , count number of chars, allocate memory for count+1 on static memory block, and “name” is a array constant un modifiable lvalue
  4. char* name = “mithun” ,  allocate the memory for number of characters + 1 for nul, and also 4 bytes for the pointer variable “name”, and “name” is a variable
  5. in the array notation name is the address of the first element i.e. &name[0], and will always point to the same location (a constant).

Sunday, July 4, 2010

Pointers to pointers,and arrays and strings I

  1. the value stored (rvalue) and the address (lvalue) at which the value is stored and the name(variable name) which is mapped(in symbol table) to this address
  2. variable for storing lvalue i.e. address is “pointer”
  3. address of operator is ‘&’ , to get the address of any variable
  4. dereferencing operator is ‘*’ , to get the value which the pointer variable is pointing
  5. pointer arithmetic involves modifying address 
    1. so for a char* cptr saying cptr + 1, will increment the address by one (sizeof char)
    2. for a int* iptr saying iptr + 1, will increment the address by four (sizeof int) 

Sunday, June 27, 2010

char[] and char*

  1. char data[] = “data”;
    1. data: [D] [A] [T] [A] [\0]
  2. so , char data[] is a array holding the data\0
  3. the individual characters can be modified
  4. but data will always refer to the same storage

 

  1. char *data = “data”;
    1. [data] –> [D] [A] [T] [A] [\0]
  2. so, char *data is a pointing to a string constant
  3. the pointer can be modified to point to other location
  4. modifying the string constant gives undefined result (SIGSEGV in gcc)

Deep Copy

  1. needed when the class has dynamically allocated members
  2. always remember the explicit this pointer of c++
  3. copy constructor is implemented (algorithm)
    1. allocate memory for dynamically allocated members
    2. copy the data from const_argument.member to this.member
  4. assignment operator is implemented (algorithm)
    1. check for self assignment
    2. delete memory for this.member
    3. allocate new memory for this.member
    4. copy the data from const_argument.member to this.member
    5. return this
  5. when copy constructor is called
    1. doing initialization with objects, like
      1. class1 object1(“value”);
      2. class1 object2 = object1; //object to object assignment
    2. passing a object by value
    3. returning a object by value
  6. when assignment operator is called
    1. assigning a existing object(object with memory for its data members) a new object