指针帮助C / C ++ [英] Pointer help in C/C++

查看:160
本文介绍了指针帮助C / C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道C和C ++中的指针 - 它如何帮助节省内存?我搜索,但没有得到满意的答案。请帮助我。

I want to know about the pointer in C and C++ - how does it help in saving memory? I searched but did not get a satisfactory answer. Please help me out.

推荐答案

如果比较以下两段代码:

If you compare the following two pieces of code:

foo() {
    large_struct x;
    bar(x);
}
bar(large_struct x) {
    //do some funny things
}

foo() {
    large_struct* x;
    bar(x);
}
bar(large_struct* x) {
    //do some funny things
}

在第一部分中,大型结构x被复制到内存中,而在第二个代码片段中只是一个指针被复制到内存中。这是要点,当你可以用指针保存内存。

In the first piece, the large struct x is copied in memory, while in the second snippet just a pointer is copied in memory. That are the points, when you can save memory with pointers.

这篇关于指针帮助C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆