c++ - What is the difference between "hard-coding" and passing in arguments in regard to memory? -
so title question. in memory, arguments can located on stack or heap depending on how initialized, how hard-coded information dealt with?
as example, use constructor ifstream
what difference between this:
void function(){ ifstream infile("home/some/file/path"); }
vs
void function(char* filepath){ ifstream infile(filepath); //filepath points character array contains home/some/file/path }
could memory implications arise use of 1 on other? (multithreading lead heap corruption if char* isn't free'd correctly? etc).
i trying understand difference , possible implications can apply answer larger problem. insight welcome , feel free correct me if i've made incorrect statements/assumptions!
literals (which first example shows) placed static initialization portion of executable (which why, if on *nix system), can use command strings
, obtain list of literals in application.
your 2nd example should modified to
void function(const char* filepath) { ... }
unless going modify pointer in function. memory function can come anywhere (a string literal being passed function, constant string declared somewhere else in application, string stored in memory , inputted command line or console, etc.)
the primary issue run multithreading here if 2+ threads attempting load same file @ same time. may not problem if reading it, if have thread wants write , obtains exclusive lock on file, other threads deadlock. isn't directly related strings question, though.
Comments
Post a Comment