SOFT3410 - C language in system programming

Core Dump/Segmentation Fault

core dump caused by modifying constants

char *q = "cannot be modified.";
*(q + 1) = 'A';//Modified string literal

1 pointer

1.1 Dangling pointers

Pointer to the memory area that has been freed. As shown below, the local variable of the sub function has been released, but its reference is returned to a pointer in the main function. Change to static variable. Global.

So don't return the address of the local variable of the function.

1.2 wild pointer

Defines an uninitialized pointer.

1.3 pointers and arrays

It's best to learn through code examples!

1.3. 1 pointer and algorithm

#include<cstdio>
#include<bits/stdc++.h>
using namespace std;

void fun(int arr[]) {
  int i = 0;
  cout << sizeof(arr)<<endl;
  cout << sizeof(arr[0])<<endl;
  int arr_size = sizeof(arr)/sizeof(arr[0]);
  for (i = 0; i < arr_size; i++) {
    printf("%d",arr[0]);
    }
  }
int main() {
  int arr[4] = {1,2,3};
  printf("%d", sizeof(arr[0]));
  printf("%d", sizeof(arr));
  fun(arr);
  return 0;
}

Memory


Question: what are the seven areas above?
Why put the text instruction below the stack?
Textwhy is it read-only? (string literals, const variable and pointer of const variable are stored)

2.1 function call stack

2.2 reactor

2.2.1 void *malloc(size_t size);

2.2.2 void *calloc(size_t num, size_t size);

2.2.3 void free(void *ptr);

2.2. 4 memory leak

2.2.5 void *realloc(void *ptr, size_t size);

3 best practices on Security

De-allocate memory that is no longer required.

4 tutorial

4.1 file reading and writing (a system call)

Write an application that will generate a list of numbers and write it to a file. Use from stdlib H's rand() function to do this.
Write: fopen - > fprintf() - > Fclose
Read: fopen - > fgets - > Fclose

fopen, fwrite, fgets, fread, fputs,fclose,

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>

int main(int argc, char** argv) {
	
	FILE* output_file = fopen("random.numbers", "w");
	long n_numbers = 0;
	if(output_file) {
		if(argc >= 2) {
			n_numbers = strtol(argv[1], NULL, 10);
			if(errno == 0) { //No error
				srand(time(NULL));
				for(long i = 0; i < n_numbers; i++) {
					fprintf(output_file, "%ld\n", 
						rand());
				}
			}
		}
		fclose(output_file);
	}
	return 0;
}

Read:

int main() {
FILE* f = fopen("numbers.txt", "r");
char buf[512];
while(fgets(buf, 512, f)) {
printf("%s", buf);
}
fclose(f);
}

4.2 command line parameters

Construct a program that accepts three command line parameters. The first parameter specifies whether the program will output lines from the beginning or end of the file. The second parameter specifies the number of rows it will output. The last parameter is the file name from which you will open and read. You can use the following basisSolve to read only the first n linesRewrite function without using your solution, instead of creating a function to read the characters and check the new lineChange logic to end the reading from fileNote: do not use variable length arrays.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINE_LEN (128)

void read_head(FILE* f, long n_lines) {
	char buf[LINE_LEN];	
	while(n_lines > 0) {
		if(!fgets(buf, LINE_LEN, f)) {
			break;
		}
		int length = strlen(buf);
		if(buf[length-1] == '\n') {
			n_lines--;
		}
		printf("%s", buf);
	}
}

void read_tail(FILE* f, long n_lines) {
	long seen_lines = 0;
	char buf[LINE_LEN];
	
	while(fgets(buf, LINE_LEN, f)) {
		int length = strlen(buf);
		if(buf[length-1] == '\n') {
			seen_lines++;
		}
	}

	rewind(f);
	seen_lines = seen_lines - n_lines;	
	while(n_lines > 0) {
		if(!fgets(buf, LINE_LEN, f)) {
			break;
		}
		
		int length = strlen(buf);
		if(buf[length-1] == '\n') {
			if(seen_lines > 0) {
				seen_lines--;
			}
			if(seen_lines <= 0) {
				n_lines--;
			}
		}
		if(seen_lines <= 0) {
			printf("%s", buf);
		}
	}
}

int main(int argc, char** argv) {

	long n_lines = 0;
	FILE* f = stdin;
	if(argc == 4) { f = fopen(argv[3], "r"); }

	if(strcmp(argv[1], "-h") == 0) {
		n_lines = strtol(argv[2], NULL, 10);
		read_head(f, n_lines);
	} else if(strcmp(argv[1], "-t") == 0) {
		n_lines = strtol(argv[2], NULL, 10);
		read_tail(f, n_lines);		
	} else {
		fputs("Missing -h or -t flag", stderr);
	}
	fclose(f);
	
	return 0;
}

4.3 wc instructions for linux

Your task is to recreate the wc unix tool. This will require accepting a file and counting the characters, words, and lines it contains. You can assume that each character is ASCII encoded.
You can think of words as any string between spaces. One line escape character sequence is \ n. Your application should have an output format similar to wo/ wc words. txt1068 356 100 build some test cases to ensure that your logic is correct and calculate the correct characters, words, and lines.
Total characters,
Number of words
Number of rows
fgetc

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>


void do_wc(FILE* f, const char* name) {
    
    int words = 0;
    int lines = 0;
    int bytes = 0;
    char c = 0;

    
    if(f != NULL) {
        
        int word_ev = 0;
        while((c = fgetc(f)) != EOF) {
            if(c == '\n') {
                lines++;
                if(word_ev > 0) {
                    words++;
                    word_ev = 0;
                }
            } else if(c == ' ') {
                if(word_ev > 0) {
                    words++;
                    word_ev = 0;
                }
            } else {
                word_ev++;
            }
            bytes++;
        }
        if(word_ev > 0) {
            words++;
        }
        printf("%d\t%d\t%d\t%s\n", lines, words, bytes, name);
    }
}

int main(int argc, char** argv) {
    if (argc > 1) {
        for(int i = 1; i < argc; i++) {
            FILE* f = fopen(argv[i], "r");
            if(f != NULL) {
                do_wc(f, argv[i]);
            }
            if(f) { fclose(f); }
        }
    } else {
        if(stdin != NULL) {
            do_wc(stdin, "stdin");
        }
    }
}

Keywords: C C++

Added by genericnumber1 on Sun, 19 Dec 2021 09:25:20 +0200