473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

vector<string> to char*[]

i have std::vector<stringarg; for dynamic program configuration.
i need get const char* argv[]; for execv(3).

how i can make its simple?
there may be ready solutions?

--
www.andr.ca
Oct 2 '07 #1
6 4279
On Tue, 2 Oct 2007 06:09:22 +0000 (UTC), Andrew Wingorodov wrote:
i have std::vector<stringarg; for dynamic program configuration.
i need get const char* argv[]; for execv(3).

how i can make its simple?
there may be ready solutions?
I assume you are talking about some POSIX system (indicated by execv).
Since execv replaces the current running program with the executed
one, worries of memory leaks do not apply (except when an error happens).
So you can do this:

const char** argv = new const char*[ arg.size() ];
for(size_t a=0; a<arg.size(); ++a) argv[a] = arg[a].c_str();
execv(... argv ...);
// cleanup when error happened
delete[] argv;

--
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)
Oct 2 '07 #2
Joel Yliluoma <bi*****@iki.fiwrote:
const char** argv = new const char*[ arg.size() ];
for(size_t a=0; a<arg.size(); ++a) argv[a] = arg[a].c_str();
execv(... argv ...);
// cleanup when error happened
delete[] argv;
i do correct?

//
const char**
args::constchar (const std::vector<std::string>& arg_)
{
static std::auto_ptr<const char*v;
v = (std::auto_ptr<const char*>) new const char* [arg_.size()+1];

std::vector<std::string>::const_iterator i = arg_.begin();
size_t j;

for (; i != arg_.end (); ++i)
{
v.get()[j++] = (*i).c_str() ;
}

v.get()[j] = NULL;
return v.get();
}

--
www.andr.ca
Oct 2 '07 #3
Alf P. Steinbach <al***@start.nowrote:
auto_ptr calls delete, whereas you need delete[]. Not that it matters
much in practice for a char array. But formally UB.

When the function returns, the auto_ptr calls delete. As mentioned

Try what I posted earlier, it's safe:
i got it, 10x
but i'm afraid that the vector isn't equiv to the array of poiter to chars

may be this is a better way?

//--code--

class constchar
{
private:
const char** v;

public:
constchar (const std::vector<std::string>& arg_);

inline virtual ~constchar () { delete [] v; }

inline operator const char** () const { return v; }

}; //!class constchar

//
constchar::constchar (const std::vector<std::string>& arg_)
{
v = new const char* [arg_.size()+1];
std::vector<std::string>::const_iterator i = arg_.begin();
size_t j;

for (j^=j; i != arg_.end (); ++i)
{
v[j++] = (*i).c_str() ;
}

v[j] = NULL;
}

int
main ()
//////.......

vector<stringargv;
vector<stringenvp;
....
::execve (..., constchar (argv), constchar (envp));
::spawnve (..., constchar (argv), constchar (envp));

//--code--

--
www.andr.ca
Oct 2 '07 #4
On Oct 2, 4:33 pm, "Alf P. Steinbach" <al...@start.nowrote:
* Andrew Wingorodov:
i have std::vector<stringarg; for dynamic program configuration.
i need get const char* argv[]; for execv(3).
how i can make its simple?
there may be ready solutions?

std::vector<stringarg;
...

std::vector<char const*v( arg.size() );
for( size_t i = 0; i < v.size(); ++i ) { v[i] = arg[i].c_str(); }
blahblah( &v[0] );
Whay go through all that when he already has what he wants
in his original vector?

const char* tmp = arg.at( n ).c_str();

Cheers,
Chris Val

Oct 2 '07 #5
Alf P. Steinbach <al***@start.nowrote:
>
Why not write just j=0 (far more readable), and why not declare j right
there, in the innermost scope it can be declared?

And why keep incrementing both an index and and an interator?
the best is the enemy of the rest (c) ;-)
my code are works, thanks for all advise

--
www.andr.ca
Oct 2 '07 #6
On Oct 3, 12:52 am, "Alf P. Steinbach" <al...@start.nowrote:
* Chris ( Val ):


On Oct 2, 4:33 pm, "Alf P. Steinbach" <al...@start.nowrote:
* Andrew Wingorodov:
>i have std::vector<stringarg; for dynamic program configuration.
i need get const char* argv[]; for execv(3).
how i can make its simple?
there may be ready solutions?
std::vector<stringarg;
...
std::vector<char const*v( arg.size() );
for( size_t i = 0; i < v.size(); ++i ) { v[i] = arg[i].c_str(); }
blahblah( &v[0] );
Whay go through all that when he already has what he wants
in his original vector?
const char* tmp = arg.at( n ).c_str();

Andrew wants an array of pointers to C-strings, for use as execv
argument (later down the thread I checked execv's signature and found
that there's also a const issue, and that the array should be
null-terminated, but those are just technicalities).
I see.
What you have is one pointer to a C-string.
Yes.

I thought that would be sufficient for the OP's needs,
and didn't see the necessity of an additional vector :-)

--
Chris Val

Oct 2 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
10
by: dalbosco | last post by:
Hello, I am new to STL and I've written the following code which crash. Could anyone tell me why this code crash? Thanks by advance. -- J-F #include <iostream>
4
by: misirion | last post by:
Ciao, vorrei poter scrivere e leggere su files binari dei vettori di stringhe, ed avrei implementato questo codice: ifstream fin(conf.c_str(),ios::binary); char inc; fin.read( inc,...
2
by: ehui928 | last post by:
hi, everybody I am a newbie in STL. When I compile the following program under gcc4.0, I got a the following errors. I wonder whether the form of list< vector<string> > is correct in STL ? //...
10
by: Shafik | last post by:
Hello, I am new to C++. I know the reason is probably template instantiation problems ... but what's the *real* reason I cannot declare a: vector<stringv = vector<string>(4); Thanks!...
2
by: Rockair | last post by:
hi! there is a class: class card { static vector<string> names; //... };
5
by: Etrex | last post by:
Hello, This is my first attempt at a c++ program, and it is a long post, please bear with me. I'm trying to read in a text file containing a firewall log, make the information...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
5
by: Peithon | last post by:
Hi, I'm trying to create a vector of strings and print the contents using an iterator but I'm getting an error with my very first string. Can anyone help?
6
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1",...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.