Download presentation
Presentation is loading. Please wait.
1
CSCI206 - Computer Organization & Programming
C Arrays, String library, and Memory zyBook: 6.2, 6.9, 6.10, 6.12, 6.13
2
Partner Activity #include <stdio.h> char a[80]; int main(){ char b[80]; printf("a is at %p\n", &a[0]); printf("b is at %p\n", &b[0]); } This declares two 80 byte arrays of characters (strings). The output is: $ ./mem a is at 0x601060 b is at 0x7ffd5beafab0 Talk to your partner about what you think this might mean about how C variables are located in memory. format as a pointer (memory location) address of first character in array. Try out address.c
3
Memory Management Because C deals directly with memory, we have to understand the program’s view of memory To keep things organized, memory is split into (at least) 4 segments: Stack Heap Data Code
4
Memory Map Dynamic (runtime) allocation
a is at 0x601080 b is at 0x7ffd23ea0ad0 Local (automatic) variables inside functions; activation records (recursion); unsafe to share between functions / modules Dynamic (runtime) allocation Dynamically allocated memory: malloc and free; safe to share between functions / modules Global variables Static (compile time) allocation Your program’s compiled machine code
5
Global data memory allocation
#include <stdio.h> char a[80]; int main(){ //... } Data used multiple places (different functions) in your program. can be exported across modules (extern). Minimize use to keep your program clean.
6
Local memory allocation
int main(){ char b[80]; //... } Data used within a single function. Unsafe to share with other functions! Use whenever possible to isolate variables to the function they belong.
7
Arrays dataType identifier[numElements]; Creates numElements of dataType sequentially in memory from some starting address. Array index starts at 0 (first element is [0]). e.g., char name[32]; name[0] = ‘A’;
8
sizeof sizeof(identifier) returns the total number of bytes in memory for identifier. e.g., char a; printf(“%d\n”, sizeof(a)); // prints 1 char *b; printf(“%d\n”, sizeof(b)); // prints 8 int nums[20]; printf(“%d\n”, sizeof(nums)); // prints 80 Try out sizeof.c
9
Array initialization int x[10]; // no intialization
Warning, some of these are compiler specific and may behave differently on different compilers. Check the manual! Verify assumptions! int x[10]; // no intialization int x[10] = {};// no initialization int x[10] = {1,2,3,4,5,6,7,8,9,10}; int x[10] = {0}; // set first int to 0 int x[10] = {1}; // set first int to 1 Try out array-example.c
10
Alternate Initialization
string.h has a useful function for initialization. void bzero(void *s, size_t n); n bytes starting from s set to zero void *memset(void *s, int c, size_t n); n bytes starting from s are set to the value c. memset(my_array, 42, sizeof(my_array)); // every byte in my_array set to 42
11
2d arrays As simple as adding an extra [x] in code float rain[5][12];
C stores arrays in row major order array[row][column] this means rows are stored together
12
float rain[5][12]; (logically)
… rain[0][11] rain[1][0] rain[1][1] rain[1][2] rain[1][11] ... rain[4][0] rain[4][1] rain[4][2] rain[4][11] 5
13
float rain[5][12]; (logically) rain[1] is highlighted.
… rain[0][11] rain[1][0] rain[1][1] rain[1][2] rain[1][11] ... rain[4][0] rain[4][1] rain[4][2] rain[4][11] 5
14
float rain[5][12]; (physically)
N dimensional arrays are physically stored as a long 1 dimensional array in memory C translates your N-dimensional indexes to the correct offset from the beginning of the array (memory address) 5*12 rain[0][0] rain[0][1] rain[0][2] ... rain[0][11] rain[1][0] rain[1][1] rain[4][11]
15
Strings Don’t compare strings using == #include <string.h>
char *str == char str[]; is *almost* the same thing… they can be used interchangeably. Strings char *str = “initial value”; char str[] = “initial value”; Don’t compare strings using == this will compare the address of the string. #include <string.h> strcmp - lexical order of strings, 0 if the same strcat - concatenate strings strstr - find a substring in a string strcpy - copy a string strtok - split a string using a delimiter
16
strtok example 1/2 #include <stdio.h> #include <string.h> int main() { char message[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; char* word; /* get the first word from the message, separated by * space character */ word = strtok(message, " "); printf("1st word: %s\n", word); 1st word: Lorem 1st call to strtok, pass the string (buffer) and delimiter. 1st matching token is returned (delimiters are removed).
17
char message[] = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
strtok example 2/2 /* get the second word from the message, NULL must be * used to get tokens from the previous string now */ word = strtok(NULL, " "); printf("2nd word: %s\n", word); /* the following loop gets the rest of the words until the * end of the message */ while ((word = strtok(NULL, " ")) != NULL) printf("Next: %s\n", word); } 2nd word: ipsum Next: dolor Next: sit Next: amet, Next: consectetur Next: adipiscing Next: elit. subsequent calls FOR THE SAME STRING, pass NULL as the string! The delimiter CAN change. YOU can only tokenize one string at a time! When there are no more tokens, strtok returns NULL Try out string.c
18
Strings - possible errors
char *string; char *string = “initial value”; char string[100] = “initial value”; string = “new value”;
19
Strings - possible errors
char *string; no memory is allocated. char *string = “initial value”; “initial value” is const, so as the variable string, can’t be changed. char string[100] = “initial value”; is 100 bytes enough? string = “new value”; don’t assign with strings (strncpy). until you understand pointers.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.