malloc in a function

Hello,

today i was studying for my exams in 2 weeks, where i need some C (not C++).
thats no big deal. but i tried a few things with malloc and pointers to check if i understood it.

ok, here we go:

in my main() i have
1
2
3
char *p0 = NULL;
char *p1 = NULL,
char *p2 = NULL;


i tested 3 things.

1
2
3
4
5
void f0(char *p) {
	p = malloc(100);
	printf("Pointer in f0: %p\n", p);
	return;
}

i supposed, that when i printf my pointer in the main, i get (nil)... so far so good.

next was
1
2
3
4
5
6
void f1(char **p) {
	p = malloc(100);
	printf("Pointer in f1: %p\n", p);
	return;
}
calling it with: f1(&p);

since i have a pointer to a pointer in the function, i thought in main i still have the space requested from the heap... but i get (nil) again.

last one
1
2
3
4
5
char* f2() {
	char *p = malloc(100);
	printf("Pointer in f2: %p\n", p);
	return p;
}

here i got again what i expected, the pointer wasnt (nil).

now my question is: why does f1 not work the way i think? :(

the complete code and output.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <stdlib.h>

void f0(char *p) {
	p = malloc(100);
	printf("Pointer in f0: %p\n", p);
	return;
}

void f1(char **p) {
	p = malloc(100);
	printf("Pointer in f1: %p\n", p);
	return;
}

char* f2() {
	char *p = malloc(100);
	printf("Pointer in f2: %p\n", p);
	return p;
}

int main(int argc, char *argv[]) {
	
	char *p0 = NULL;
	char *p1 = NULL;
	char *p2 = NULL;
	
	printf("p0 befor malloc: %p\n", p0);
	f0(p0);
	printf("p0 after malloc: %p\n", p0);
	
	printf("p1 befor malloc: %p\n", p1);
	f1(&p1);
	printf("p1 after malloc: %p\n", p1);
	
	printf("p2 befor malloc: %p\n", p2);
	p2 = f2();
	printf("p2 after malloc: %p\n", p2);
	
	free(p0);
	free(p1);
	free(p2);
	
	return 0;
}

p0 befor malloc: (nil)
Pointer in f0: 0x181a010
p0 after malloc: (nil)
p1 befor malloc: (nil)
Pointer in f1: 0x181a080
p1 after malloc: (nil)
p2 befor malloc: (nil)
Pointer in f2: 0x181a0f0
p2 after malloc: 0x181a0f0
Last edited on
Remember that when you pass parameters to functions, you're actually passing a COPY of the variable and not the variable itself. This applies just the same to pointers.



f0

Note that this creates a copy of the passed pointer. Even though you pass p0 to the function, 'p' is a seperate variable. So when you change 'p' with that malloc line, p0 remains unchanged (still NULL)

Also, once you return from this function, you have a memory leak because you cannot free() what you malloc here.

f1

This is a great exaimple why C's lack of type safety can be troublesome.

Here your pointer 'p' points to 'p1'. But then when you malloc, you change 'p' to point to newly allocated space instead (so it will no longer point to p1)

Notice that you're modifying the local pointer 'p' and not the pointer that 'p' points to. Therefore p1 remains unchanged.

The correct way to do this would be like so:

1
2
3
4
5
void f1(char **p) {
	*p = malloc(100);  // change the pointer that p points to
	printf("Pointer in f1: %p\n", *p);  // print that pointer
	return;
}


You can think of it this way.... because p points to p1, *p is p1. Therefore changes to *p (read: not p) will change p1.
first of all, thank you very much. :-D

the behavior of f0 you explained was just what i expected.

but now i understand how that works. i thought having a pointer to a pointer and calling it with & it'll work.. but after reading your explaination, i think its clear to me now.

Edit:
i edited my code to test it, and now i get:
1
2
Pointer in f1: 0x7fff3173fc68
p1 after malloc: 0x158e010

is that supposed to be right?

Edit #2:
works now.. i forgot to put the * like you said in printf...
Last edited on
Topic archived. No new replies allowed.