ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Oh, OK. Well, in the code example you gave, *you* are the one responsible for allocating memory for 'args' and initializing it (and later freeing it). Also the declaration doesn't make sense; you are requesting a static allocation but have not specified the size; try "char **args" instead. You will then have to allocate space for 'args' to store the list of char pointers; keep in mind that pointer sizes can vary from one machine to another (or even on the same machine), so you need to allocate memory according to the size of char* as determined at run-time (actually at compile-time is sufficient if you know what's going on).
Otherwise, your later line with the '???' doesn't have anything wrong with it. You just have to be careful with overrunning your allocated memory etc.
I won't comment on the rest of the code in general; I'm assuming that is only for explaining your problem and not actual production code.
I understand this. I'm just not quite sure about how to allocate memory for each of the array elements and the array itself. The syntax is what I'm not sure about.
As in C, those strange sequences of ()* and so on can get confusing; you just have to get used to the rules on precedents. Most of the time you can use new/delete naked, but in some cases you do need to use ::new/::delete
[edit] Your assignment will upset the compiler though since c_str() returns 'const CharT *' not 'char *'. On top of that, what is CharT? Is it ASCII or unicode or UTF-8?
Thanks. That actually helps a lot. I just couldn't wrap my mind around the sizeof() and stuff. And I wasn't sure how to do it with new(). It's late. I probably should be sleeping and working on it in the morning when I can actually read correctly.
If new() is redefined within a class, every time you use new() within that class, the new definition is used. To force the compiler to use the usual definition you need to specifically request it with '::new()'. This is similar to other name/scope issues which arise with the C++ object model.
I won't comment on the rest of the code in general; I'm assuming that is only for explaining your problem and not actual production code.
I would just like to point out that using a std::vector will remove some of the allocations from user code and that using the pointer return by c_str() in the manner which you do is error prone for reasons such as the string going out of scope.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.