Initializing a char array through passed pointer segfaults
- by Bitgarden
Ie., why does the following work:
char* char_array(size_t size){
return new char[size];
}
int main(){
const char* foo = "foo";
size_t len = strlen(foo);
char* bar=char_array(len);
memset(bar, 0, len+1);
}
But the following segfaults:
void char_array(char* out, size_t size){
out= new char[size];
}
int main(){
const char* foo = "foo";
size_t len = strlen(foo);
char* bar;
char_array(bar, len);
memset(bar, 0, len+1);
}