首页 > 其他分享 >problem analysis - 1

problem analysis - 1

时间:2023-02-05 21:23:36浏览次数:55  
标签:string address analysis memory problem pointer array name

problem analysis

标签(空格分隔): CS:APP


Problem

An information table of students in terms of Id, Age, and their name of string type. Algorithm implementation seems to be simple, but a handful of detail makes me headaches such as memory allocation and the insidious confusion with pointer and array.

To cut to the chase, let's troubleshoot these problems.

1. mindmap

problem: record student information

-> data structure -> operation

dynamic array

insert in the way from the index at 0 to its upper index at one step

2. memory and symbol table

2.1 functions 1: Array of Variable length

Usually, the length of an array is prespecified before compilation, but the length of AVL is specified during execution thus even after linking.

The advantages are obvious to make implementation more flexible.

But, in my own opinion because I have not found any evidence to support it,

Due to these declarations of AVLs being assigned to static memory and belonging to static variables, which only is allocated into the main memory after execution, the addresses of AVLs are different from those prespecified with length.

==> whatever the length you specify is, the address of each element is identical.

Reason: this is because C is a static programming language, and it won't expand the address further.( symbol table plays an essential role in managing these names )

Solution : malloc() and free()

2.2 function 2 : assign array to pointer of char type

It's known as the string literal, which is a constant char array that is on behalf of the initial address of the string.

(One assumption: how does it work? by three permission bits on PATE? )

problem: why the pointer cannot interchange with the array name?

Solution: It has something to do with linking as well.

Due to the constant property of string literal, any array associative with it is stored in the static memory until it is executed later along the allocation of main memory. So when it is executed, **the content of string" is copied to its memory block.

In contrast to the array name, the pointer to this string only is assigned by the initial address of the string.

From this point forward, a point can be regarded as an address variable instead of a constant address which happens to array name.
=>
No modification operation is allowed to perform on the array name, but the pointer totally supports these modifications instead.

Example:

int array[3]={1,2,3};
int *pointer = array; // = &array[0]

*(array+1) OK!
*(pointer+1) OK!

++array FALSE!
++pointer OK!

标签:string,address,analysis,memory,problem,pointer,array,name
From: https://www.cnblogs.com/UQ-44636346/p/17093954.html

相关文章