Access Violation, Why?

Its the same! Just a different location in memory...

1
2
//main.cpp
wineries->insert(winery("Lopez Island Vinyard", "San Juan Islands", 7, 95));


Then it takes me here.
1
2
3
4
5
6
7
8
9
10
11
...
const int MAXNAME_SZ = 75;
winery::winery(const char * const name, const char * const location, const int acres, const int rating)
  : name( new char[strlen(name)+1] ), location( new char[strlen(location)+1] ), acres( 0 ), rating( 0 )
{
	strcpy_s( this->name, MAXNAME_SZ, name ); //works!
	this->acres  = acres;
//GRR!
	/*strcpy_s( this->location, MAXNAME_SZ, location );*/ //does not work..
	this->rating = rating;
}


And the header:
1
2
3
4
5
6
7
8
9
10
11
12
class winery
{
public :

	winery(const char * const name, const char * const location, const int acres, const int rating);
        ...
private :
                char	*name;
	char	*location;
	int	acres;
	int	rating;
};


Whats wrong the code?
Last edited on
What is the value of MAXNAME_SZ ?
75. Enough for "San Juan Islands". Why is this an access violation? I thought that, an access violation happens when I am trying to access memory that doesnt belong to me.. But that doesnt make sense because im using name in the same way that Im using location..
Last edited on
In the winery::dtor, you specified neither the name nor the pointer can be changed, i.e.,
 
const char * const name,


but the data members are not so specified:
1
2
private:
 char * name,


Can you try to make both match? (Try removing all the const first?)

C-stle strings often give us much headache. Is is possible for you to use std::string instead?
1
2
3
4
5
6
7
8
9
10
11
12
class winery
{
public :

	winery(const std::string& name, const std::string& location, const int acres, const int rating);
        ...
private :
        std::string& name;
	std::string&  location;
	int	acres;
	int	rating;
};


1
2
3
4
5
const int MAXNAME_SZ = 75;
winery::winery(const std::string& name, const std::string& location, const int acres, const int rating)
  : name( name), location( location), acres( acres ), rating( rating )
{
}
Last edited on
I solved it!

1
2
3
4
5
6
7
8
winery::winery(const char * const name, const char * const location, const int acres, const int rating)
  : name( new char[strlen(name)+1] ), location( new char[strlen(location)+1] ), acres( 0 ), rating( 0 )
{
	this->name = const_cast <char*> (name);
	this->acres  = acres;
	this->location = const_cast <char*> (location);
	this->rating = rating;
}


The header:
1
2
3
4
5
6
7
8
9
10
11
12
class winery
{
public :

	winery(const char * const name, const char * const location, const int acres, const int rating);
        ...
private :
                char	*name;
	char	*location;
	int	acres;
	int	rating;
};


everyone forgets about the brute force of casting :x
Last edited on
Well, your solution is completely orthogonal to your original implementation, and the result is now you have a memory leak.

Your "solution" is not to copy the string, but rather copy the pointer to it. Your original code copied the string. Your
new code still allocates memory, however, and thus the memory leak.
so cast it back? Obviously I do not want a memory leak. Thanks, I will review it more.
I would use std::string. Regardless, how is the following code(no exception for memory allocation is considered; bad practice of returning raw pointers: who would be responsible for deleting them and when? I would use auto_ptr or boost smart pointers in this case. Not a good solution even if the code compiles and no memory leak)?
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
class winery
{
public :
	winery(const char * const name, const char * const location, const int acres, const int rating);
	char* getName() const {return m_name; }
	char* getLocation() const {return m_location; }
private :
        char* m_name;
	char* m_location;
	int	m_acres;
	int	m_rating;
};

winery::winery(const char * const name, const char * const location, const int acres, const int rating)
  : m_acres( acres ), m_rating( acres )
{
	if (name)
	{
		size_t len = strlen(name) + 1;
		m_name = new char [len];
		strcpy_s(m_name,len, name);
	}
	else					// passing in empty name: name = "";
	{
		m_name = NULL;
	}
	if (location)
	{
		size_t len = strlen(location) + 1;
		m_location = new char[len ];
		strcpy_s(m_location,len,location);
	}
	else					// passing in empty location: location = "";
	{
		m_location = NULL;
	}
	
}
int main()
{
	winery w = winery("Lopez Island Vinyard", "San Juan Islands", 7, 95);
        // winery w = winery("", "", 7, 95); // play with empty name and location
	std::cout << w.getName() << std::endl;
	std::cout << w.getLocation() << std::endl;
	return 0;
}
Last edited on
Yes.

I thought I could pull that off in the c'tors init list.

However, this will not work with a default ctor for winery!

Reason: we need to free the memory being used by m_location and m_name for the very next call in main.

...



Topic archived. No new replies allowed.