Jump to content
  • Advertisement
Sign in to follow this  
snm04

Help with Pointers

This topic is 4858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I am trying to do this "C plus plus" program for my class. I cannot figure out the right way to make one. Please tell me, what is the right way to do it.
void Employee::setEmployeeInfo(char *fName, char * lName, int deptN, int m, int d, int y)
{
	if( firstName != NULL)
	firstName = NULL;
	
	firstName = new char[ strlen(fName) +1 ];
	strcpy(firstName, fName);

	if( lastName != NULL)
	lastName = NULL;

	lastName = new char[ strlen(lName) +1];
        strcpy(lastName, lName);
	
	deptNumber = deptN;
	Date temp(m, d , y );
	birthDate = temp;
}//end setEmployeeInfo

OR
void Employee::setEmployeeInfo(char *fName, char * lName, int deptN, int m, int d, int y)
{
	firstName = new char[ strlen(fName) +1 ];
	strcpy(firstName, fName);

	lastName = new char[ strlen(lName) +1];
        strcpy(lastName, lName);
	
	deptNumber = deptN;
	Date temp(m, d , y );
	birthDate = temp;
}//end setEmployeeInfo

so, I guess my real question is do I need to check for NULL, and if it is do I set it to NULL or delete it. Thank you

Share this post


Link to post
Share on other sites
Advertisement
Firstly, you should always delete the old value. Otherwise, calling setEmployeeInfo multiple times will cause memory leaks.

Secondly, you should check for NULLs, since doing strlen() on a null pointer is an error. Whether you set the string in the employee data to NULL or not (leaving the old string untouched -- don't delete it in this case) is up to you, depending on the program's specifications.

Thirdly, you say you're using C++. Why are you not using the standard C++ string class instead of the C-string manipulation functions?

Fourthly, you're allocating memory in your object. I hope for your sake that you have properly a defined destructor, a copy constructor and an assignment operator.

Share this post


Link to post
Share on other sites
Simply changing the value of a pointer (to NULL or 0 or whatever) doesn't do anything except change what the pointer points to.

If I understand what you are trying to do, you probably should be using delete instead setting the value of the pointer.

Two related points:
  1. Using delete with a pointer whose value is 0 does nothing, so there is no need to check if it is 0 before deleting.
  2. Setting a pointer to 0 after delete is frequently a good idea. It prevents you from accidently deleting the same allocation twice. However, in this case since you would immediately set the pointer to a new value, setting it to 0 doesn't do anything meaningful.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Thirdly, you say you're using C++. Why are you not using the standard C++ string class instead of the C-string manipulation functions?


Presumably because he's doing it "for his class" (pun very much intended). The education system really sucks at this stuff.

Chances are good this should really be a constructor anyway :/ (how can it ever make sense to change the name or birthdate of an already existing Employee? If there *are* such exceptional cases, do you really want to have to do them all at once again? Chances are good this is normally called once to initialize Employees, and that is defeating much of the purpose of the language.)

By the way, you don't need to declare a temporary variable in order to do an assignment. A constructor call is an expression, and the result of an expression can be assigned. Then again, if we in turn change setEmployeeInfo into a constructor, we should use the initializer list.

Putting all of that together, it should look like:


#include <string>
class Employee {
//...
public:
// ...
Employee(const std::string& firstName, const std::string& lastName,
int deptNumber, int birthMonth, int birthDay, int birthYear) :
firstName(firstName), lastName(lastName), deptNumber(deptNumber),
birthDate(Date(birthMonth, birthDay, birthYear)) {}
// ...
}


I would go so far as to say that if this looks unfamiliar to you at all, your teacher/professor effectively is not teaching you C++, and needs a good talking-to on the subject :/

Share this post


Link to post
Share on other sites
Thankyou everyone for help. I know I could have used the String class but the assignment specifications say to to use char pointers to store employee data so we get more practice with pointers.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!