c - Sprintf fails with invalid parameter passed to function in loadrunner while defining string using *varname -


this question has answer here:

first version: (works)

//using sprintf int index = 56; char filename[64], * suffix = "txt"; sprintf(filename, "log_%d.%s", index, suffix); lr_output_message ("the new file name %s", filename); //this works 

second version: (does not work)

 //using sprintf  int index = 56; char *filename, * suffix = "txt"; sprintf(filename, "log_%d.%s", index, suffix); lr_output_message ("the new file name %s", filename); //fails invalid parameter passed function 

  • your first version (is correct and) works, because there (enough) memory allocated filename, char array.

    char filename[64]. //.... 
  • your second version (is incorrect and) not work, because there no memory allocation filename, define pointer.

    char *filename,//... 

this not allocate memory pointer automatically.

in other words, pointer not pointing valid memory address. so, trying access memory pointed pointer invalid , invokes undefined behaviour.

solution: need allocate memory pointer before use it. can use malloc() , family functions allocate memory pointer.

also, 1 allocate memory , usage done. you've release allocated memory using free()


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -