|
Hello in my code i have this
double idf[50];
for (int zz = 0 ; zz < m_QueryWords ; zz++ )
{
idf[zz] = log10 ( (double)(nDoc/df[zz]) );
-> }
While i debug i have :
<code>name Value
df[zz] 2
idf[zz] 0.00000 (red colour just took the value)
nDoc 3
+this etc :P
zz 0 </code>
what i do wrong ?
I changed the code with
int location = f + (m_QueryWords*ii);
SCs = SCs + nArray.GetAt(location)*idf[f]*idf[f] ;
and now works
-- modified at 14:32 Saturday 2nd June, 2007
|
|
|
|
|
Immunity18 wrote: idf[zz] = log10 ( (double)(nDoc/df[zz]) );
Change to log10 ( (nDoc/(double)df[zz]) );
df[zz] needs to be a double in order to provide a floating point result.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I posted a question before and got hint of "managed/unmanaged code".
I do heard "Managed C++", I guess it is another language - at least it is different from c++ hear.
my question is:
how to generate "managed code" in our C++, such as in MFC or Win32?
any samples if it is possible?
thx
|
|
|
|
|
Hope this article will help you.
Managed Code[^]
Regards,
Satips.
|
|
|
|
|
...or this article
Managed Extensions for C++ Programming[^]
It's not another language. It's an extension to Microsoft's C++ which allows you to use the
Common Language Runtime and the .NET framework.
Managed code is not compiled to processor machine code like regular C++ is. It is compiled to
an intermediate language that can run on an implementation of the Common Language Infrastructure
(CLI). At runtime, the intermediate language code is compiled to CPU-specific machine code and
executed. On a Windows machine with the .NET framework installed, this is done by the Common
Language Runtime (CLR), which is Microsoft's implementation of CLI.
In order for this to be possible from C++, it was necessary to extend the language to be able to
work with the common data types of the CLI specification and to work with the managed, garbage-
collected heap of the .NET framework.
The biggest impact IMO with managed C++ programming is with managed pointers. Objects allocated
on the managed heap can be moved at any time by the runtime. Objects allocated with malloc/new
don't move - that's what we're used to as C++ programmers. Also, managed objects are
automatically destroyed by the runtime.This means there's rules to follow when mixing managed and
unmanaged C++ code (mixed-mode programming).
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
//global
long i_Lock=0;
//use it in thread
int MyTread(void*p)
{
InterlockedIncrement(&i_Lock);
}
--------------------------
I am really confused, what is difference with
int MyTread(void*p)
{
i_Lock++;
}
???
it sounds nothing related to sinycronization, could u explain?
thx
|
|
|
|
|
The InterlockedIncrement() version is thread safe, the i_Lock++ version is not.
InterlockedIncrement() prevents any other thread from using i_Lock while it's being incremented.
InterlockedIncrement() also checks the result and gives a return value based on the result.
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
includeh10 wrote: it sounds nothing related to sinycronization, could u explain?
sure it is !
you MUST lock a resource that can be modified by several threads at the same time, so that no data loss is possible.
imagine you do i_Lock++ as you suggest, but at the same time, another thread does i_Lock-- , one of the 2 operations will be done for nothing...
at
|
|
|
|
|
my test code is as following:
the thread has been created for 5 times by CreateThread(), so I have 5 threads:
//---------- code
long i_Lock=0;
long i_Thread=0;
int WINAPI ZDlg::Thread_Simple(void*pv)
{
if(i_Thread==0)
{
::InterlockedIncrement(&i_Lock);
}
else
{
i_Lock++;
}
i_Thread++;
int iNum=0;
while(iNum<5)
{
iNum++;
Sleep(1000);
}
return 0;
}
//--------------------
so only first thread calls InterlockedIncrement(&i_Lock);
and other 4 call i_Lock++;
if i_Lock is really locked, its value must be 1.
but when I test the value in while() loops, it is 5.
so i_Lock++ works and i_Lock is not locked!
is something wrong?
|
|
|
|
|
How can it lock if you don't use InterlockedIncrement() for all cases? It can't.
The system has no way of knowing if you are changing the variable directly.
You have to use InterlockedIncrement() every place you increment the variable for it to work.
The way you've coded your thread, all 5 threads are going to increment the value regardless.
InterlockedIncrement() ensures the increment won't happen at the same time but it's still
going to happen as soon as another thread is done incrementing it.
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
1) do you mean : if first thread calls InterlockedIncrement(&i_Lock) first, then second thread calls i_Lock++;
the i_Lock++ still works?
2) what does "same time" mean?
do u mean: if InterlockedIncrement(&i_Lock); are called at "same time" by two threads, only one call works?
but, a PC has only one CPU, it can not change a value at "same time" by 2 threads in any way (++ or InterlockedIncrement).
|
|
|
|
|
includeh10 wrote: 1) do you mean : if first thread calls InterlockedIncrement(&i_Lock) first, then second thread calls i_Lock++;
the i_Lock++ still works?
Yes. All the threads incrementinging i_Lock should use InterlockedIncrement() to do so. That's
the only way the operation will be thread safe.
includeh10 wrote: 2) what does "same time" mean?
do u mean: if InterlockedIncrement(&i_Lock); are called at "same time" by two threads, only one call works?
but, a PC has only one CPU, it can not change a value at "same time" by 2 threads in any way (++ or InterlockedIncrement).
While it's true it won't occur at the "same time" on a single-processor machine, it's best to
assume that logically it can. You have no control over when the system is going to switch threads.
If the switch occurs right in the middle of a thread changing the variable and another thread
takes over and changes that variable then the first thread resumes but the variable has changed.
That can be bad.
In the simple case of incrementing a long, chances are pretty good these days that it will take
a single machine-code instruction to do the increment. Theoretically one wouldn't need
to use thread synchronization because the active thread isn't going to change in the middle
of a CPU instruction.
InterlockedIncrement() does more than just increment though. It also checks the result and
returns the result. Now imagine doing all those operations without thread synchronization:
1) i_Lock = 0
2) Thread A increments i_Lock - i_Lock now == 1
3) Thread B becomes the executing thread
4) Thread B increments i_Lock - i_Lock now == 2
5) Thread B gets the value of i_Lock (2)
6) Thread A becomes the executing thread
7) Thread A gets the value of i_Lock (2)
i_Lock should have been 1 in thread A but thread B changed it before thread A was finished.
Now do the same with proper synchronization:
1) i_Lock = 0
2) Thread A calls InterlockedIncrement() to increment i_Lock
3) Thread B becomes the executing thread
4) Thread B calls InterlockedIncrement() to increment i_Lock
5) Thread B is put into a waiting state (paused) until Thread A's call to InterlockedIncrement()
is completed
6) Thread A becomes the executing thread
7) Thread A's call to InterlockedIncrement() completes, returning 1 (i_Lock == 1)
8) Thread B becomes the executing thread
9) Thread B's call to InterlockedIncrement() is allowed to continue
10) Thread B's call to InterlockedIncrement() completes, returning 2 (i_Lock == 2)
That's the expected behavior - first-come, first-serve Make sense?
InterlockedIncrement() and InterlockedDeccrement() are often used for object reference count
variables. When an object's reference count is 0, often the object should be destroyed.
Imagine not synchronizing this process (as described above). Two threads try to decrement the
ref count on an object, but both think the ref count is 0 after they have done so. Both threads
try to destroy the object - one succeeds, one crashes.
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|
|
Hai, i have an Access Control System which has a client server architecture, It supoports data tranfer to third part by a connectivity called SAGE. The manufacturer supplied all dlls and header files. but when i include these header files in my VC++ project to access Data from the system, i get Fatal Errors, cannot include the header files. What could be the problem. Can any one help please.. my mail id mahesh.mudackal@gmail.com
|
|
|
|
|
mahesh.mundackal wrote: but when i include these header files in my VC++ project to access Data from the system, i get Fatal Errors, cannot include the header files.
What errors?
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
mahesh.mundackal wrote: i get Fatal Errors, cannot include the header files. What could be the problem.
Hint, hint. Linker is unable to find the header files. check your paths.
|
|
|
|
|
what errors? and can you show them.
|
|
|
|
|
Hi all,
I have created an SDI application and 3 dockable toolbars within them. But the problem is these toolbars are appearing in 3 different lines being left aligned. I want them to be in a single line unless the first line is filled.
Is there is any way to align the tool bar just after creation.
Anurag Gandhi.
|
|
|
|
|
Dear,
I want to insert an ActiveX control of DIO card in SDI Application. Previously I tried this in Dialog based application it worked. But when I inserted it in the SDI application. It Compiled with no errors but when I RUN it this is givig some error.
Plz any one of u have solution how to insert an ActiveX control in SDI plz send it.
Murali.S
|
|
|
|
|
Murali_mom wrote: this is givig some error.
Please wait until I search my telescope, so that I can look at your monitor to read those errors.
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
Plz find some solution I need it badly
|
|
|
|
|
brahmma wrote: Please wait until I search my telescope, so that I can look at your monitor to read those errors.
Maybe dont need to telescope ,like advanced tools (did you see Ring I,II).
|
|
|
|
|
Can you more explain about errors?
|
|
|
|
|
I have a program,using thirdpart code.(there are some C codes in it)
It works when I debug it step by step,but it would crash if I run it weather in release or debug mode.
I found that there are some exceptions occur.But the most strange thing is that the exception cound not be catched using CException but only be catched if I use catch(...).
Someone would tell me why?
BEST REGARDS
|
|
|
|
|
You are probably stomping on memory that you do not own. Can you post relevant portion of the code where the crash occurs?
Nobody can give you wiser advice than yourself. - Cicero
|
|
|
|
|
OK,it very kind of you.
But if you give me your e-mail,that I can send the codes for you?
My E-mail is:kcynic@163.com
Thanks.
|
|
|
|