简体   繁体   English

为什么我的程序不显示每个 output? 2次迭代后停止

[英]Why isn't my program displaying every output? It stops after 2 iterations

An application to read atleast 3 or more command line arguments, where every argument consists of servername and port number (0-1023) pair separated by a colon.读取至少 3 个或更多命令行 arguments 的应用程序,其中每个参数由服务器名和端口号 (0-1023) 对组成,用冒号分隔。 You need to parse, extract and display all server names and port numbers.您需要解析、提取和显示所有服务器名称和端口号。

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX_RANGE 1023
    #define MAX_LENGTH 30
    #define DELIMITER ":"
    
/*
Validate every word received. It should contain a Server name and a port number in range 0-1023  Use isalpha(), isdigit() to validate 
server name and port numbers
 */

    int validateInputs(char *input){
        
        char *serverName = NULL;
        char *portNumber = NULL;
        char token[MAX_LENGTH];
        int portNo;
        int flag1 = 1;
        int flag2 = 1;
        int i;
        int serverLen;
        int portLen;
        
        strcpy(token,input);
        
        serverName = strtok(token, DELIMITER);
        portNumber = strtok(NULL, DELIMITER);
            
        serverLen = strlen(serverName);
        portLen = strlen(portNumber);
        
        for(i = 0; i < serverLen; i++){
            if(!isalpha(serverName[i])){
                flag1 = 0;
                break;
            }
        }
        
        for(i = 0; i < portLen; i++){
            if(!isdigit(portNumber[i])){
                flag2 = 0;
                break;
            }
        }
        
        if(flag1 && flag2){
            if(atoi(portNumber)>0 && atoi(portNumber)<=MAX_RANGE){
                return (EXIT_SUCCESS);
            }
        }
        return(EXIT_FAILURE);
    }

/*
function to receive a word, to extract and display server name and port number. Should store and return pointer to the server name
*/
    
    char* displayServerPort(char *input){
        
        char token[MAX_LENGTH];
        char *serverName = NULL;
        char *portNumber = NULL;
        char *name = NULL;
        int serverLen;
        
        strcpy(token,input);
        
        serverName = strtok(token, DELIMITER);
        portNumber = strtok(NULL, DELIMITER);
        
        printf("Server Name: %s\t Port No: %s\n", serverName, portNumber);
        
        serverLen = strlen(serverName);
        name = (char*) malloc (serverLen * sizeof(char));
        strcpy(name, serverName);
        
        return name;
    }
    
    int main(int argc, char *argv[]){
        
        int i, value;
        
        char *serverNames[MAX_LENGTH];
        char *name = NULL;
        char str[MAX_LENGTH];
        
        if(argc < 3){
            printf("Less number of arguments! Pass atleast 3 arguments");
            exit(EXIT_FAILURE);     
        }
        
        for(i = 1; i < argc; i++){
            
            value = validateInputs(argv[i]);
            if(value == 1){
                exit(EXIT_FAILURE);
            }
            name = displayServerPort(argv[i]);
            strcpy(serverNames[i],name);
        }
    
        return 0;   
    }

If i give the input as serverA-01 serverB-02 serverC-03 the display function is only working for the 1st two arguments(serverA and serverB).如果我将输入作为 serverA-01 serverB-02 serverC-03 显示 function 仅适用于第一个两个参数(serverA 和 serverB)。 When debugging I can see all the 3 outputs but when running the code only shows 1st two args.调试时我可以看到所有 3 个输出,但运行代码时只显示第一个和两个参数。

You have two problems with your code:您的代码有两个问题:

  1. In displayPortServer , you're not allocating enough space for name .displayPortServer中,您没有为name分配足够的空间。 strlen does not include the NUL terminator in the length, but strcpy automatically tacks one on at the end, writing beyond the size of you buffer by one, invoking undefined behavior . strlen的长度中包含 NUL 终止符,但strcpy会自动在末尾添加一个,写入超出缓冲区大小一,调用未定义的行为 You need to malloc strlen + 1 bytes so there's enough room for the terminator:您需要malloc strlen + 1个字节,以便为终结符留出足够的空间:
name = malloc(serverLen + 1);

A couple more points on this:关于这一点还有几点:

  1. strcpy(serverNames[i],name); is incorrect.是不正确的。 serverNames is defined as an array of char pointers. serverNames被定义为一个字符指针数组。 However, you never initialize those pointers, so they don't point to any valid memory, and writing data to some random memory address you don't own also invokes undefined behavior.但是,您永远不会初始化这些指针,因此它们不会指向任何有效的 memory,并且将数据写入一些您不拥有的随机 memory 地址也会调用未定义的行为。 The easiest fix here is simply assign name to that pointer, since you're malloc ing and returning that in displayPortServer :这里最简单的解决方法是简单地为该指针分配name ,因为您是malloc ing 并在displayPortServer中返回它:
name = displayServerPort(argv[i]);
serverNames[i] = name;

Here is a working demonstration , but as the warning says, you're not actually using serverNames .这是一个有效的演示,但正如警告所说,您实际上并没有使用serverNames You should also free all the allocated strings in serverNames when you're done, or make a note that you'll let the OS reclaim the memory when the process terminates.完成后,您还应该free serverNames中所有分配的字符串,或者记下您将让操作系统在进程终止时回收 memory。

Furthermore, there may be a problem with your test input.此外,您的测试输入可能存在问题。 Your code specifies that server names must have alpha characters only , so for a server name like "serverA-01", its invalidated as soon as it sees '-' , and your program will exit(EXIT_FAILURE) .您的代码指定服务器名称必须包含字母字符,因此对于像“serverA-01”这样的服务器名称,它在看到'-'时立即失效,并且您的程序将exit(EXIT_FAILURE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM