tags:

views:

32

answers:

5

hi,

id like to spawn a process with unknown number of parameters. my code so far (using power++):

char** psaParameters = new char*[ saParameters.GetCount() ];

            for( int i = 0; i < saParameters.GetCount(); i++ )
            {
                psaParameters[ i ] = new char[ saParameters[ i ].GetLength() ];
                strcpy( psaParameters[ i ], saParameters[ i ].GetText() );
            }

            spawnv( P_WAIT, psaParameters[ 0 ], psaParameters );

but it's crashing. why?

thank you! -dominik

+1  A: 

What type saParameters[i] is?

Probably you need to change this line to:

psaParameters[ i ] = new char[ saParameters[ i ].GetLength() + 1 ];

to include the zero-terminator.

ybungalobill
A: 

What is the type of saParameters? My guess, without knowing that is that GetLength() returns the number of characters not including a NUL terminator. Since strcpy automatically adds this it would result in overflowing each of the buffers inside the saParameters array.

torak
A: 

You don't allocate enough space for the terminating null character.

 psaParameters[ i ] = new char[ saParameters[ i ].GetLength() ];

should be

 psaParameters[ i ] = new char[ saParameters[ i ].GetLength() + 1 ];
Nick Meyer
A: 

saParameters is a WStringArray, a class defined in the power++ development environment. thank you for your answers so far! but the application still crashes. (it crashes in the spawnv()-line)

Dominik
A: 

ok we found out what the problem was:

  1. the GetLength() + 1 was correct - thank you ;)
  2. the last array element (pointer) in psaParameters had to be NULL so the spawnv()-method knows when to stop reading arguments from the argument-array.
Dominik