|
This is what MSDN says about it:
Notice the difference between deleting an element's object and removing the element itself. Removing an element from the list merely removes the list's reference to the object. The object still exists in memory. When you delete an object, it ceases to exist and its memory is reclaimed. Thus, it is important to remove an element immediately after the element's object has been deleted so that the list won't try to access objects that no longer exist.
CArray<CPerson*, CPerson*> myArray;
<font color=blue>int</font> i = 0;
<font color=blue>while</font> (i < myArray.GetSize() )
{
<font color=blue>delete</font> myArray.GetAt( i++ );
}
myArray.RemoveAll();
Owner drawn
Jesus Loves
|
|
|
|
|
Objects are usualy responsible for freeing the memory they use. If your CArray is storing objects, then they will free the memory when they are removed. But if the CArray is only storing pointers to objects, then you are responsible for freeing the memory [associated with each object]. The reason for this is that the array object does not actualy know whether it is storing an object or a pointer to an object.
INTP
Every thing is relative...
|
|
|
|
|
consider the small situation:-
funcFCC()
{
ClassA a;
}
here the object is destroyed when the function scope end....
funcFCC()
{
ClassA *a=new ClassA;
}
here the Memory is not freed when scope of function end.. resulted in memory leak...
same apply for the RemoveAll function.. hope you understand this!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
thanks for all the responses,
please look at this code:
void COtherClass::FirstFunction(CArray<CSomeClass, CSomeClass&> & pSomeClassArray)
{
CSomeClass objSomeClass;
pSomeClassArra.Add(objSomeClass);
......
}
void COtherClass::SecondFunction()
{
// m_objSomeClassArray is a member variable array of COtherClass (Type CSomeClass)
FirstFunction(&m_objSomeClassArray);
......
m_objSomeClassArray.RemoveAll();
}
is this OK?
|
|
|
|
|
Kleser wrote:
void COtherClass::FirstFunction(CArray<CSomeClass, CSomeClass&> & pSomeClassArray)
{
CSomeClass objSomeClass;
pSomeClassArra.Add(objSomeClass);
......
}
void COtherClass::SecondFunction()
{
FirstFunction(&m_objSomeClassArray);
......
m_objSomeClassArray.RemoveAll();
}
Is CSomeClass implement the copy constructor?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
i made a mistake writing the code above. it has to look like this:
void COtherClass::FirstFunction(CArray<CSomeClass, CSomeClass&>* pSomeClassArray)
{
....
}
instead of this:
void COtherClass::FirstFunction(CArray<CSomeClass, CSomeClass&>& pSomeClassArray)
{
....
}
the method has a pointer parameter instead of a reference parameter.
i guess CSomeClass does not implement a copy constructor, i have not added anything in the constructor neither write a new constructor in that class.
i use the class the way it is as i show you before, and it seems to work.
but i want to know how to implement a copy constructor, can you please show me how to do it?
thanks in advance
|
|
|
|
|
Hi, i have a problem with this code i am working on. It does compile correctly but when i input my data file, which is a string of integers it does not seem to work.. pls advice if possible
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main(){
FILE*scores;
float s,y,z,mean,deviation,m,n,A,B,C,D,F;
int k,a,b,c,d,f,q;
scores=fopen("scores.txt","r");
a=b=c=d=f=k=q=0;
s=y=z=mean=deviation=m=n=A=B=C=D=F=0;
while(fscanf(scores,"%f",&s)!=EOF){
y+=s;
z+=(pow(s,2));
k++;
if(90<=s<=100){
a++;
}else if(80<=s<=89.9){
b++;
}else if(70<=s<=79.9){
c++;
}else if(60<=s<=69.9){
d++;
}else{
f++;
}
}
printf("%d\t%d\t%d\t%d\t%d\n",a,b,c,d,f);
mean=y/k;
printf("The average score is %.1f.\n",mean);
m=z/k;
n=pow(mean,2);
deviation=sqrt(m-n);
printf("The standard deviation is %.1f.\n",deviation);
A=a/k*100;
B=b/k*100;
C=c/k*100;
D=d/k*100;
F=f/k*100;
printf("\nA\t%d\t%4.1f\t",a,A);
for(q=1;q<=a;q++){
printf("*");
}
printf("\nB\t%d\t%4.1f\t",b,B);
for(q=1;q<=b;q++){
printf("*");
}
printf("\nC\t%d\t%4.1f\t",c,C);
for(q=1;q<=c;q++){
printf("*");
}
printf("\nD\t%d\t%4.1f\t",d,D);
for(q=1;q<=d;q++){
printf("*");
}
printf("\nF\t%d\t%4.1f\t",f,F);
for(q=1;q<=d;q++){
printf("*");
}
printf("\n");
This code is suppose to calculate mean, std deviation and print a bar chart to represent the distribution.
|
|
|
|
|
scorpiotkh wrote: if(90<=s<=100){
a++;
}else if(80<=s<=89.9){
b++;
}else if(70<=s<=79.9){
c++;
}else if(60<=s<=69.9){
d++;
}else{
f++;
}
Do you see the problem with this? It should instead look like:
if (90.0 <= && s <= 100.0)
a++;
else if (80.0 <= s && s <= 89.9)
b++;
...
scorpiotkh wrote: ...it does not seem to work...
Which means nothing useful. What exactly is not working?
Not that it will correct the problem, but change float to double , and use floating-point constants (e.g., 0.0).
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli
|
|
|
|
|
It does not really matter what exactly is wrong with this code. This code in itself is wrong. The code looks like it's a copy from one of books on "how NOT to write programs", or "how to write programs so that they do NOT work". I wonder where Scorpio got it, isn't it copyrighted?
|
|
|
|
|
I really have no idea what is the purpose/aim for your sarcasm here. All i request here are constructive comments on how to make this code work to get the desired grade to be printed out when integer values are entered.
As far as i am concern, i am new to programming and i did refer to books and tutorials to get this code up. I would politely request that if you have not have any contructive/helpful comments to add, please do not post in here.
Thanks once again for taking time to read.
|
|
|
|
|
Basically i need help with the input, the program will return a grade when an integer value is entered, but i am not getting that when a value is entered.
Moreover when i opened the textfile, the scores which i input was actually 1 whole sting of numbers all stuck together.
|
|
|
|
|
scorpiotkh wrote: ...the program will return a grade when an integer value is entered...
Entered? I do not see anything in your code that would facilitate this (e.g., scanf , gets ).
scorpiotkh wrote: ...but i am not getting that when a value is entered.
What sort of value?
scorpiotkh wrote: Moreover when i opened the textfile, the scores which i input was actually 1 whole sting of numbers all stuck together.
Your input file should look something like:
12.34
56.78
1.09
...
"The greatest good you can do for another is not just to share your riches but to reveal to him his own." - Benjamin Disraeli
|
|
|
|
|
If I have a class CTest, and have provided my own constructor (with zero parameters), I would expect that the compiler (VC++ 6.0 SP5 and VC++ 7.1) would not create the default constructor. However, my constructor only gets called with a statement like...
CTest test;
When I try and instantiate an object of CTest with a statement like...
CTest test();
my constructor never gets called but the program executes and the compiler does not complain. I have an explicit copy constructor as well and it does not come into play as I entertained this possibility for 10 seconds or so. This begs the question, "What is the difference between the two methods of instantiating an object"?
Am I missing something elemetary here?
|
|
|
|
|
bob16972 wrote: CTest test();
That isn't a variable declaration, it's a prototype for a function called "test" that returns a CTest and takes no parameters.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
|
|
|
|
|
Fair enough.
Thanks. Sorry I didn't catch that earlier
|
|
|
|
|
Michael Dunn answered your question, but ponder this.
Whether you create the constructor or the compiler creates it for you, it is an implicit constructor. Why are explicit constructors needed? (C++ keyword: explicit)
INTP
Every thing is relative...
|
|
|
|
|
Your right. My apologees for using the word "Explicit" and not knowing it was a keyword.
I had to look that up in Meyers Effective C++ (on page 5 so now I'm really embarrassed) though as I've never used that keyword before. If I'm understanding Meyers correctly, is it to prevent implicit casts from being performed?
I have been using C++ for years but I'm just now building my first polymorphic classes and it's been interesting to see how much of this language I have gotten by NOT knowing all this time. I think learning MFC corrupted me and allowed me to get by without needing to know too much. I'm pretty much starting over and learning the language hopefully the right way this time around.
Thanks for the correction.
|
|
|
|
|
Welcome brother!
I went straight from C coding to using MFC, and [yah] it corrupted me too. My best code [my opinion] was always the code that did not depend on MFC or even Windows.
On my own time I started working on standard C++ code, utilizing STL and building templates, amongst other things. If you really want to learn the details, try writing a complex C++ template class. Meyers is an excellent reference, which I have used myself.
The most important method used by the STL [in my opinion] is swap(), know why and how to implement it as a method guaranteed not to throw.
Good luck!
INTP
Every thing is relative...
|
|
|
|
|
Thanks again for the advice.
John R. Shaw wrote: On my own time
That's about the phase I'm in now. Using MFC at work and bound and determined to finally learn how to write my own templates and finally embrace the STL on my own time. I wish I would've done this years ago but I guess that's how life turns out sometimes.
Take Care.
|
|
|
|
|
I keep receiving an unexplained error when I use the CSocket class in a multithreaded program. The error is in the socketcore.cpp file:
BOOL CSocket::PumpMessages(UINT uStopFlag)<br />
{<br />
ASSERT(m_pbBlocking == NULL);<br />
<br />
_AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;<br />
<br />
ASSERT(pState->m_hSocketWindow != NULL); <br />
<br />
.<br />
.<br />
.<br />
.<br />
}
I have read several articles regarding the reliability of the CSocket class in multithreaded applications. I just wanted to know if anyone here had any opinions on the subject or a solution to this particular problem.
Thanks!
-- modified at 14:01 Thursday 16th February, 2006
|
|
|
|
|
Call AfxSocketInit() in each thread that is using sockets.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
I have a class member CSocket variable that is used in several class member functions. AfxSocketInit() is first called in the app's InitInstance() . Should I also call it in my class constructor?
The problem is that the app will receive half of the file sent by a TCP server and then generate the error. The size of the file doesn't make a difference!
|
|
|
|
|
masnu wrote: Should I also call it in my class constructor?
No, you need to call it at the beginning of any thread that uses sockets.
Ryan "Punctuality is only a virtue for those who aren't smart enough to think of good excuses for being late" John Nichol "Point Of Impact"
|
|
|
|
|
Hi,
I have been trying to construct a simple server client system just to come to grips with TCP/IP programming. It has a authentication system where the protocol goes something like...
server: "AUTH"
client: "AUTH:<user>:<pass>"
server:"AUTH:SUCCESS" or "AUTH:FAILURE" depending on result
server may follow the "AUTH:FAILURE" with an "EXIT" to break the connection in certain conditions. however when i try to send the "AUTH:FAILURE" and "EXIT" strings in succession, i land up receiving "AUTH:FAILUREEXIT" at the client which then messes up things. I have tried disabling the Nagle's algorithm but the problem still persists.
Any pointers on how i could solve this?one way is to introduce seperators between two send()s but i would like to know of any methods to get by without seperators.
Thanks!
clueso
|
|
|
|
|
Nagle-ing might not have anything to do with it. The Nagle algorithm works to improve the performance of a send operation. But the intermediate network equipment as well as the receving system are free to combine or break apart data as they see fit - TCP sockets are stream sockets, and as such, data will stream into and out of them.
Moving to a datagram socket will prevent this, but you lose the automatic guarnanteed delivery you get with TCP.
I would suggest better parsing (watch for keywords made of combined data segments), change the format of the messages (e.g. each one starts with 0x01 and ends with 0xFF ), or change to a request-ack system, where each command gets an ACK back before the next one is sent.
Peace!
-=- James If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! DeleteFXPFiles & CheckFavorites (Please rate this post!)
|
|
|
|