Question 1:
void db_cstr(char* cstr, int len) {
char* temp2 = cstr;
cstr = (char*)malloc(len*2*sizeof(char));
// print 1
printf(cstr);
printf("\n");
//print 2
printf(temp2);
printf("\n");
strcpy(cstr, temp2);
//free
free(temp2);
//print 3
printf(cstr);
}
int somefunction(){
int array_len = 10;
char* cmd = (char*)malloc(array_len*sizeof(char));
strcpy(cmd, "apple");
db_cstr(cmd, array_len);
// final print
printf(cmd);
return 1;
}
Why does final print, printf prints nothing ?Also,
How do you make the cmd pointer point to what the new cstr is pointing to
in db_cstr.
Question 2:
int main(int argc, char ** argv) {
int * arr;
foo(arr);
printf("car[3]=%d\n",arr[3]);
free (arr);
return 1;
}
void foo(int * arr) {
arr = (int*) malloc( sizeof(int)*25 );
arr[3] = 69;
}
Why the error:Non-aligned pointer being freed"
The following free()s the caller's cmd:
ReplyDeletechar* temp2 = cstr;
free(temp2);
Therefore the final printf() is trying to print memory that's already been freed, which is undefined behaviour.
The easiest way to make db_cstr() return the new pointer is like so:
char* void db_cstr(char* cstr, int len) {
...
printf(cstr);
return cstr;
}
int somefunction(){
...
cmd = db_cstr(cmd, array_len);
...
}
Alternative:
If you want a function to change a var, you should pass a pointer to it. This is true even when the var is a pointer.
void db_cstr(char** cstr, int len), db_cstr(&cmd, array_len);
Question 2:
ReplyDeleteYou pass the pointer by value, not by reference, so whatever you do with arr inside foo will not make a difference outside the foo-function. As m_pGladiator wrote one way is to declare a reference to pointer like this (only possible in C++ btw. C does not know about references):
int main(int argc, char ** argv) {
int * arr;
foo(arr);
printf("car[3]=%d\n",arr[3]);
free (arr);
return 1;
}
void foo(int * &arr ) {
arr = (int*) malloc( sizeof(int)*25 );
arr[3] = 69;
}
Another better way is to not pass the pointer as an argument but to return a pointer:
int main(int argc, char ** argv) {
int * arr;
arr = foo();
printf("car[3]=%d\n",arr[3]);
free (arr);
return 1;
}
int * foo(void ) {
int * arr;
arr = (int*) malloc( sizeof(int)*25 );
arr[3] = 69;
return arr;
}
And you can pass a pointer to a pointer. That's the C way to pass by reference. Complicates the syntax a bit but well - that's how C is...
int main(int argc, char ** argv) {
int * arr;
foo(&arr);
printf("car[3]=%d\n",arr[3]);
free (arr);
return 1;
}
void foo(int ** arr ) {
(*arr) = (int*) malloc( sizeof(int)*25 );
(*arr)[3] = 69;
}