I wanted to understand memory and pointers better before I endeavour into file path manipulations for larger project. This is simple replace part of the string test function that I need to rewrite into struct to call upon. I used scopes to test for memory leaking and changes, I work on Windows 7, CygWin and notepad++.
I am using std::size_t
as an size representation due to:
A type whose size cannot be represented by std::size_t is ill-formed mentioned by cppreference with "std::size_t is commonly used for array indexing and loop counting".
I decided to go for the c-style null terminated character array since arguments passed through main(int argc, char **argv)
are null terminated characters and system calls accept those best. It seemed beneficial to code parts that i need myself instead of relying on another library, and it is tiresome to keep track what is converted with c_str()
and what isn't.
As mentioned above, this function is part of project that manipulates, changes and makes full or relative paths to the files. The character array helps me use stat
that comes with CygWin to be able to automatically test if path is not accessible, directory or file. I could use std::string
but that is an extra header and i plan serious expansions of file paths to include some custom behaviour - which isn't tied to this function.
#include <iostream>
size_t get_str_size(const char* temp){
size_t s=0;
while( temp[s] != '\0' )s++;
return s;
}
int main(int argc, char **argv){
const char * temp = "sublime/subliminal/sfit.exe"; // example string
std::cout << &temp << "\t" << temp << std::endl; // prints out address and value
{ // wild scope to help me navigate and debug
const char* shitcake = "sfitcake"; // string to be put in place of sfit
size_t ldelim = 0; // / last delimiter
size_t edelim = 0; // . end delimiter
size_t original_size = get_str_size(temp);
// finding the necessary indexes of charachters
for( int i =0 ; i <= original_size; i++ ){
if( temp[i]=='/' ){ldelim=i;}
if( temp[i]=='.' ){edelim=i;}
}
// calculating size that influences the char array
size_t old_size = edelim - ldelim -1; // current char array size(-1 to remove last delimiter)
size_t new_size = get_str_size(shitcake); // new char array size
// char array duplication , since original string is const
char* tempcopy = new char[original_size]; // holds the copy of the temp
for( int i =0 ; i < original_size; i++ ){ tempcopy[i] = temp[i]; }
// value to be constructed
char *new_version = new char[ original_size + (new_size-old_size) ];
// filling the part before replacement happen
for( int i =0 ; i <=ldelim; i++ ) { new_version[i] = tempcopy[i]; }
// filling replaced char array
for( int i =0 ; i <= new_size;i++ ) { new_version[i+1+ldelim] = shitcake[i]; }
// the rest of original string
for( int i =edelim ; i < original_size;i++ ) { new_version[i+old_size] = tempcopy[i];}
//conversion back to const char* with expanded size and new value
temp = (const char*)new_version;
//deleting pointers
delete tempcopy;
delete new_version;
}
std::cout << &temp << "\t" << temp << std::endl;
return 0;
}
The output i am getting is:
0xffffcbb8 sublime/subliminal/sfit.exe
0xffffcbb8 sublime/subliminal/sfitcake.exe
Which means it works, but i am afraid of pointer not changing memory address even though the size and value changed.
Main concerns and desires for code:
- Removing chance of dangling pointer and memory issue
- Fast and readable code