我尝试释放由STL列表的指针项指向的内存.
这应该工作正常,但在我的情况下,列表中可能有重复的指针,我得到一个双dealloc异常,即使我测试指针是否为NULL(请参阅下面的源代码).我怎么解决这个问题 ?
list<Line *> * l = new list<Line *>;
Line * line = new Line(10, 10, 10, 10);
l->push_back(line);
l->push_back(line);
cout << "line addr " << line << endl;
for (list<Line *>::iterator it = l->begin(); it != l->end(); it++)
{
if (*it != NULL)
{
cout << "line it " << *it << " " << (*it)->toString() << endl;
delete (*it);
(*it) = NULL; …Run Code Online (Sandbox Code Playgroud) 我正在和朋友一起开发一个GUI库,我们遇到了如何确定某个元素是否可以点击(或者可移动等)的问题.
我们决定只检查特定对象是否存在函数,所有gui元素都存储在带有指向基类的指针的向量中.
所以,例如,如果我有
class Base {};
class Derived : public Base
{
void example() {}
}
vector<Base*> objects;
Run Code Online (Sandbox Code Playgroud)
如何检查对象成员是否具有名为example的函数.
如果这不可能实现可选行为(如点击和相似)的不同方式.
struct a
{
int (*ptr1)();
int (*ptr2)();
int data;
};
typedef struct
{
struct a x;
}all;
int fun1()
{
return 5;
};
int fun2()
{
return 9;
};
Run Code Online (Sandbox Code Playgroud)
我可以分配喜欢
all *mem = (all*)malloc(sizeof(all));
mem->x.ptr1 = fun1;
mem->x.ptr2 = fun2;
Run Code Online (Sandbox Code Playgroud)
有没有其他方法来分配这些函数指针?有可能像这样分配吗?
all *mem;
(void **)mem->ptr1[0] = fun1;
(void **)mem->ptr2[1] = fun2;
Run Code Online (Sandbox Code Playgroud) 我是新手 指针使我疯狂T_T.现在,我正在进行套接字编程项目.这是我的代码.
typedef struct {
char *ip[INET6_ADDRSTRLEN];
char *username[20];
time_t login_time;
enum client_state client_state;
int no_login;
} Client;
Client client[max_connections] = {}; // set to null
char remoteIP[INET6_ADDRSTRLEN];
.
.
.
.
.
if(client[new_fd-4] == NULL) { // if fist attempt, client always null
// I want to setting client[new_fd-4].ip = &remoteIP
// How to write the code ??
}
Run Code Online (Sandbox Code Playgroud) 可能的重复:
当C++中的pass-by-pointer更喜欢传递引用时?
在C++中通过引用传递指针是否有好处?
如何将对象传递给C++中的函数?
大家好,我正在努力开发一个大型面向对象的c ++应用程序框架,作为我的化学工程研究生研究的一部分.
我发现我的许多函数都指向自定义对象或STL对象.我发现在编写代码方面,这使得访问存储在其中的函数或变量变得更加困难.
但是,除了简单之外,通过引用传递指针是否有任何优点/缺点?
如果一个优于另一个优势,我可能会考虑重构我的代码以统一使用任何最佳方法.如果没有,我仍然可以重构为了可读性而始终使用引用传递(即不必取消引用)
我想再做出最好的决定,因为我的框架已经有40多个文件了,所以我想尽可能早地实现统一的结构,并且无论最佳方法是什么.
提前致谢!
gcc 4.4.4 c89
不知道为什么我收到此错误.
在我的头文件中,我有以下内容
handle.h
typedef struct Handle_t Handle
Run Code Online (Sandbox Code Playgroud)
在我的实现文件中
handle.c
struct Handle {
size_t id;
char *name;
};
Handle* create_handle(size_t id)
{
Handle *hdev = NULL;
hdev = malloc(sizeof(*hdev)); /* Error */
.
.
}
Run Code Online (Sandbox Code Playgroud)
非常感谢任何建议,
gcc 4.4.4 c89
我不断得到一个"不能解除对不完整类型的解释".
但是,我确信我的结构类型完整.我返回Network_t实例,它是指向已分配内存的指针.我应该能够解除那种记忆.
非常感谢任何建议,
我在头文件中有这个:driver.h
typedef struct Network_t Network_t;
Network_t* create_network(int id);
Run Code Online (Sandbox Code Playgroud)
实现文件driver.c
#include "driver.h"
struct Network_t {
int id;
};
Network_t* create_network(int id)
{
Network_t *network = malloc(sizeof *network);
if(network) {
network->id = id;
}
return network;
}
Run Code Online (Sandbox Code Playgroud)
在我的main.c
#include "driver.h"
Network_t *network = NULL;
network = create_network(1);
printf("Network ID: [ %d ]\n", network->id); /* Cannot dereference pointer to incomplete type */
Run Code Online (Sandbox Code Playgroud) 我有一个我似乎无法弄清楚的指针问题.看起来我已经用这种方式使用指针了1000次,所以我不太清楚这里发生了什么.我有以下代码:
int iRetVal;
CycleCountOrder* cycleOrder = NULL;
CycleCountLineItem* cycleLine = NULL;
iRetVal = m_CycleCount.GetCCOrderLine(pOneLocation.szOrderNum[c], cycleOrder, cycleLine);
Run Code Online (Sandbox Code Playgroud)
每当我调用GetCCOrderLine时,我都会进入函数内部,并为指针cycleOrder和cycleLine分配有效值.当我走出函数GetCCOrderLine之外时,引用再次为NULL.下面的代码是如何定义GetCCOrderLine:
头文件
int GetCCOrderLine(CString szOrderLnitem, CycleCountOrder* cycleOrder, CycleCountLineItem* cycleCountLine);
Run Code Online (Sandbox Code Playgroud)
cpp文件
int CCycleCount::GetCCOrderLine(CString szOrderLnitem, CycleCountOrder* cycleOrder, CycleCountLineItem* cycleCountLine)
{
CString szCurrOrderLnitem;
for(int c = 0; c < m_Orders.GetCount(); c++)
{
CycleCountOrder* currentOrder = m_Orders[c];
for(int d = 0; d < currentOrder->m_LineItems.GetCount(); d++)
{
CycleCountLineItem* currentLine = currentOrder->m_LineItems[d];
szCurrOrderLnitem.Format("%s-%d-%d", currentOrder->szOrderNum, currentLine->nLnitemNum, currentLine->nSubitemNum);
if(szCurrOrderLnitem == szOrderLnitem)
{
cycleOrder = currentOrder;
cycleCountLine = currentLine;
return FUNC_OK;
}
} …Run Code Online (Sandbox Code Playgroud) 在C++ for Windows中,我有一些对象工厂,它应该通过将指向对象的指针传递给Create函数并返回一个创建的对象来创建一系列Info对象.
void CreateInfoObject(AbstractInfo** info); // The creation function
Run Code Online (Sandbox Code Playgroud)
AbstractInfo是一个基类,我们有许多类型的Info对象派生.
我想我现在可以创建一个Info对象,如下所示:
MyInfoObject* InfoObj = NULL; // derived from AbstractInfo object
InfoFactory fc;
fc.CreateInfoObject(&InfoObj); // Now I want to get my initialized pointer back
Run Code Online (Sandbox Code Playgroud)
但它说不能做演员......有什么不对?
错误:无法从MyInfoObject**_ W64转换为AbstractInfo**
编辑:第一个答案提到界面很可怕,看不到谁在分配等等......我怎样才能改进?
我正在尝试学习C指针,但我无法理解某些东西......以下代码:
#include <stdio.h>
void foo(int *x, int *y);
void foo(int *x, int *y) {
printf("x = %p\ny = %p\n", &x, &y);
*x = 5;
*y = 6;
}
int main(void) {
int a, b;
printf("a = %p\nb = %p\n", &a, &b);
foo(&a, &b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么地址不同?第一个printf(主)输出两个地址.另一个printf(foo)输出不同的地址.我将地址传递给foo(&operator).