Open Course Study Notes-[Harvard] Computer Science CS50 (5)

Source: Internet
Author: User

Lesson 1 memory, header files, and data structures-2011.10.30

For C language, memory allocation is very important, which involves program stability and security. It is risky to store sensitive data such as username and pw in the stack, because the content does not actually disappear when the function returns. If it is our own program, ensure that only our own code can access the memory, but if it is a cooperative or library method, there is a hidden risk of buffer overflow exploit. For example, if memcpy overwrites the return address of the function (stored in the stack), you can jump to the hacker code entry. Therefore, we should pay attention to the data Boundary Detection and do not allow cross-border processing. In addition, this type of data can be stored in heap mode and allocated and released independently (the stack is automatically implemented by the system ). In addition, we should avoid writing some sensitive values, such as pw, as initialized.
Data, the relative address and value remain unchanged, and there is a risk that it can be obtained through gdb or in some ways. This is not the case in java. All memory allocation and release are performed by the system.

There is a space allocation function realloc () that has never been used before, that is, re-allocation means that the memory size can be expanded without affecting the original data. Realloc actually calls malloc and copies the original content to free the original content.

Next we will introduce the header file, custom data type, and file read/write.

12th class memory, file storage, linked list-2011.10.31

The sscanf syntax is similar to that of printf, but it is not written but read from it. For example, sscanf (line, "% d % c", & n, & c); reads the corresponding content of line into n and c according to the format, and returns the read content, if line = "123", only % d to n, 1 data, 1 is returned. If line "123X", 2 is returned. Scanf reads data from keyborad.

This section describes arrays. The size of the array is limited in advance, and a fixed value can be allocated, which may lead to out-of-bounds. It can be solved with realloc, but the efficiency is low. Next we will introduce the single-chain table.

Typedef struct node {
Int n;
Struct node * next;
} T_node;

The file storage method is similar. You can add blocks to increase the storage space. With this, we can get or guess the address of the deleted file and restore it. For example, you can add a file to read the data stored in the previous address from the file. The details are related to the disk file architecture.

This section describes how to add or delete a single-chain table.

Related Links: My articles related to programming ideas

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.