CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
-
date post
21-Dec-2015 -
Category
Documents
-
view
218 -
download
0
Transcript of CSSE 332 Explicit Memory Allocation, Parameter passing, and GDB.
CSSE 332CSSE 332Explicit Memory Explicit Memory
Allocation, Parameter Allocation, Parameter passing, and GDBpassing, and GDB
22
Example 11#include <stdio.h>
int main(int argc, char *argv[]){int *ptr;/* allocate space to hold an int */ptr = (int*)malloc(4 * sizeof(int));
/* do stuff with the space */*ptr=4; //ptr[0] = 4;
/* free up the allocated space */free(ptr);return 0;
}
Explicit memory allocation Explicit allocation and de-allocationExplicit allocation and de-allocation
33
int *ptr;
?
ptr
4000
ptr = (int*)malloc(4 * sizeof(int));
??? ?
6012600860046000
6000
*ptr=4;
4
free(ptr);
Explicit memory allocation
44
Dynamic arrayDynamic array
int *ptr, i,size;int *ptr, i,size;
printf(“Enter the size of the array”);printf(“Enter the size of the array”);
scanf(“%d”, &scanf(“%d”, &sizesize))
ptr = (int *) malloc( ptr = (int *) malloc( size * size * sizeof(int)sizeof(int) ); );
for(i=0; i<size; i++){for(i=0; i<size; i++){
ptr[i] = i;ptr[i] = i;
}}
55
Array of PointersArray of Pointers Variable length stringsVariable length strings
/* card[4] => array of 4 elements/* card[4] => array of 4 elements char* => element is a pointer to a character.char* => element is a pointer to a character. *card[4] => array of 4 pointers */*card[4] => array of 4 pointers */char *card[4]; char *card[4];
card[3]
4012
card[2]
4008
card[1]
4004
card[0]
4000 NULL
NULL
NULL
NULL
66
card[0] = card[0] = (char*)malloc(6*sizeof(char));(char*)malloc(6*sizeof(char));
card[1] = card[1] = (char*)malloc(3*sizeof(char)); (char*)malloc(3*sizeof(char));
. . . . . .
Static allocation of a 2D array:Static allocation of a 2D array:
char card[4][10]; char card[4][10]; //waste of //waste of spacespace
Array of PointersArray of Pointers
77
Common errors - Memory Common errors - Memory leakleak
int *ptr, x; int *ptr, x; //ptr points to newly allocated memory//ptr points to newly allocated memory
ptr = (int*)malloc(10*sizeof(int)); ptr = (int*)malloc(10*sizeof(int)); // ptr now points away from allocated memory // ptr now points away from allocated memory
ptr = &x; ptr = &x;
Memory allocated with malloc is no longer Memory allocated with malloc is no longer available for use by the program.available for use by the program.
Released only when program quits.Released only when program quits. Becomes a problem in large programs where a Becomes a problem in large programs where a
large number of variables are created and large number of variables are created and destroyed during the execution of the program.destroyed during the execution of the program.
88
Common errors - Dangling Common errors - Dangling pointerspointers
int *i, *x;int *i, *x;i = (int*)malloc( 5 x sizeof(int));i = (int*)malloc( 5 x sizeof(int));x = i; x = i; //both point to the same address.//both point to the same address.
/* both i and x are dangling pointers; trying to /* both i and x are dangling pointers; trying to accessaccess
either of them can cause logical errors */either of them can cause logical errors */
free(x);free(x);
x = NULL;x = NULL; // One way to prevent incorrect access // One way to prevent incorrect access i = NULL;i = NULL;
99
Functions – pointers as arguments
Example 12#include <stdio.h>int sumAndInc(int *pa, int *pb,int* pc);
int main(int argc, char *argv[]){ int a=4, b=5, c=6; int *ptr = &b;
/* call the function */ int total = sumAndInc(&a,ptr,&c);
printf(“The sum of %d and %d is %d and c is %p\n”,
a, b, total, c);}/* pointers as arguments */int sumAndInc(int *pa, int *pb,int *pc ){ *pc = *pc+1; // return a pointee value; NOT *(pc+1)
return (*pa+*pb); /* return by value */}
1010
a
4
4000
b
5
4004
c
6
4008
ptr
4004
4012
In main()
pa
4000
6000
pb
4004
6004
pc
4008
6008
In function
1111
a
4
4000
b
5
4004
c
7
4008
ptr
4004
4012
In main() after the function call
1212
What’s wrong with this ?Example 13#include <stdio.h>
void DoSomething(int *ptr);
int main(int argc, char *argv[]) {int *p;DoSomething(p);printf(“%d”, *p); /* will this work ? */return 0;
}/* passed and returned using pointers */void DoSomething(int *ptr){ int temp= 5+3; ptr = &temp;}
/* compiles correctly, but gives incorrect output */
1313
p
?
4000
ptr
6000
?temp
6004
86004
In main()
In the function
1414
p
?
4000
In main() after the function call
int main(int argc, char *argv[]) {int main(int argc, char *argv[]) {int *p;int *p;DoSomething( DoSomething( &p&p ); );printf(“%d”, *p); printf(“%d”, *p); /* will this work ? *//* will this work ? */return 0;return 0;
}}/* passed and returned using pointers *//* passed and returned using pointers */void DoSomething(int void DoSomething(int **ptr**ptr){ ){ int temp= 8;int temp= 8; *ptr = (int*)malloc(sizeof(int));*ptr = (int*)malloc(sizeof(int)); **ptr = temp;**ptr = temp;}}
1616
p
?
4000
ptr
6000
temp
6004
84000
In main()
In the function
?
8000
On the heap8000
8
1717
p
?
4000
In main()
In the function
?
8000
On the heap8000
8
1818
Functions - Passing and returning arraysExample 14#include <stdio.h>
void init_array( int array[], int size ) ;
int main(int argc, char *argv[] ){ int list[5]; init_array(list, 5); //No & because list //is already an address for (i = 0; i < 5; i++) printf(“next:%d”, list[i]);}
/* why size ? */void init_array(int array[], int size) {
/* arrays ALWAYS passed as pointers */ int i; for (i = 0; i < size; i++) array[i] = i; }
1919
Passing/Returning a structure
/* pass struct by value */void displayYear_1(struct birthday mybday) {
printf(“I was born in %d\n”, mybday.year);}/* - inefficient: why ? */
/* pass pointer to struct */void displayYear_2(struct birthday *pmybday) {
printf(“I was born in %d\n”, pmybday->year);/* Note: ‘->’, not ‘.’, after a struct pointer */
}
/* return struct by value */struct birthday get_bday(void){
struct birthday newbday;newbday.year=1971; /* ‘.’ after a struct */
return newbday;}/* - also inefficient: why ? */
2020
GNU debugger (gdb)GNU debugger (gdb)
Tutorial and reference sheet on class Tutorial and reference sheet on class websitewebsite– ……/Homework/Sec-01/gdbtutorial.htm/Homework/Sec-01/gdbtutorial.htm– ……/Homework/Sec-01/gdb-reference-/Homework/Sec-01/gdb-reference-
card.pdfcard.pdf
Homework 4 and 5Homework 4 and 5
More practice on C.More practice on C. Available on class’s Angel pageAvailable on class’s Angel page
– follow link from the schedule pagefollow link from the schedule page
2121