Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access violations when using strcpy function and polymorphism 1

Status
Not open for further replies.

moron

Programmer
May 10, 2000
2
US
I'm new to this so forgive me if this is too simple a question, but for my assignment we were to change a program from just a simple exercise with a base and derived class to begin using virtual functions, etc.<br><br>In the first program I used:<br>Highway::Highway(char *n, double dist):Length(dist) {<br> setName(n);<br> carCount = 0;<br> width = 0; }<br><br>void Highway::setName( char *n)&nbsp;&nbsp;{ strncpy(name, n, strlen(n)); }<br><br>to set my private data of &quot;Name&quot; to the value sent to it.<br><br>However, when I made the necessary changes to add polymorphism to this program, I began getting access violations whenever I tried to copy strings.&nbsp;&nbsp;I traced through the code and it seems to be occurring during the copy portion and gives me the access violation message.&nbsp;&nbsp;I made the strncpy a friend in the class, but it did not seem to make any difference.&nbsp;&nbsp;Any help provided would be greatly appreciated.<br><br>
 
I dont know if you noticed, but you mispelled strcpy, you have it as strncpy. by the way, I am a little bit confused on your first function, are you definning two different functions, in the same scope? <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
Dear mor_n,<br><br>It is very difficult to tell what is causing your problem give the code you provided to us. In the following:<br><br>&gt; strncpy(name, n, strlen(n)); <br><br>There is no code provided that shows where or how 'name' is being allocated. Of course if it isn't being allocated that would be your problem.<br><br>-pete
 
As well as improper declaration in the class itself. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
Well, let me start off by saying that I gave myself the right handle.&nbsp;&nbsp;I know that I put in the subject line strcpy but the code I copied was strncpy.&nbsp;&nbsp;That was because I had changed the function to see if that was the case.&nbsp;&nbsp;Finally about 1:30 last night, I figured out how to get this to work.&nbsp;&nbsp;As it turned out, my primary problem was that I was using the string functions when it wasn't necessary at all.&nbsp;&nbsp;I was passing a pointer for my char string to the constructor, which then passed the pointer to the setName function I included in my post.&nbsp;&nbsp;What I finally figured out last night was that because my private data was defined as:<br><br>char *name;<br><br>all I had to do was change the setName function to <br><br>Highway::setName(char *n) {name = n;}<br><br>which then assigned the pointer I was passing to the *name and my entire problem was solved.&nbsp;&nbsp;Obviously, I'm still struggling with pointers and I imagine I'm going to continue to have issues like this.&nbsp;&nbsp;I really appreciate the response!&nbsp;&nbsp;<br><br>A few additional thoughts.&nbsp;&nbsp;One thing is that I obviously didn't include enough code for people to figure out what I was trying to do, is there a limit on how large the post should be?&nbsp;&nbsp;Additionally, are silly questions like I'm going to have for awhile OK to post in this forum?&nbsp;&nbsp;This is the only forum I've been able to find where you can ask questions like this.&nbsp;&nbsp;Another question I have is related to Visual C++ 6.0.&nbsp;&nbsp;I have had a a difficult time learning how to use the tool.&nbsp;&nbsp;I know that it has tons of capabilities that would really assist me in my debugging efforts, but without someone around who really knows it I'm at a loss.&nbsp;&nbsp;The documentation is pretty sketchy and I feel like I'm really underutilizing the program.&nbsp;&nbsp;Does anyone know of any good instruction books or programs to help me learn how to properly use it?&nbsp;&nbsp;
 
I would recomend &quot;Special Edition Using VC++ 6&quot; ISBN: 0-7897-1539-2 to get some basic help with the IDE itself, but for the language of C++, I strongly recomend &quot;C++ from the ground up : second edition&quot; (or whatever newer edition they have, reason being is that it gets into some really advanced topics for most users near the end, yet covers all the regular stuff, and only shows you everything in the Standard, meaning whatever you learn from it can be applied to all compilers. ISBN : 0-07-882405-2<br><br><br>Check <A HREF=" TARGET="_new"> whenever you find a book you want to find pricing on, I've always found bookpool.com to have the lowest prices for any technical books. and shipping is nice. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in , or have messed with : VC++, Borland C++ Builder, VJ++6(starting),VB-Dos, VB1 thru VB6, Delphi 3 pro, Borland C++ 3(DOS), Borland C++ 4.5, HTML, ASP(somewhat), QBasic(least i didnt start with COBOL)
 
Dear moron,<br><br>There are no 'silly questions' only silly answers. Unless of course the answer is from me, then there is really good excuse for the answer being silly, like I had a headache or my dog ate that page of the book, you know things like that.<br><br>-pete
 
To m___n:<br>Be very carefully with objects and pointers when your manipulate them. You are about to do a very common mistake among C++ beginner programmers. You pass to the SetName function a pointer to a char array. Inside SetName function you store that address in your name data member. What is going to happen if the original char array ends its scope or is dynamical freed by you in a different module? Your data member (name) will point garbage and you can have further problems if you allocate new objects and then try to access the old name.<br>A safe way to implement this would be to allocate space for your string inside SetName function, something like this:<br>void Highway::setName(char *n)<br>{<br>&nbsp;&nbsp;&nbsp;if( name != NULL )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete [] name; // pay attention to memory leaks<br>&nbsp;&nbsp;&nbsp;name = new char[ strlen(n) * sizeof(char) ];<br>&nbsp;&nbsp;&nbsp;strcpy(name, n);<br>&nbsp;}<br>Don’t forget to free the space allocated for name in the Highway’s destructor.<br>void Highway::~Highway( void )<br>{<br>&nbsp;&nbsp;&nbsp;if( name != NULL )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete [] name;<br>}<br>Since your using Visual C++ you can start using the CString class because a CString object takes care of all dynamic allocations;<br>A good C++ book which it happens to be free on the Internet is Bruce Eckel's &quot;Thinking in C++&quot;, 2nd ed., downloadable at <A HREF=" TARGET="_new"> .
 
To Karl:<br><br>The declaration:<br>Highway::Highway(char *n, double dist):Length(dist)<br>{<br>&nbsp;&nbsp;&nbsp;setName(n);<br>&nbsp;&nbsp;&nbsp;carCount = 0;<br>&nbsp;&nbsp;&nbsp;width = 0;<br>}<br>It’s not a double function definition within the same scope. It’s just a call to Highway’s base class (Length) constructor.<br>Probably Highway is derived from Length and inherits a data member that stores the distance. That’s being initialized in Length’s constructor. This is called the constructor’s initialization list.<br><br><br><br>
 
Actually Length doesn't have to be the base class of Highway for this to work. It can be a plain old float. The Latest ANSI specification for C++ allows this syntax for initializing variables. You can even do stuff like this:
Code:
float num(0.0);
to declare a float variable named num and initialize it to 0.0 anywhere you could do this :
Code:
float num=0.0;
. This works for all the basic types in C++.
Also, in case you don't want to use MFC, a good old string does the trick as well just
Code:
#include <string.h>
.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top