If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

URGENT: Initializing the size of a char*[]

I am writing a program that acts as a simple shell. Basically, my program outputs a prompt, then waits for the user to input a command. My program then handles that command and ouputs a new prompt. This process continues until the user quits. The specifics of the program are not relevant though. The only question I have is with the variable char *arg[] inside of my Info struct. I am not sure how to initialize it's size. I tried just saying char *arg[20], but that ended up giving it a size of 0 for some reason.
Can anyone correct this problem in my code for me?
Can anyone tell me how to initialize the size of a char*[]?
I have the entire program below, or you can download the attachment of it.Thank You!

The sizeof(args) prints 4, because args is a pointer, and the size of a pointer is 4 bytes. The strlen(*args) prints 7 because the *args is the address of the first string in the args array, and that is "Testing".

So the bottom line is that when you pass a pointer, all your information concerning the number of elements in the array is lost. You cannot recover this information. So either you use a higher-level structure such as a vector of strings (which you can get the size), or you must pass the number of elements separately as another argument to parseCmd.

If your problem is that the sizeof is not returning the right value, why did you post all your code? If you have a problem, post only that section or ask a specific question. I don't even know if I just answered what you wanted.

Also, if you wrote a small example program as I have done, you will see why things work the way they do. Many C++ programmers write small, test programs to see how things work.

Thank you for the comments. I have tested my program and it works if you only input one word (ex: "ls"). But if you input more than one word, it goes out of bounds of the array. So my only question is how do you initialize the size of the *args[ ] array?

For example, using the exact code that I have, how would you create an instance of the struct Info, with *args[ ] having the size of 20 (i.e. args will be an array of 20 character arrays each of size 20 as well)?

Originally posted by waverdr9
Thank you for the comments. I have tested my program and it works if you only input one word (ex: "ls"). But if you input more than one word, it goes out of bounds of the array. So my only question is how do you initialize the size of the *args[ ] array?

Did you see my example? There are 20 char pointers in the args member. I successfully set two strings in the args array my example -- I could have set up to args[19].

For example, using the exact code that I have, how would you create an instance of the struct Info, with *args[ ] having the size of 20 (i.e. args will be an array of 20 character arrays each of size 20 as well)?

I answered that question with the example I posted. There is nothing special in your code that would make it follow different rules. If you want an array of 20 pointers, you declare an array of 20 pointers.

Originally posted by waverdr9
That is what I have been trying to ask all along.
How do you initialize and allocate the size a char*[], using the 'new' operator?

Code:

argv[0] = new char [SomeLength];
//...
delete [] argv[0];

This allocates SomeLength characters for argv[0] and removes it when finished. You do the same thing for argv[1], argv[2], etc. You also have to make sure you free the memory at the end.

The best way to do this is to create a class that handles these details. Your class should have an AddArg() member function which just adds a string to a string vector. When you need the arguments, another member function, call it GetArgs(), creates the char *[] array from the arguments that are in the string vector, and returns it. The destructor of the class would clean up the memory.

If you compile and run this, however, you will get a segmentation fault when trying to assign a value to index 2 of pInfo.arg[]. Can you fix this little code so that it would work properly? If you can do that, I am sure that I can apply it to my actual program. I am sorry that I am being so difficult in absorbing this concept. It is just foreign to me. Thank you in advance for any more help you can provide. I really appreciate it!

The strcpy() function takes a pointer to a null-terminates string as the second argument. Why are you specifying a single char?

There are a lot of things wrong with your code that I haven't covered, but the major thing is that you are not managing the memory correctly. This is where usage of the standard classes is paramount, and only when you need to set up a char *[] do you actually do it. In your code, you do not have a class that encapsulates the argument setting -- instead, you are delegating a lot of the "setting up" and allocating to the main program.

Note the main() program and the parseCmd function. You see no calls at all to allocate any memory. What you do see is a public member function of the ArgHandler class called AddArg(). How it does what it does is no business of the parseCmd() function, it is the responsibility of the ArgHandler class.

Look how AddArg() is implemented -- all it does is add a string to a string vector. There is no concern for the char*[] array because there is no need for it when you add an argument. The only time when you do need a char *[] is when you actually need it for the execvp() call in your program. In this case, another member function, GetArgs() creates and returns a char **.

The other thing to note is that when the ArgHandler class is destroyed, the memory is cleaned up (see the ArgHandler destructor).

with an optional break in the else of the "if" if one is sure that, once the first NULL is reached in argv, there will be no more. This code also doesn't rely on the logic of AddArg to maintain the 20 size limit correctly.

Yes, you're right, but I wrote the code in a flash and did not consider any "fine points" i.e. #define or any thorough debugging. Posting "real-world" code would have taken too much of my time and hopefully the OP corrects the problem.

Anyway, I believe this was a homework problem, since the very same question was asked just two days ago by someone else.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.