从函数返回本地 CassStatement 指针
Returning local CassStatement pointer from function
我正在使用 Cassandra 的 C++ 驱动程序,我对我在我的程序中使用的一些函数有疑问,其中我有这样的函数:
CassStatement* func()
{
/* Code */
CassStatement* l_stmt= cass_prepared_bind(cass_future_get_prepared(l_future));
//l_future is of type CassFuture* used in the 'Code' section
return(l_stmt);
}
由于 l_stmt 是局部变量,我有点困惑,想确保我没有在这里进行任何非法内存访问。例如在我的主程序中,如果我正在做
CassStatement* x=func();
那,可以吗?我希望它没问题,因为我认为虽然 func 中的 l_stmt 是一个局部变量(在堆栈上),但它指向的地址将在堆上(由 cass_prepared_bind() 返回)因此这应该没问题!
I hope it's fine because I think that although l_stmt inside func is a local variable(on stack), but the address to which it is pointing will be on heap(returned by cass_prepared_bind()) and hence this should be fine!
这是正确的。根据 documentation:
[cass_prepared_bind
] Returns a bound statement that must be freed.
因此您必须使用 cass_prepared_free
.
自行释放语句
我正在使用 Cassandra 的 C++ 驱动程序,我对我在我的程序中使用的一些函数有疑问,其中我有这样的函数:
CassStatement* func()
{
/* Code */
CassStatement* l_stmt= cass_prepared_bind(cass_future_get_prepared(l_future));
//l_future is of type CassFuture* used in the 'Code' section
return(l_stmt);
}
由于 l_stmt 是局部变量,我有点困惑,想确保我没有在这里进行任何非法内存访问。例如在我的主程序中,如果我正在做
CassStatement* x=func();
那,可以吗?我希望它没问题,因为我认为虽然 func 中的 l_stmt 是一个局部变量(在堆栈上),但它指向的地址将在堆上(由 cass_prepared_bind() 返回)因此这应该没问题!
I hope it's fine because I think that although l_stmt inside func is a local variable(on stack), but the address to which it is pointing will be on heap(returned by cass_prepared_bind()) and hence this should be fine!
这是正确的。根据 documentation:
[
cass_prepared_bind
] Returns a bound statement that must be freed.
因此您必须使用 cass_prepared_free
.