2

I can't understand why the whole thing doesn't work.

I just want to do malloc in the function func, when I return from it, the malloc disappears... and I get

* glibc detected ./test: free(): invalid pointer: 0xb76ffff4 **

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <string.h>
#include <errno.h>

int func(char *p) {
    p=(char*)malloc(1);
    *p='a';
    printf("1. p= %c\n",*p);
    return 0;
}

int main()
{
    char *p;

    func(p);
    printf("2. p= %c\n",*p);
    free(p);

    return 0;
}
3
  • You are trying to free an invalid pointer. p is only a copy of the pointer. Commented Nov 24, 2012 at 14:11
  • 1
    C is call by value, with a quirk that makes arrays turn into pointers when you pass them to functions. Commented Nov 24, 2012 at 14:17
  • Normally in C you allocate memory and then pass to functions who use it and then free the memory in the same function where you allocate it. Commented Nov 24, 2012 at 14:57

3 Answers 3

4
char *p;

makes a pointer local to main(), you can pass it to another function, however your passing it by value, so any changes you make to it (like changing what its pointing at) outside of the scope.won't "stick".

the solution is to pass a pointer to a pointer, or simply the address of p:

func(&p);

but don't forget to change the parameter list of func()!

int func(char **p)
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any difference with this stuff in c and c++ ? maybe this is the thing that got me confused? because i don't remember i ever did it like this
@hudac - defiantly! C++ has a concept of "pass by reference", which technicaly doesn't exist in C, the "work around" to that is passing the address of a variable (as in this example)
3

While in func, p is a copy of the pointer you passed to it, so p is created inside func and deleted when func is finished.

You have two solutions :

  1. Pass a pointer to the pointer, a char **p, and do *p = malloc(1), then func will accept a char **p
  2. Return the alloced variable un func and assign it : p = func();

2 Comments

Is there any difference with this stuff in c and c++ ? maybe this is the thing that got me confused? because i don't remember i ever did it like this
In C++ you still have access to malloc() function but you have to use the C++ "new" operator syntax that is adapted to objects.
3

pass &p in main means call func as func(&p) change the function sugnature of func(char *p) to func(char **p)

Thep in func is the local one to that func so when func exits it destroyed and also leads to memory leak.

And in main you are freeing a pointer which is not pointing to memory allocated by malloc, calloc so free(p) is Undefined behaviour in this case and it's not freeing that memory which you have allocated in func so it's memory leak

1st method:

void func(char **p)
{
 *p=malloc(1); 
//rest of your code here 
 }

int main()
{
 int *p;
 func(&p);
 // your code here 
 free(p); 
}

2nd method : It's easy no use of **

char * func(char *p)
{
p=malloc(1);
// rest of code here 
return p; 
}

int main()
{
char *p;
p=func(p);
//rest of code here 
free(p);
}

3 Comments

Shouldn't I do "func(&p)" only if i set "p" to be "char p",and not "char *p" ?
If funcs accepts a char **p and p is a char *, then &p will be a char **, so that's good.
@Intrepidd : yeah agreed with you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.