Memory problems

Hi,
I have a memory access problem in this code:

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pension.h"

struct _DOG_BASIC {
	int dog_id;
	char* dog_name;
	char dog_gender;
	char* first_name;
	char* family_name;
	char* cell_phone;
	char* date;
	int days_num;
};

struct _DOG1 {
	int dog_id;
	char* dog_name;
	char dog_gender;
	char* first_name;
	char* family_name;
	char* cell_phone;
};
struct _DOG2 {
	DOG1* dog_type2;
	char* date;
};
struct _DOG3 {
	DOG1* dog_type3;
	char* date;
	int days_num;
};
struct _PENSION {
	DOG1* p1;
	DOG2* p2;
	DOG3* p3;
};

BOOL AddDog(PENSION* ppension, int dog_id, char* dog_name, char dog_gender,char* first_name, char* family_name, char* cell_phone, char* date,int days_num) {
	int len_dog_name = strlen(dog_name);
	int len_first_name = strlen(first_name);
	int len_family_name = strlen(family_name);

	//char *p_dog_name, *p_first_name, *p_family_name;

	if ((date == NULL) && (days_num == -1)) { // type 1
		ppension->p1->cell_phone = cell_phone; 
		ppension->p1->dog_gender = dog_gender;
		ppension->p1->dog_id = dog_id;
		ppension->p1->dog_name = (char*) malloc(sizeof(char[len_dog_name]));
		ppension->p1->first_name = (char*) malloc(sizeof(char[len_first_name]));
		ppension->p1->family_name = (char*) malloc(sizeof(char[len_family_name]));
		if ((ppension->p1->dog_name == NULL) || (ppension->p1->first_name== NULL) || (ppension->p1->family_name == NULL))
			return FALSE;
		ppension->p1->dog_name = dog_name;
		ppension->p1->first_name = first_name;
		ppension->p1->family_name = family_name;
	} else if ((date != NULL) && (days_num == -1)) { // type 2
		ppension->p2->dog_type2->cell_phone = cell_phone; 
		ppension->p2->dog_type2->dog_gender = dog_gender;
		ppension->p2->dog_type2->dog_id = dog_id;
		ppension->p2->dog_type2->dog_name = (char*) malloc(sizeof(char[len_dog_name]));
		ppension->p2->dog_type2->first_name = (char*) malloc(sizeof(char[len_first_name]));
		ppension->p2->dog_type2->family_name = (char*) malloc(sizeof(char[len_family_name]));
		if((ppension->p2->dog_type2->dog_name == NULL)|| (ppension->p2->dog_type2->first_name == NULL)|| (ppension->p2->dog_type2->family_name == NULL))
			return FALSE;
		ppension->p2->dog_type2->dog_name = dog_name;
		ppension->p2->dog_type2->first_name = first_name;
		ppension->p2->dog_type2->family_name = family_name;
		ppension->p2->date = date;
	} else { // type 3
		ppension->p3->dog_type3->cell_phone = cell_phone;
		ppension->p3->dog_type3->dog_gender = dog_gender;
		ppension->p3->dog_type3->dog_id = dog_id;
		ppension->p3->dog_type3->dog_name = (char*) malloc(sizeof(char[len_dog_name]));
		ppension->p3->dog_type3->first_name = (char*) malloc(sizeof(char[len_first_name]));
		ppension->p3->dog_type3->family_name = (char*) malloc(sizeof(char[len_family_name]));
		if ((ppension->p3->dog_type3->dog_name == NULL)|| (ppension->p3->dog_type3->first_name == NULL)|| (ppension->p3->dog_type3->family_name == NULL))
			return FALSE;
		ppension->p3->dog_type3->dog_name = dog_name;
		ppension->p3->dog_type3->first_name = first_name;
		ppension->p3->dog_type3->family_name = family_name;
		ppension->p3->date = date;
		ppension->p3->days_num = days_num;
	}

	return TRUE;
}


When I called the function AddDog I received this error:
Cannot access memory at address 0xbaadf015

in this line: ppension->p3->dog_type3->cell_phone = cell_phone;
Should I have to add any malloc there?

Thanks
This is so similar to memory address 0xBAADF00D that it's clearly just BAADF00D with a small value added to it.

0xBAADF00D is used by the MS compiler in debug mode to indicate uninitialised allocated heap memory; memory that you have allocated using malloc but haven't actually done anything with will contain the value 0xBAADF00D over and over and over again.

Since you're trying to access memory location BAADF015, I'd guess that you've alllocated some space for a pointer and then you never actually made that pointer point at an object.
Last edited on
Thank you Moschops for the explanation !
I called (after else) 3 mallocs and assigned values for the pointers there, so I still can't figure out what I missed there.
That's how I called the function:
 
result = AddDog(ppension ,123 ,&dog_name_str, 'F', &first_name_str, &family_name_str, &cell_phone_str, &date_str, 5);


I guess the problem is in the function itself.
Do you see what is missing?

Thanks..
I guess one of these pointers ppension->p3->dog_type3->cell_phone has a bad value. Print out the value of each pointer (not the thing it points to - the pointer itself) and see which one you haven't set.
Ok I'll try that. Thanks !
Topic archived. No new replies allowed.