简体   繁体   中英

Pointer to a pointer of a character pointer, or why can't I reassign this array?

I'm having a lot of trouble performing a very basic task: resizing an array. Every intro to programming class I've ever taken taught me to do this by creating a larger array, filling it, and then point the original array to the new (larger) one.

The program below tokenizes a string into a program name and its argv[] (it's ultimately going to be a basic shell implementation). It allocates space for 8 arguments at a time -- if there are more than 8 then it recursively allocates a larger array and fills it.

Everything is working well (please let me know otherwise!) except I can't point the args array to the moreArgs array. I have a statement that should do this at the end of the getArgs function but it simply is not reassigning the address of args[]. What am I doing wrong?

#define debug 1

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

char ** getArgs( char *input,  char **args, int ct);

/*Is there a better way than making these global?*/
char ** args;
char **moreArgs;

int main(int argc, char* argv[]) {
  char input[]="echo arg1 arg2 arg3 arg4 arg5 arg6 arg7 arg8 arg9 arg10";
  char inputcpy[strlen(input)];
  strcpy(inputcpy, input);
  char * temp;
  temp=strtok(input, " ");
  char * prog=temp;

  args=( char **) calloc(8, sizeof( char*));  

  getArgs(inputcpy, args, 1);

  if(debug) {
    printf("arg address after: %p\n", args);
    printf("morearg address after func: %p\n", moreArgs);
  }

  /*This is basically  what the shell will look like. The actual implementation will use stdin
   for input. (Unless a pipe or < is present in the input)*/
  int q;
  int pid=fork();
  if (pid!=0) {
    execvp(prog, args); //when moreArgs!=null, args should point to moreArgs
    return 0;
  }
  else {
    int status=0;
    wait(&status);
  }
}


/*This function should takes the first argument and inserts int into the second as " " separated tokens. If the second argument is too small -- the function recurses, and resizes the array as needed. The third argument is used to keep
  track of the recursion*/
char ** getArgs( char *input,  char **args, int ct) {
  int adj=(ct-1)*8;//if we recurse, this ensures correct indexes are used
  char *inputcpy=malloc(strlen(input));
  strcpy(inputcpy, input);

  /*Initialize indexes/Prepare for copying*/
  int i; 
  if(ct==1) {
    i=1; // this might throw off later adjusts
    args[0]=" "; //quick hack to ensure all args are used by exec()
  }
  else
    i=0;

  /**Actually do the copying now**/
  char *temp=strtok(NULL, " "); //What if later tokens are longer?
  args[adj+i++]=temp;

  while (temp != NULL && i<8) {
    temp=strtok(NULL, " ");
    args[adj+i++]=temp;
  }   

  /*If there are more args than we have room for*/
  if(i>=8){

    //is this allocation right?
    moreArgs = (char **) malloc((++ct)*8*sizeof( char *));

    /*Fill moreArgs with args*/
    int j;
    for (j=0; /*j<ct*8 && */args[j]!=NULL; j++) {
      moreArgs[j]=args[j];
    }   

    getArgs(inputcpy, moreArgs, (ct) ); //could probably move inc to malloc

    //free(args)?    
    if(ct>1)
      args=moreArgs;
  }
  /*Done with too many args problem*/


  return NULL;//(char **) args; //we don't want the global args though
}

The reason it's not behaving the way you want it to is because you're passing args by value.

char ** getArgs( char *input,  char ***args, int ct);

This way, you can reassign args .

Edit: Make sure you free args before reassigning. Edit 2: That was too specific of me. Make sure you free all the objects that you dynamically allocated. There's quite a few that you have just left.

As a side note, you're calling execvp from the parent process, and calling wait from the child process. It should be the other way around. Also, you should avoid using fork with execvp and use system instead. You get the benefit that it's an atomic operation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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