Learned C like 4 years ago on Tan Haoqiang’s weird silly book.
Now I need to have a recap of this and hopefully understand better in C/C++. I don’t like C/C++ honestly, but I have to understand it better to deal with my coursework in COMP6771&6733.
What is a pointer and is pointer a really special component of C/C++? Some times I get confused and thinking pointer in C/C++ is literally a “pointer”. But the truth is, the pointer is just a type of variable like other types such as int, char, …
In C, there is only one way of passing variables to function: call by value; And what “call by value” does is to make copies value of parameters to formal parameters. Changes to the formal param has no effect to outside!
In many cases, we wish to have this outside effect, so what do we do? we copy(pass) not the value of a var but the address as formal param so we can access the original value!
like this:
int foo(int x) {
}
foo(A);
vs
int foo(int* ptr){
…
}
foo(&A);
Assume you what a function to be able to allocate certain length of memory resource e.g and int array. call int * p = (int *) malloc(n*sizeof(int)), but if you do it inside a function?
void allocation(char ** p, int len)
{
*p = (char*) malloc(sizeof(char)*(len+1));
}
int main ()
{
int i,n;
char * buffer;
allocation(&buffer,i);
//do else...
return 0;
}
Compare the false .
Why use char * p in function declaration? why not simply *p? Recall the definition of call by value, suppose we have a machine, when we create char buffer(ptr), it has a address 100 and the value inside for now is NULL. If we pass ptr instead of &buffer, what happens is that the program creates a copy of ptr called A with type: (int *, value null). After the termination of allocate(), where is that? its gone! so when you check the false calls in debug, the ptr is going to be null.
The correct way however is in the code section, you first create a char * buffer, assuming its address is 100. Then you pass &buffer as param in allocate() call, the program will copy a int** type using &buffer’s value which is 100. In malloc sentence, the malloc returns a (char *) and this will be put in 100 because this is where p is pointing to. What’s p’s address? it is definitely not 100 but we don’t really care.
We also have a way to create a matrix:
/*
Write a function matrixAllocate that takes two integers, m
and n and allocate an m by n block of memory.
*/
void matrixAllocate(int *** matrix, int c, int r)
{
*matrix = (int **) malloc(r*(sizeof(int *)));
//we allocate row and pass the row0 (also r=0 c=0)
//to *matrix. each row is pointed by an int*
int i = 0;
for (;i<c;i++)
{
*((*matrix)+i) = (int *) malloc(r*(sizeof(int)));
}
// we create col here
}
C++ call by reference
In C++, we also have a way to call by reference, note that the default is still call by value.
Look at the this function, void swap(int &x, int &y) is very much different than void swap(int *x, int *y) with call (swap(&a, &b) where a,b are int )but achieves the same result.
void swap(int &x, int &y)
{
int temp;
temp = x; /* save the value at address x */
x = y; /* put y into x */
y = temp; /* put x into y */
return;
}
/*
int a = 100;
int b = 200;
call
swap(a, b);
*/
C/C++指针与内存管理
本文深入探讨了C/C++中指针的概念及其重要性,并解释了如何使用指针来有效地管理内存资源。文章对比了传值调用与传引用调用的区别,详细介绍了如何通过传递地址来修改原始变量的值,以及如何使用指针进行动态内存分配。
3307

被折叠的 条评论
为什么被折叠?



