(New page: int * a; a’s value is an address At that address, stores an integer (integer pointer) c = * a; if it’s * a on the right hand side it means to go to that variable’s address to retrie...)
 
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
int * a;
+
[[Category:ECE264]] [[Category:Programming]] [[Category:C]] [[Category:lecture notes]]
a’s value is an address
+
At that address, stores an integer (integer pointer)
+
  
c = * a;
+
=Lecture 7, [[ECE264]], Spring 2012, Prof. Lu=
if it’s * a on the right hand side
+
----
it means to go to that variable’s address to retrieve value
+
int * a;<br>a’s value is an address<br>At that address, stores an integer (integer pointer)
  
* a = * b;
+
c = * a;<br>if it’s * a on the right hand side<br>it means to go to that variable’s address to retrieve value
We take b’s value as address, and read value
+
Then we take a’s value as address, modify the value at that address
+
(Start on the right hand side, then work to the left hand side)
+
  
int * a = 5
+
* a = * b;<br>We take b’s value as address, and read value<br>Then we take a’s value as address, modify the value at that address<br>(Start on the right hand side, then work to the left hand side)
This will be an error
+
  
int a = 5;
+
int * a = 5<br>This will be an error
int * b = & a;
+
a is an integer
+
b is a pointer, and the value is the address of a
+
A pointer’s value is an address and an integer is stored at that address
+
  
If a pointer is on the left hand side of an assignment (=), it is to be modified.
+
int a = 5;<br>int * b = &amp; a;<br>a is an integer<br>b is a pointer, and the value is the address of a<br>A pointer’s value is an address and an integer is stored at that address
If a pointer is on the right hand side of an assignment (=), it is to be read
+
  
printf(“%d\n”, * b);
+
If a pointer is on the left hand side of an assignment (=), it is to be modified.<br>If a pointer is on the right hand side of an assignment (=), it is to be read
Because * b is considered on the RHS, it reads the value at that address (i.e. -9).
+
If you use printf(“%d\n”, b); in the function that initializes as * b, then the function will print the address as a value (i.e. 103). The same applies when you use &b.
+
  
 +
printf(“%d\n”, * b);<br>Because * b is considered on the RHS, it reads the value at that address (i.e. -9).<br>If you use printf(“%d\n”, b); in the function that initializes as * b, then the function will print the address as a value (i.e. 103). The same applies when you use &amp;b.
  
  
Line 34: Line 22:
  
  
IPA1-1:
 
• read a maze file once to find the width and height
 
• allocate a one or two-dimensional array
 
• use fseek to reset the file pointer to the beginning
 
• read the file again and store the maze in the array
 
Today’s topic
 
• pointer (value is an address)
 
• heap memory
 
• malloc and free
 
Practice:
 
• Use one-dimensional array to implement two-dimensional array
 
• (hint: convert [row][col] to [index])
 
Practice:
 
• allocate the array without reading the file twice
 
• (hint: use fseek and ftell)
 
  
Heap Memory
+
IPA1-1:<br>• read a maze file once to find the width and height<br>• allocate a one or two-dimensional array<br>• use fseek to reset the file pointer to the beginning<br>• read the file again and store the maze in the array<br>Today’s topic<br>• pointer (value is an address)<br>• heap memory<br>• malloc and free<br>Practice:<br>• Use one-dimensional array to implement two-dimensional array<br>• (hint: convert [row][col] to [index])<br>Practice:<br>• allocate the array without reading the file twice<br>• (hint: use fseek and ftell)
It is random access:  
+
You don’t need to follow the order first-in-last-out
+
It comes at a price: you must allocate and you must release (garbage collection like Java)
+
If a program does a lot for you, it tends to be slower. C is usually faster than Java
+
If you don’t release the memory, it’s called a memory leak (allocated, but not released)
+
If you have a memory leak in your program, you will have a -50% penalty. Memory leaks will cause unexpected crashes of your program.  There are easy tools to check for memory leaks.
+
  
Memory Allocation
+
Heap Memory<br>It is random access: <br>You don’t need to follow the order first-in-last-out<br>It comes at a price: you must allocate and you must release (garbage collection like Java)<br>If a program does a lot for you, it tends to be slower. C is usually faster than Java<br>If you don’t release the memory, it’s called a memory leak (allocated, but not released)<br>If you have a memory leak in your program, you will have a -50% penalty. Memory leaks will cause unexpected crashes of your program. There are easy tools to check for memory leaks.
There are two ways to create a one-dimensional array.
+
1) You know the size; it’s a fixed size.
+
2) Array size unknown, increase as program continues
+
C does not initialize arrays; indexes start from zero
+
  
int arr[100];
+
Memory Allocation<br>There are two ways to create a one-dimensional array.<br>1) You know the size; it’s a fixed size.<br>2) Array size unknown, increase as program continues<br>C does not initialize arrays; indexes start from zero
int x;
+
 
If array starts at address 200, x will have an address of 300. It reserves 100 spaces for your array.
+
int arr[100];<br>int x;<br>If array starts at address 200, x will have an address of 300. It reserves 100 spaces for your array. <br>
 +
 
 +
----
 +
[[2012_Spring_ECE_264_Lu|Back to ECE264, Spring 2012, Prof. Lu]]

Latest revision as of 08:23, 13 February 2012


Lecture 7, ECE264, Spring 2012, Prof. Lu


int * a;
a’s value is an address
At that address, stores an integer (integer pointer)

c = * a;
if it’s * a on the right hand side
it means to go to that variable’s address to retrieve value

  • a = * b;
    We take b’s value as address, and read value
    Then we take a’s value as address, modify the value at that address
    (Start on the right hand side, then work to the left hand side)

int * a = 5
This will be an error

int a = 5;
int * b = & a;
a is an integer
b is a pointer, and the value is the address of a
A pointer’s value is an address and an integer is stored at that address

If a pointer is on the left hand side of an assignment (=), it is to be modified.
If a pointer is on the right hand side of an assignment (=), it is to be read

printf(“%d\n”, * b);
Because * b is considered on the RHS, it reads the value at that address (i.e. -9).
If you use printf(“%d\n”, b); in the function that initializes as * b, then the function will print the address as a value (i.e. 103). The same applies when you use &b.




IPA1-1:
• read a maze file once to find the width and height
• allocate a one or two-dimensional array
• use fseek to reset the file pointer to the beginning
• read the file again and store the maze in the array
Today’s topic
• pointer (value is an address)
• heap memory
• malloc and free
Practice:
• Use one-dimensional array to implement two-dimensional array
• (hint: convert [row][col] to [index])
Practice:
• allocate the array without reading the file twice
• (hint: use fseek and ftell)

Heap Memory
It is random access:
You don’t need to follow the order first-in-last-out
It comes at a price: you must allocate and you must release (garbage collection like Java)
If a program does a lot for you, it tends to be slower. C is usually faster than Java
If you don’t release the memory, it’s called a memory leak (allocated, but not released)
If you have a memory leak in your program, you will have a -50% penalty. Memory leaks will cause unexpected crashes of your program. There are easy tools to check for memory leaks.

Memory Allocation
There are two ways to create a one-dimensional array.
1) You know the size; it’s a fixed size.
2) Array size unknown, increase as program continues
C does not initialize arrays; indexes start from zero

int arr[100];
int x;
If array starts at address 200, x will have an address of 300. It reserves 100 spaces for your array.


Back to ECE264, Spring 2012, Prof. Lu

Alumni Liaison

Ph.D. 2007, working on developing cool imaging technologies for digital cameras, camera phones, and video surveillance cameras.

Buyue Zhang