C/C++ Pointer Learning ||

C/C++指针与内存管理
本文深入探讨了C/C++中指针的概念及其重要性,并解释了如何使用指针来有效地管理内存资源。文章对比了传值调用与传引用调用的区别,详细介绍了如何通过传递地址来修改原始变量的值,以及如何使用指针进行动态内存分配。

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;
}

false call

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);
*/
【路径规划】(螺旋)基于A星全覆盖路径规划研究(Matlab代码实现)内容概要:本文围绕“基于A星算法的全覆盖路径规划”展开研究,重点介绍了一种结合螺旋搜索策略的A星算法在栅格地图中的路径规划实现方法,并提供了完整的Matlab代码实现。该方法旨在解决移动机器人或无人机在未知或部分已知环境中实现高效、无遗漏的区域全覆盖路径规划问题。文中详细阐述了A星算法的基本原理、启发式函数设计、开放集与关闭集管理机制,并融合螺旋遍历策略以提升初始探索效率,确保覆盖完整性。同时,文档提及该研究属于一系列路径规划技术的一部分,涵盖多种智能优化算法与其他路径规划方法的融合应用。; 适合人群:具备一定Matlab编程基础,从事机器人、自动化、智能控制及相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于服务机器人、农业无人机、扫地机器人等需要完成区域全覆盖任务的设备路径设计;②用于学习和理解A星算法在实际路径规划中的扩展应用,特别是如何结合特定搜索策略(如螺旋)提升算法性能;③作为科研复现与算法对比实验的基础代码参考。; 阅读建议:建议结合Matlab代码逐段理解算法实现细节,重点关注A星算法与螺旋策略的切换逻辑与条件判断,并可通过修改地图环境、障碍物分布等方式进行仿真实验,进一步掌握算法适应性与优化方向。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值