class food- problem in debuging- in Destructor and toString




1 problem in destructor :

Error : in file " dbgdel.cpp" on this code,it breaks in debuging with break -points

/* verify block type */
_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse));

************

Error : problem in Foods::toString ,in file " output.c " on ""while loop"" , it breaks in debugging with break-pionts.


{
if (text.sz == NULL) /* NULL passed, use special string */
text.sz = __nullstring;
p = text.sz;
while (i-- && *p)
++p;
textlen = (int)(p - text.sz); /* length of the string *
}
*****************

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
90
91
92
93
94
95
96
97
98
 

class Foods
{
public:
	Foods(void);
	Foods(char* c, char* n, char* p);
	~Foods(void);
	
private:
	char* code;
	char* name;
	char* price;
	char* to_string;
	char* file_to_string;
	char** foods_pointers;

public:
	void setCode(char* c);
	void setName(char* n);
	void setPrice(char* p);
	
	char* getCode(void);
	char* getName(void);
	char* getPrice(void);
	char* getToString(void);
	char* getFileToString(void);
	char** getFoodsPointers(void);

	void init(char* c, char* n, char* p);
	void init(void);
	
	void toString(void);
	void print(void);
	
	int readFromFile(void);
	char* searchInFoods(char* food_code_to_search);
};

 

Foods::~Foods(void)
{
   
        if ( code!= NULL )
        {
            delete [] code;
            code = NULL;
        }
        ......
        ......
        .....
 }



Foods::Foods(void)
{
    price = NULL;
    name = NULL ;
    code = NULL;
    to_string = NULL;
    file_to_string = NULL;
    foods_pointers = NULL;
}


void Foods::setCode(char* c)
{  
       .....
       .....

         if ( Foods::searchInFoods (c) == "0")
        {
            code = strdup ( c );
        }
        else
        {
            cout << "This Food code is already exist\n\n";
            return;
        }

       .....
       .....
}



void Foods::toString()
{
    to_string = new char [100];        
    sprintf ( to_string, "%s\t\t%s\t\t%s\n", getCode(), getName(), getPrice() );
}




strdup() is not a standard function.

it's very likely that malloc() is used to get the memory and hence you must use free() to discard it. Using delete in this case will fail
why ? i didn't know it fail here, but why?
why strdup is not standard ? what do you mean ?

so i must use strcpy () ? I think it's just like that
malloc()/free() and new/delete are not compatible
thanks too much
yes, you need strcpy ().

Like so:
1
2
char *s = new[strlen(c) + 1]; // Note +1 for the terminating 0
strcpy (s, c);
i used malloc and free,
the last problem in destructor ,not happend, but it breaks in another file,

it breaks in file " dbgheap.c" in part " if " from this code that i wrote below :


extern "C" static int __cdecl CheckBytes(
unsigned char * pb,
unsigned char bCheck,
size_t nSize
)
{
int bOkay = TRUE;
while (nSize--)
{
if (*pb++ != bCheck)
{
thanks too much, the problem was from constructor with parametr, when i use it to make an object, before set every thing, i must call defult constructor in cinstructor with parametr to NULL every parameter.

I don't know why i must do that, but when i did this, it was no Error

thanks so mach.
well yes, you must initialize a pointer otherwise you will face a crash.

From the code you showed here I cannot see what happend
yes, it's true

thanks too much
Topic archived. No new replies allowed.