Azusa C language Learning notes linked list & dynamic memory & Files

Source: Internet
Author: User
Tags function definition rewind

Azusa C language Learning notes linked list & dynamic memory & Files

First, the definition:

A linked list is a kind of linear storage structure that is implemented by the order of pointers link in a non-contiguous physical storage.

Second, features:

A linked list consists of a series of nodes (each element in the list is called a node), which is dynamically generated at runtime (malloc), and each node consists of two parts:

Data fields that store data elements

The pointer field that stores the next node address

Such as:

  typedef struct STUDENT

 {

int num;

Char name[20];

struct student *next;

}stu;

Three, dynamic memory application

In actual programming, sometimes the required memory space depends on the actual input data, can not be predetermined, so the need to dynamically allocate memory space, while the no longer use of space recycling reuse.

If the memory of the linked list needs to be applied dynamically


1. static distribution and dynamic allocation

Static assignment

Allocates the allocation of memory space in a predetermined size, during program compilation or operation. such as: int a [10]

You must know the size of the space you need beforehand.

Allocated in a stack or global variable area, usually in the form of an array.

Allocated on a schedule.

Dynamic allocation

When the program is running, allocate the required space freely as needed.

On demand

Allocated in the heap area, typically using a specific function for allocation.

Typically, the malloc function void *malloc (unsigned int size) is used; Allocates a contiguous range of size bytes in the heap to hold the type specified by the type descriptor.

The function returns the void* pointer, which must be cast with the corresponding coercion type.

The allocated memory space content is indeterminate and is typically initialized with memset.

When you're done, remember to use the free () function to release memory

Return value: The starting address of the allocated space (Assignment succeeded)

NULL (allocation failed)

Attention

1, after calling malloc, be sure to determine whether to apply for memory success.

2. If more than one malloc application memory, the memory of the 1th and 2nd applications is not necessarily continuous

Cases:

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

int main ()

{

int count,*array,n;

printf ("Please enter the number of array elements you want to apply \ n");

scanf ("%d", &n);

array= (int *) malloc (n*sizeof (int));

if (array==null)

{

printf ("Application memory failed \ n");

return 0;

}

memset (array,0,n*sizeof (int));

for (count=0;count<n;count++)

{

Array[count]=count;

}

for (count=0;count<n;count++)

{

printf ("%d\n", Array[count]);

}


Free (array);//Releases the memory that the array points to


Return0

}

Free function (frees memory function)

Header files: #include <stdlib.h>

function definition: void free (void *ptr)

Function Description: The free function releases the memory that the PTR points to.

Cases:

Char *p= (char *) malloc (100);

Free (P);//

Attention

1, free, because the p is not assigned, so p still point to the original dynamic application of memory. But the memory is no longer available, p becomes a wild pointer.

2, a dynamic application of memory can only be free once, not multiple free


Iv. Documents

A file is a collection of data that is stored on disk.

1. Row buffer:

Standard IO library functions that are buffered when you export something to a standard output (screen)

row buffers flush buffers in only the following cases

1-line break "\ n" in buffer

2-buffer full, auto flush buffer

such as: while (1)

{

printf ("Hello World");

}

3-Manual Flush Buffer Fflush (STDOUT)

4-Program ends normally, flush buffer return 0;


2. Full buffer:

Standard IO library functions to read and write data to ordinary files are fully buffered,

Do not flush buffers when encountering newline characters

1. Buffer full, flush buffer

2. Manual Flush buffer fflush (file pointer)

3. Normal end of program flush buffer

3. No buffering:

When reading and writing files through the system call IO (read write), the file read and write data is unbuffered, that is, the write data will immediately enter the file, read data will immediately enter the memory


4. The process of writing the file:

Application space-(kernel space-(Driver---(HDD

Applications and kernel programs run in different spaces in order to protect the kernel.

Buffering allows you to reduce the number of incoming and outgoing cores to increase efficiency.


5. Common file Operation:

5.1 Open files: File *fopen (const char *path, const char *mode);

FILE *FP;

Fp=fopen ("./test.txt", "R");

5.2 closing files: int fclose (file *fp);

Cases:

#include <stdio.h>

int main ()

{

FILE *FP;

int ret;

Fp=fopen ("./test.txt", "r+");

if (fp==null)

{

Perror ("fopen");

return 0;

}

Fclose (FP);

return 0;

}


5.3 File location:

Rewind reset read/write location

Move the position pointer inside the file to the top of the file

Rewind (FP);


Ftell test file Read and write location how many bytes from the beginning of the file

int length; Length = Ftell (FP);


Fseek position pointer (read and write position)

fseek function (typically used for binary files that require a file to be opened with B)

int fseek (FILE *stream, long offset, int whence); int fseek (file type pointer, displacement amount, starting point);

Parameters:

whence start position

File Start Seek_set 0

File Current Location Seek_cur 1

End of File Seek_end 2

Offset Displacement:

At the starting point, the number of bytes to move forward and backward, positive to the end of the file offset, negative number toward the beginning of the file offset.

Cases:

Fseek (Fp,50,seek_set)

Fseek (Fp,-50,seek_end);

Fseek (Fp,0,seek_end);


Azusa C language Learning notes linked list & dynamic memory & Files

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.