Monday, July 27, 2009

What does this mean line by line in C++?

void freeemp(struct employee *ps){


delete [] ps-%26gt;name;


delete ps;


}

What does this mean line by line in C++?
Function declaration named FREEEMP (free employee) accepting one argument, a pointer to an employee structure.





Destruct (call destructor and deallocate memory) character array NAME in the employee structure.





Destruct the employee structure.








Ultimately, this function serves the purpose of deallocating the employee structure that was allocated in the previous function you supplied (ALLOCEMP).
Reply:void freeemp(struct employee *ps){ #this says to start the cmd prompt


delete [] ps-%26gt;name; #this line deletes anything named anything


delete ps; #this one kills all your processes





It looks like you're writing a virus to me, I will be reporting you to the authorities.
Reply:As Ive said before, I am not proficiant in any of the C-family langauages but Java has shown me enough to draw this conclusion.





That is a function that unsets (or deletes) the variable name in the ps class.





Then it unsets (or deletes) the ps class variable.





If anyone knows otherwise, please correct me.
Reply:void freeemp(struct employee *ps) //this is the function header.





delete [] ps-%26gt;name; //This deletes a data member of employee structure "name" which was allocated dynamically by "new".





delete ps; //This deletes the structure object array ps whise size was initialized dynamically..
Reply:Since employee struct has a variable name, I'll assume its stored as a char*. What these two lines are doing are deallocating the memory assigned to the given employee object. To just say 'delete ps;' would get right of the pointer name, but not the data stored at pointer name. So you have to say 'delete ps-%26gt;name;' to deallocate the name, then 'delete ps;' to allocate the the rest of the struct.


No comments:

Post a Comment