Unix Programming - modify argc and argv

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > July 2005 > modify argc and argv





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author modify argc and argv
Billy N. Patton

2005-07-19, 5:54 pm

I would like to modify argc and argv.
I'm trying to build a procedure that will:
1. take in argc and argv as input.
2. remove known arguments
3. reduce argc
4. rebuild argv by removing known arguments.

Using g++ 3.2.3

The source:
#include <iostream>
#include <string>
using namespace std;
void xx(int & argc,char**& argv);
int main(int argc,char** argv)
{
xx(argc,argv);
return 0;
}
void xx(int & argc,char**& argv)
{
string s;
int i;
for (i = 1; i < argc; i++)
{
s = argv[i];
if ( s == "-a")
{
i++;
cout << __LINE__ << "\n"; // line 24
free(argv[i]);
cout << __LINE__ << "\n";
argv[i] = NULL;
}
}
}

How I compile: /usr/bin/g++ -o x x.cxx
What I execute: x -a 1 -b 2 -c
What are the results:
24
Segmentation fault

THe free is where it is getting the segfault.
I assumed that it was created with free. I tried delete with the same
results.

I tried char** argv to be passed instead of char**& with the same segfault.


--
___ _ ____ ___ __ __
/ _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
/ _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
/____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
/___/
Texas Instruments ASIC Circuit Design Methodology Group
Dallas, Texas, 214-480-4455, b-patton@ti.com
David Schwartz

2005-07-19, 5:54 pm


"Billy N. Patton" <b-patton@ti.com> wrote in message
news:dbiv4a$s9m$1@home.itg.ti.com...

> free(argv[i]);


> Segmentation fault


> The free is where it is getting the segfault.
> I assumed that it was created with free. I tried delete with the same
> results.
>
> I tried char** argv to be passed instead of char**& with the same
> segfault.


Why are you trying to 'free' something you didn't allocate?

DS


Fletcher Glenn

2005-07-19, 5:54 pm

Billy N. Patton wrote:
> I would like to modify argc and argv.
> I'm trying to build a procedure that will:
> 1. take in argc and argv as input.
> 2. remove known arguments
> 3. reduce argc
> 4. rebuild argv by removing known arguments.
>
> Using g++ 3.2.3
>
> The source:
> #include <iostream>
> #include <string>
> using namespace std;
> void xx(int & argc,char**& argv);
> int main(int argc,char** argv)
> {
> xx(argc,argv);
> return 0;
> }
> void xx(int & argc,char**& argv)
> {
> string s;
> int i;
> for (i = 1; i < argc; i++)
> {
> s = argv[i];
> if ( s == "-a")
> {
> i++;
> cout << __LINE__ << "\n"; // line 24
> free(argv[i]);
> cout << __LINE__ << "\n";
> argv[i] = NULL;
> }
> }
> }
>
> How I compile: /usr/bin/g++ -o x x.cxx
> What I execute: x -a 1 -b 2 -c
> What are the results:
> 24
> Segmentation fault
>
> THe free is where it is getting the segfault.
> I assumed that it was created with free. I tried delete with the same
> results.
>
> I tried char** argv to be passed instead of char**& with the same segfault.
>
>

Since argv is controlled by the system, you have no assurance that
anything you do to change argv will not cause problems. I would
recommend that if you want to play with argv, that you make your
own deep copy to play with. You can always label argv with a different
name, and refer to your copy as argv if you wish.

--

Fletcher Glenn

Alex Colvin

2005-07-19, 5:54 pm

>THe free is where it is getting the segfault.
>I assumed that it was created with free. I tried delete with the same
>results.


don't assume that.
it might have been part of a larger structure, it might have been static.
it might work today and fail tomorrow. it might work for you and fail for
me.
--
mac the naïf
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com