变量由于某种原因正在改变 [英] Variable is being changed for some reason

查看:163
本文介绍了变量由于某种原因正在改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此信息后续追踪
致电'userConnect() '方法跳过

更正我的最后一个帖子,它实际上是输入userConnect()方法,但是一些如何更改SETUP_ERROR标志。正如你可以看到在我的userConnect()的地方我改变了SETUP_ERROR,我清楚地打印了什么错误可能是。

Correction to my last post, It is actually entering userConnect() method but some how SETUP_ERROR flag is being changed. As you can see in my userConnect() the places I changed SETUP_ERROR, I clearly printed what the error could be.

现在使用'getMyIPAddress()'方法中的'cout<<'in,SETUP_ERROR为0,并进入userConnect当我删除它,不知何故SETUP_ERROR正在更改为1,并打印SETUP_ERROR。

Now with the 'cout <<' in 'getMyIPAddress()' method, the SETUP_ERROR is 0 and enters into IF loop in userConnect() and works perfect. When I remove it, Somehow SETUP_ERROR is being changed to 1 and printing "SETUP_ERROR".

无法弄清为什么SETUP_ERROR被更改为1,我知道'cout'与这无关,但它是什么。打败我。

Cannot figure out why SETUP_ERROR being changed to 1, I know 'cout' has nothing to do with this but what could it be. Beats me.

getMyIPAdress():

getMyIPAdress():

void getMyIPAddress (char* command, char* port) {
    struct hostent *he;
    struct in_addr ipv4addr;
    char dnsIP[] = "8.8.8.8";                                   // Google DNS IP
    char dnsPort[] = "53" ;                                     // Google DNS TCP Port
    // Converting IP to struct in_addr type     
    inet_pton(AF_INET, dnsIP, &ipv4addr);
    // getting host details from IP address
    he = gethostbyaddr(&ipv4addr, sizeof ipv4addr, AF_INET);
    cout << "bazinga!" << endl;
    // Connecting to google DNS to get IP address of this process
    userConnect (he->h_name, dnsPort, port, command);
}

userConnect():

userConnect():

void userConnect ( char* sIP, char* sPort, string my_port, char* command )
{
    int numbytes;
    char buf[MAXDATASIZE];
    char rbuf[MAXDATASIZE];
    int rv;
    char s[INET6_ADDRSTRLEN];
    struct addrinfo *servinfo,*p,hints;
    int sockfd;
    int SETUP_ERROR;
    memset(&hints, 0, sizeof hints);
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    // Getting address related information 
    // flagging invalid ip addresses 
    if ((rv = getaddrinfo(sIP, sPort, &hints, &servinfo)) != 0) {
        cout << "getaddrinfo: " << endl << gai_strerror(rv) << endl;
        cout << " Invalid IP Address! " << endl;
        SETUP_ERROR = 1;
    }else
    {
        int count;
        for(p = servinfo; p != NULL; p = p->ai_next) {
            if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
                perror("client: socket");
                continue;
            }

            if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
                close(sockfd);
                perror("client: connect");
                continue;
            }

            break;
        }
        // flagging invalid port numbers
        if (p == NULL) {
            cout << stderr << "client: failed to connect" << endl;
            cout  << "Invalid Port Number! " << endl;
            SETUP_ERROR = 1;
        }
    }
    if (SETUP_ERROR != 1) {

        inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), s, sizeof s);       // gettin printable ip
        freeaddrinfo(servinfo);                                                                 // don't need anymore
        int len;
        // Functionalities based on input commands
        if (strcmp(command,"REGISTER")==0) {
            // client adding server as default first connection
            conn_head = insert(conn_head , server_IP, sPort, sockfd-OFFSET );
            servfd = sockfd;
            len = my_port.length();
            // sending client's listening port number to server 
            if (send (sockfd, &len, sizeof( len ), 0) == -1)
                perror("send packet length");
            if (send(sockfd, my_port.c_str(), len, 0) == -1)
                perror("send");
            // adding socket fd to master list 
            addFdToMaster(sockfd);
        }else if(strcmp(command, "MYIP")==0) {
            /* Next 10 line code snippet is taken from a blog online! Cant find it anymore to post reference ! :) */
            //get local socket info::
            struct sockaddr_in local_addr;
                socklen_t addr_len = sizeof(local_addr);
            if (getsockname(sockfd, (struct sockaddr*)&local_addr, &addr_len) < 0) {
                perror("getsockname");
            }
            /* get peer ip addr */
            char my_ip[INET_ADDRSTRLEN];
            if (inet_ntop(local_addr.sin_family, &(local_addr.sin_addr), myip, sizeof(myip)) == NULL) {
                perror("inet_ntop");
            }
            else
                cout << "HOST IP Address:: " << myip << endl;
        }
    }else
        cout << "SETUP_ERROR" << endl;
}


推荐答案

$ c> SETUP_ERROR ,也不要将其设置为 0 。测试 if(SETUP_ERROR!= 1)正在测试一个未初始化的变量,在你似乎期望它 0

You never initialize SETUP_ERROR, nor set it to 0. The test if (SETUP_ERROR != 1) is testing an uninitialized variable, in the cases where you seem to be expecting it to be 0.

为了解释你看到的是什么,你的编译器把 SETUP_ERROR 并且因为没有初始化,它只是假设一个值从堆栈生长的内存位置的任何字节。这些可能会受到您在其他功能中执行的操作的影响。

To explain what you're seeing, your compiler is placing SETUP_ERROR on the stack, and since there's no initialization, it is just assuming a value from whatever bytes are in the memory location that the stack grows into. These could be affected by what you were doing in the other function.

这篇关于变量由于某种原因正在改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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