值内存泄漏的函数调用。

时间:2021-06-08 20:46:10

As far as I know, call a function in C or C++ is call by value, meaning while calling functions compiler makes a copy of all parameters and then passes them to the function body.

据我所知,在C或c++中调用一个函数是按值调用的,这意味着调用函数编译器会生成所有参数的副本,然后将它们传递给函数体。

Hence, is that possible to face with memory leak due to many parameter copying and function calls?

因此,由于许多参数复制和函数调用,可能会出现内存泄漏吗?

2 个解决方案

#1


2  

Q: Is that possible to face with memory leak due to many parameter copying and function calls?

问:由于许多参数复制和函数调用,可能会出现内存泄漏吗?

A: Directly, No. Indirectly, yes.

答:直接,不。间接的,是的。

Say you have two structs.

假设你有两个结构。

struct A
{
   A(int i) : data(new int(i)) {}
   A(const& copy) : data(new int(*copy.data)) {}

   // Forgot to add a destructor that deallocates
   // memory.

   int* data;
};

struct B
{
   B(int i) : data(i) {}
   int data;
};

When you create and delete instances of A, you are leaking memory. B does not suffer from that problem.

当您创建和删除A的实例时,您正在泄漏内存。B不受那个问题的困扰。

If you pass around instances of A to functions, you will create and delete objects, which will lead to memory leaks. That won't be a problem if you pass around instances of B.

如果您将A的实例传递给函数,您将创建和删除对象,这将导致内存泄漏。如果你传递B的实例,那就不会有问题了。

The point I wanted to make was that passing objects to functions and making copies while doing that does not by itself cause memory leaks. Only when there are memory leaks associated with object construction and deletion will you see the memory leaks while passing them around in functions.

我想要说明的一点是,将对象传递给函数并在执行时复制,这本身不会导致内存泄漏。只有当存在与对象构造和删除相关的内存泄漏时,您才会看到内存泄漏,并在函数中传递它们。

#2


0  

for c, the max stack size is limited,so you can do this make things bad.

对于c,最大的堆栈大小是有限的,所以您可以这样做使事情变得糟糕。

#define MAX_STACK_SIZE 0x10000000

struct test_s {
    char test[MAX_STACK_SIZE];
}

void func(struct test_s instance){
    //dothings
}

but, no one does, instand func as this

但是,没有人会这样做。

void func (struct test_s *instance_ref){
    //dothings
}

#1


2  

Q: Is that possible to face with memory leak due to many parameter copying and function calls?

问:由于许多参数复制和函数调用,可能会出现内存泄漏吗?

A: Directly, No. Indirectly, yes.

答:直接,不。间接的,是的。

Say you have two structs.

假设你有两个结构。

struct A
{
   A(int i) : data(new int(i)) {}
   A(const& copy) : data(new int(*copy.data)) {}

   // Forgot to add a destructor that deallocates
   // memory.

   int* data;
};

struct B
{
   B(int i) : data(i) {}
   int data;
};

When you create and delete instances of A, you are leaking memory. B does not suffer from that problem.

当您创建和删除A的实例时,您正在泄漏内存。B不受那个问题的困扰。

If you pass around instances of A to functions, you will create and delete objects, which will lead to memory leaks. That won't be a problem if you pass around instances of B.

如果您将A的实例传递给函数,您将创建和删除对象,这将导致内存泄漏。如果你传递B的实例,那就不会有问题了。

The point I wanted to make was that passing objects to functions and making copies while doing that does not by itself cause memory leaks. Only when there are memory leaks associated with object construction and deletion will you see the memory leaks while passing them around in functions.

我想要说明的一点是,将对象传递给函数并在执行时复制,这本身不会导致内存泄漏。只有当存在与对象构造和删除相关的内存泄漏时,您才会看到内存泄漏,并在函数中传递它们。

#2


0  

for c, the max stack size is limited,so you can do this make things bad.

对于c,最大的堆栈大小是有限的,所以您可以这样做使事情变得糟糕。

#define MAX_STACK_SIZE 0x10000000

struct test_s {
    char test[MAX_STACK_SIZE];
}

void func(struct test_s instance){
    //dothings
}

but, no one does, instand func as this

但是,没有人会这样做。

void func (struct test_s *instance_ref){
    //dothings
}