如何通过这个指针? [英] How to to this passing pointers?

查看:69
本文介绍了如何通过这个指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的结构(它是一个全局结构)

i had an structure like this(it is a global structure)

typedef struct MachineList
{
    BSTR AccountId;
    BSTR MachineId;
    
    char* Make;
    
    char* Model;
    BSTR SerialNumber;
    
    char* IpAddress;
    
    int Port;
    BSTR LocationCode;
    SOCKET Sock;
    char* Status;
} MACHINELIST,*PMACHINELIST;



通过解析XML填充此结构,并将其添加到STL列表中.

然后比较结构中每个条目的制造商和模型,我使用new运算符创建指向类的指针,并通过像这样传递结构来调用连接机器功能



This structure is filled by parsing an XML.and added to an STL list

Then comparing the make and model for each entry in the structure i create an pointer to a class using new operator and call the connect to machine function by passing the structure like this

while(iList!=SS1.end())
{
    MACHINELIST* pList=*iList;
    
    
    if((pList->Status=NO_CONNECTION)||(pList->Status=NOT_ACTIVE))
    {
        if((strcmp(pList->Make1,MAKE)==0)&&(strcmp(pList->Model1,MODEL)==0))
        {
            CConnector *p_CDiamax=new CConnector();
            pList->Sock=p_CDiamax->ConnectToMachine(*pList);
            printf("ip address is %s\n",pList->IpAddress);
            printf(" status is %ls\n",pList->Status);
            
            if(pList->Sock!=NULL)
            {
                pList->Status="Active";
                
            
            }
            printf(" status is %ls\n",pList->Status);
            printf("ip address is %s\n",pList->IpAddress);
        }
    }
    iList++;
}



我想一一连接每台机器,然后传递为我连接的机器创建的指针,以调用sendRequest,recvResponse
像这样



I want to connect each machine one by one and then pass the pointer created for the machine to which i have connected to call the sendRequest,recvResponse
like this

while(1)
{
    for(int i=0;i<machinecount;i++)
    {
        //make all connection as shown above
    }
    for(int i=0;i<machinecount;i++)
    {
        //pointer created in connect function use that to send request,recv response
    }
    Sleep(20000);
}


现在我要


now i want to

推荐答案

您可能想用Google搜索std::for_each.

希望这会有所帮助,
Pablo.
You may want to google std::for_each.

Hope this helps,
Pablo.


if((pList->Status=NO_CONNECTION)||(pList->Status=NOT_ACTIVE))


这条线将无法满足您的期望.

在connect函数中创建的指针在哪里,以及您打算将其用于什么?


This line is not going to do what you expect.

Where is the pointer that is created in the connect function, and what do you want to use it for?


有几件事立即跳向我.

首先是您已修复的if语句中的一对赋值.我接下来看到的是这段代码:
There''s a couple of things that leap out at me immediately.

Firstly was the pair of assignments inside an if statement that you''ve since fixed. Next thing I saw was this section of code:
while(iList!=SS1.end())
{
    MACHINELIST* pList=*iList;


    if((pList->Status=NO_CONNECTION)||(pList->Status=NOT_ACTIVE))
    {
        if((strcmp(pList->Make1,MAKE)==0)&&(strcmp(pList->Model1,MODEL)==0))
        {
            CConnector *p_CDiamax=new CConnector();


我假设最后一行是您进行连接的对象,也是您稍后要在send/recv循环中使用的对象.如果是这样,我想知道两件事-(1)您是否不需要将其保存到(a)与它所属的机器相同索引的临时列表,或者(b)更好,在pList指向的机器内部?
(2)就是全部吗-您现在需要指定端口号和url还是稍后在循环中进行?

最后,我在while(1)循环中看到您做了两件事-(1)连接到所有机器(2)从所有机器发送/接收一些数据.就目前而言,这种方法存在内存泄漏.每次运行我引用的代码时,都会为每台计算机创建一个 new 连接器.如果每台计算机需要一个连接器,则只需将成员添加到MachineList结构中,每台计算机将其初始化一次.另一方面,如果您只是将其用作中介-一种用于获取发送/接收套接字的工具,则可以在循环外的堆栈上创建一个套接字,并每次通过循环从每台计算机上从中提取所需的信息.环形.它在堆栈中,因此它将在func结束时将其自身删除.我真的不知道-我对CConnector并不熟悉,似乎也缺少代码,而不是无法产生预期的结果.真的不能再说了.


I''m assuming the last line is the one in which you make the connection and the object you''d like to use later in the send/recv loop. If thats''s the case, I wonder two things - (1) wouldn''t you need to save this to either (a) a temporary list at the same index as the machine it belongs to or (b) better yet, inside the machine pointed to by pList?
(2) Is that all there is to it - do you need to specify a port number and url now or do you do that in your loop later on?

Lastly, I see in the while(1) loop that you do two things - (1) connect to all of the machines (2) send/receive some data from them all. As it stands, that approach has a memory leak. Every time you run though the code I quoted, you''re creating a new connector for each machine. If you need a connector per machine, then just add the member to the MachineList struct, initialize it once each per machine. If, one the other-hand, you just use it as an intermediary - a tool to get a socket for send/recv, then create one on the stack outside the loop and extract the needed information from it for each machine each time through the loop. It''s on the stack, so it will delete itself when the func ends. I really don''t know - I''m not familiar with CConnector, also there appears to be code missing, as opposed to not producing the expected results. Really can''t say further.


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

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