CSc 352: Lecture-11

Arrays, Pointers, Strings, gdb

Note: All the programs mentioned in this lecture are in:

/home/cs352/SUMMER02/lecture11progs/

	

- MULTIDIMENSIONAL ARRAYS
	
	- int a[3];              (one dimensional array)
	  int a[3][5];   	  (two dimensional array)
	  int a[3][5][7];	  (three dimensional array)
	  ...


	- Two dimensioanl arrays:
	  The arrangement of array elements(rows and columns)
	  We can think of a[i][j] as the element in ith row, jth column
	

	- Given array declaration a[3][5];
	  a[i][j] is equivalent to:
	  *(a[i]+j)    	OR
	  (*(a+i))[j] 	OR
	  *((*(a+i))+j) OR
	  *(&a[0][0] + 5*i + j)	   

	  For the first three can think of a[i] as another array, say b.
	  For the last one think of the arrangement in memory.  

	  We can think of a[i] as the ith row of a.	  
	  We can think of a as &a[0]
	  Base address is &a[0][0].

	  see twodim.c

	  NOTE: 
	  In a function, we didn't have to specify the array size
	  while giving as a formal parameter for one dimensional arrays.
	  However if "a" is two dimensional, when specifying as formal 
	  parameter, its column size has to be given.(Why??)
	

	- Initialization:
	  Similar to initialization of 1-dimensional.

	  see sum.c
	

- ARRAYS OF POINTERS

	- see wordsort.c 		




- ARGUMENTS TO main()

	- two arguments, conventionally called argc and argv are used
	

	- void main(int argc, char *argv[])
	argv[0] contains the command name itself
	so argc is at least 1.


	- see arguments.c 





- DEBUGGING

        - see errwordsort.c

	- use gdb(GNUdebugger).

	- compile with -g option.

	- gdb prog_name starts debugging program prog_name

	- Common commands:
	  run (OR just r)  
		  starts the program and executes until a stop.
		  (break point, signal, after 'step'...)

	  run > outfile  
		  redirects into output.

 	  info program
		  displays info about the status of the program.
		  whether running or not, why it stopped etc.

	  break (OR just b)
		  sets a break point. can be followed by 
		  line_num, func_name, filename:line_num, +offset etc.
	          these can further be followed by an expr,
		  say if ...
		  Ex:b main.c:30 if 1==100

	  info break
		  gives info on the break points

	  watch expr
		  sets a breakpoint. There'll be break when
		  value of expr changes.

	  clear ...
		  can be followed by line_num, etc.
	   	  deletes the specified breakpoint

	  continue (OR just c)
		  continue execution after a break(until maybe the 
		  next breakpoint is reached.)

	  step (OR just s) 
		  step into the next line. can be followed by a number.

	  next (OR just n)
		  same as step but doesn't go into a function.
	  
	- Signals
	  An asynchronous eevent that can happen in a program.
	  Ex: SIGINT   interrupt signal(Ctrl-C)
	      SIGSEGV  referencing a place in memory that is 
		       out of reach, kills the prog immediately.

	- Printing source lines
	  list line_num

	- Examining data
	  print (OR just p) expr
		prints out the value of expr(either global or visible
		accd. to scope rules from the point of execution.)
	  Ex:
		printing out an array(dynamically allocated), i.e.,
		int *p1=(int *)malloc(len*sizeof(int));
		then,
		p *array@len    in gdb prints out the array.
		to the left @ firts elem of the desired array
		to the right the length desired
		
	  the values printed can be reached using $ for prev.,
	  $$ for prev. prev., $$2 for prev.prev.prev, etc.
	  Ex:
		say just printed the value of a pointer-addr.
		p *$  prints the value at that addr.

	- stack Frames
	  frame 
		prints the current stack frame, can be folowed
		by a stack frame #
	  backtrace (OR just bt)
		prints a backtrace of the entire stack, can be
		followed by a number(innermost ones then)