C++ C++;如何将未初始化的指针传递给函数
现在:编译器警告我在设置变量“net”的值之前使用了它。确实是这样,因为我没有足够的信息。它也会在运行时弹出,我只是忽略它。 我应该如何重新编写代码以使其更加优雅?C++ C++;如何将未初始化的指针传递给函数,c++,arrays,pointers,C++,Arrays,Pointers,现在:编译器警告我在设置变量“net”的值之前使用了它。确实是这样,因为我没有足够的信息。它也会在运行时弹出,我只是忽略它。 我应该如何重新编写代码以使其更加优雅? (顺便说一句,它必须是一个数组,而不是一个向量;然后我将它复制到CUDA设备上)。您可以在函数参数中使用双指针,并在函数中传递指针地址 // I need to download data from the (json-format) file net_f: std::ifstream net_f("filename", std::
(顺便说一句,它必须是一个数组,而不是一个向量;然后我将它复制到CUDA设备上)。您可以在函数参数中使用双指针,并在函数中传递指针地址
// I need to download data from the (json-format) file net_f:
std::ifstream net_f("filename", std::ios::in | std::ios::binary);
// to a square int array *net of size n:
int n;
int * net;
load_net(net_f, &n, net);
// The size is initially unknown, so I want to do it in the procedure:
void load_net(std::ifstream& f, int *n, int *net)
{
int size; // # of rows (or columns, it's square) in the array
int net_size; // the array size in bytes
/*
some code here to process data from file
*/
// Returning values:
*n = size;
// Only now I am able to allocate memory:
*net = (int *)malloc(net_size);
/*
and do more code to set values
*/
}
您可以在函数参数中使用双指针,并在函数中传递指针地址
// I need to download data from the (json-format) file net_f:
std::ifstream net_f("filename", std::ios::in | std::ios::binary);
// to a square int array *net of size n:
int n;
int * net;
load_net(net_f, &n, net);
// The size is initially unknown, so I want to do it in the procedure:
void load_net(std::ifstream& f, int *n, int *net)
{
int size; // # of rows (or columns, it's square) in the array
int net_size; // the array size in bytes
/*
some code here to process data from file
*/
// Returning values:
*n = size;
// Only now I am able to allocate memory:
*net = (int *)malloc(net_size);
/*
and do more code to set values
*/
}
由于您试图在被调用函数中修改
net
,因此需要传递net
(因为您使用的是C++)。此外,这也是n
的首选:
// I need to download data from the (json-format) file net_f:
std::ifstream net_f("filename", std::ios::in | std::ios::binary);
// to a square int array *net of size n:
int n;
int *net;
load_net(net_f, &n, &net);
// The size is initially unknown, so I want to do it in the procedure:
void load_net(std::ifstream& f, int *n, int **net)
{
int size; // # of rows (or columns, it's square) in the array
int net_size; // the array size in bytes
/*
some code here to process data from file
*/
// Returning values:
*n = size;
// Only now I am able to allocate memory:
**net = (int *)malloc(net_size);
/*
and do more code to set values
*/
}
C方式是传递一个双指针(而不是强制转换malloc的结果)!:
<>你似乎在编写C和C++代码的混合体。不要这样做。选择一个,并按预期使用其功能。因为您试图在调用的函数中修改
net
,所以需要通过net
(因为您使用的是C++)。此外,这也是n
的首选:
// I need to download data from the (json-format) file net_f:
std::ifstream net_f("filename", std::ios::in | std::ios::binary);
// to a square int array *net of size n:
int n;
int *net;
load_net(net_f, &n, &net);
// The size is initially unknown, so I want to do it in the procedure:
void load_net(std::ifstream& f, int *n, int **net)
{
int size; // # of rows (or columns, it's square) in the array
int net_size; // the array size in bytes
/*
some code here to process data from file
*/
// Returning values:
*n = size;
// Only now I am able to allocate memory:
**net = (int *)malloc(net_size);
/*
and do more code to set values
*/
}
C方式是传递一个双指针(而不是强制转换malloc的结果)!:
<>你似乎在编写C和C++代码的混合体。不要这样做。选择一个,并按预期使用其功能。这是什么意思:
*net=(int*)malloc(net\u size)
假设RHS表达式被强制转换为int*
,而LHS表达式是int
?这意味着什么:*net=(int*)malloc(net\u size)
假设RHS表达式被强制转换为int*
,而LHS表达式是int
?OP实际上是在试图修改net
(以及n
)所指向的内容。由于它还没有被初始化…@juanchopanza这就是他的代码所做的,但我相当确信这不是他想要的。“其他任何方式都没有意义。”胡安科潘扎看了看他的代码。他正在malloc
调用一个缓冲区,并试图将其分配给net
,这是调用函数中的int*
。显然,他想把缓冲区分配给指针,只是他没有使用足够的间接寻址。@JonathonReinhart,你说对了。我正在使用现成的示例编写代码,显然我还需要仔细考虑。@JonathonReinhart,谢谢你的帮助。这正是我所需要的。现在代码运行得非常好。:)学分归你!OP实际上是在试图修改net
(以及n
)所指向的东西。由于它还没有被初始化…@juanchopanza这就是他的代码所做的,但我相当确信这不是他想要的。“其他任何方式都没有意义。”胡安科潘扎看了看他的代码。他正在malloc
调用一个缓冲区,并试图将其分配给net
,这是调用函数中的int*
。显然,他想把缓冲区分配给指针,只是他没有使用足够的间接寻址。@JonathonReinhart,你说对了。我正在使用现成的示例编写代码,显然我还需要仔细考虑。@JonathonReinhart,谢谢你的帮助。这正是我所需要的。现在代码运行得非常好。:)学分归你!