The contents of the array are safe within the function, however, upon returning to main the data becomes garbled.

section of main:

unsigned short tcp_port = atoi("8080");
const char* workDir = "WhatInthe?";

// Parse command line arguments

if ( parseCmdArg( tcp_port, workDir, argc, argv) == 1)
return 1;

cout << argc << endl;
cout << tcp_port << endl;
cout << workDir << endl << endl << endl;
cout << workDir << endl << endl << endl;

section of function code:

int parseCmdArg( unsigned short &tcp_port, const char* &workDir, int argc, char* argv[])

char tempPath[256];
bool pathDef = 1;

cout << "pCA tcp_port start: " << tcp_port << endl;
cout << "pCA wordDir start: " << workDir << endl;
cout << "pCA argc start: " << argc << endl;

*/ Command line process takes place here /*

if ( pathDef == 1 )
cout << "workDir = NULL" << endl;
getcwd(tempPath, 256);
workDir = tempPath;

cout << "pCA tcp_port exit: " << tcp_port << endl;
cout << "pCA wordDir exit: " << workDir << endl;
cout << "pCA tempPath exit: "<< tempPath << endl;
cout << "pCA argc exit: " << argc << endl;

The output to console is:

pCA tcp_port start: 8080
pCA wordDir start: WhatInthe?
pCA argc start: 1
workDir = NULL
pCA tcp_port exit: 8080
pCA wordDir exit: /user/ghesquie/cse422/lab3
pCA tempPath exit: /user/ghesquie/cse422/lab3
pCA argc exit: 1

/user/ghesqui? ??

So exiting the function the char array at pCA wordDir exit: is correct, but main show the first 13 characters, but the rest is garbled?

Any suggestions,

Thank you in advance,


8 Years
Discussion Span
Last Post by Tex09

Is the buffer NULL terminated..???

Getting garbage at the end of a cstring is a common sign of mising NULL termination. After briefly looking at the getcwd() documentation, it does not say anything about providing a null terminator at the end of the array.

Edited by Clinton Portis: n/a


Thank you again for all the help.

I was unable to get getcwd() to work correctly, but after digging some more, get_current_dir_name() did the job just fine.

On to more coding...



This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.