vendredi 8 septembre 2017

Passing unique_ptr to strtok

I am facing an issue with the legacy code. The legacy code uses a char array whose size is 1024 and is being passed to strtok c function.

char record[1024] = { '\0' };
char *token = NULL;
strcpy(record, inputString);// 'inputString' very large string.
token = strtok(record, DELIMETER);

And now because of new requirement, I have to modify the size to 20000 bytes and in many places they have declared this type (record)of local variable.

Now we are compiling the code with C++11 compiler and since we are using C++11 compiler, I was planning to modify the char array to unique_ptr as shown...

#define MAX_RECORD 50000
auto record = std::make_unique<char[]>(MAX_RECORD * 4);
char *token = NULL;
strcpy(record.get(), record);
token = strtok(record.get(), DELIMETER);

My question is, can I pass the unique_ptr to strtok function as the record variable gets modified inside the strtok function?

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire