|
In my old "C" days I used zero sized arrays as placeholders for dynamic sized structs. Something like this:
<code>typedef struct _VARIABLE_SIZED_STRUCT {
BYTE data[0];
} VARIABLE_SIZED_STRUCT, *PVARIABLE_SIZED_STRUCT;
PVARIABLE_SIZED_STRUCT pDynamic = (PVARIABLE_SIZED_STRUCT)malloc(FIELD_OFFSET(VARIABLE_SIZED_STRUCT,data[iSize]));
The zero sized placeholder is antiquated and no longer needed with the C++ language. In fact I believe support for zero sized array was removed from recent MSVC compilers.
Best Wishes,
-David Delaune
|
|
|
|
|
Visual Studio 2010 will flag an error for int i[0];
You can however create it dynamically - int* i = new i[0];
|
|
|
|
|
As Superman already wrote, declaring an array with zero size is not allowed, at least on Microsoft Visual C++.
Something interesting is that, on versions prior to Visual Studio 2010, which is the first one that implements static_assert , such functionality was realized using the _STATIC_ASSERT Macro[^].
That macro tests at compile time for the given condition and give the Compiler Error C2466[^] if not satisfied. Ops! C2466 means cannot allocate an array of constant size 0.
By going deeper, we can search the macro on the crtdbg.h header:
#define _STATIC_ASSERT(expr) typedef char __static_assert_t[ (expr) ]
Now, let's try to imagine how it works...
|
|
|
|
|
You can in C99 (I think VC++ supports this syntax in structures if you use /Ze but it's not a full C99 flexible array thing) but you can't in C90 or C++. The sort of thing it's useful for in C99 is for arrays with variable sized structures. You can write:
struct foo
{
int number_of_elements_;
int elements_[];
};
and then have the constructor sort out sizing the array. In C90 and C++ you'd have to have one element and manually size everything.
Cheers,
Ash
|
|
|
|
|
Hello everyone.
I am doing a sync program that has a spying function.
The spying function will detect any changes in the Source folder and will sync it with the Destination folder as soon as detects any changes.
My problem is, when a new file is added into the Source folder, the sync function immediately will start copying the new file to the Destination folder. And, if the file is still being used by explorer for writing, the program gives an error. As the file has not been fully written.
How can I make the sync function wait for the explorer, until it finishes copying the file?
modified on Thursday, November 25, 2010 8:01 PM
|
|
|
|
|
I know the problem, and there isn't a perfect solution AFAIK. What I tend to do, whenever possible, is having the producer produce another very small file once the actual files have been handled, and use that as a signal. This of course only works when I have some say it what the producer does. When I don't, the only thing I have is polling (a wait-and-try loop).
|
|
|
|
|
mints555 wrote: And, if the file is still being used by explorer for writing, the program gives an error.
I would recommend that you attempt to open the file with exclusive access when copying. The OS should deny your request if the handle is being referenced by another process.
Best Wishes,
-David Delaune
|
|
|
|
|
Thanks for the advice David.
I solved the problem by the code shown below:
CFile sour;
CString source=_T("Path to file");
while (!sour.Open(source,CFile::modeRead | CFile::shareDenyNone,NULL))
{
Sleep(1000);
}
sour.Close();
modified on Thursday, November 25, 2010 3:34 AM
|
|
|
|
|
How could I find out where a runaway process is looping? I can attach to the process using the VS debugger but not clear where to go from there. (Also, I tried to get a dump at that point but got a permanent hourglass and had to kill the VS session.) Or is there a way at some higher level to kill the process and cause it to take a crash dump that could later be analyzed?
|
|
|
|
|
Alan Kurlansky wrote: How could I find out where a runaway process is looping?
Interesting choice of words. It contains the WinDbg command that could help your situation.
If you attach the WinDbg debugger you can issue the !runaway command which will show you the thread times. Any thread in an infinite loop will probably consume the most cpu time.
You may also want to try "!locks" and "!analyze -v -hang" in case its not an infinite loop and actually a deadlock caused by synchronization objects.
Off Topic: I really wish the VS team would add a commandline interface into the VS debugger.
Best Wishes,
-David Delaune
|
|
|
|
|
Hello Friends
I m using two ListBox in a dialog.I want to know that how can i get notification If i select member of Second List Box and to unselect the first listbox.
Thanks & Regards
Yogesh
|
|
|
|
|
Can't you just handle the ON_LBN_SELCHANGE notification?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hello,Thanks for the reply.
I am using ON_LBN_SELCHANGE. but with that i am using LButtonDown where do i need to know which listbox is selected.And whenever i change selection from one listbox to another.then first LButtonDown is called.And I think ON_LBN_SELCHANGE is the notification If i change the selection of strings in One Listbox.
I want to know when I click on other listbox and I want to deselect all the strings of first listbox.
Regards
Yogesh
|
|
|
|
|
Maybe I din't get you, but the notification should be mapped to two different handlers, one for each listbox (each handler handles -pardon the pun- just the 'selection change' coming from its listbox).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thx,I used ON_LBN_SETFOCUS to get focus of one listbox.but now i want the handle of those listbox on lBUttonDown.
I used
CWnd* pWndMlb = GetDlgItem(IDC_LISTBOX);
CWnd* pWndLlb = GetDlgItem(IDC_LISTBOX_TEMP);
bur both re coming NULL.
Regards
Yogesh
|
|
|
|
|
Are you sure IDC_LISTBOX , IDC_LISTBOX_TEMP are child controls of the window from where you're invoking GetDlgItem ?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
|
Could you post the relevant code, please?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
modified on Wednesday, November 24, 2010 8:24 AM
|
|
|
|
|
Hello Sir
I got the HAndle of tht ListBox in Lbutton Down.
Thanks for ur precious time .
Regards
Yogesh
|
|
|
|
|
Hi,
I pass two input string from VBA(forms) to (Vc++)DLL. This vc++ DLL is concatinate the string and return to VBA Forms.
please any sample code or idea?
Regards,
M.mathivanan
|
|
|
|
|
|
Unless you are doing some other string manipulation other than just concatenating two strings, why don't you just concatenate them within the VB app?
It was broke, so I fixed it.
|
|
|
|
|
I agree, Excel had its own C compiler so even for speed advantages I cant see the point of calling to vc++ to concatenate two strings! If it was another datatype that would be a different story.
|
|
|
|
|
Hi all,
I have a working program that reads stocks symbols and values from a text file and do some operations on the data, and then it outputs the the data as two reports. The first report is sorted by stock symbols, and the other report is sorted by percent gain/loss.
I'm having difficulties understating how these sort functions are doing the comparisons..
Can you guys provide some explanation please?
template<class elemType>
void listType<elemType>::sort(){
int i, j;
int min;
elemType temp;
for (i = 0; i <length; i++){
min = i;
for (j = i + 1; j < length; ++j)
if (list[j] < list[min])
min = j;
temp = list[i];
list[i] = list[min];
list[min] = temp;
}
}
void stockListType::sortByGain(){
int i, j;
int temp;
indexByGain = new int[length];
for(int c = 0; c < length; c++){
indexByGain[c] = c;
}
for (i = 0; i <length; i++){
for (j = i + 1; j < length; j++){
if (list[indexByGain[j]].getPercentChange() > list[indexByGain[i]].getPercentChange()){
temp = indexByGain[i];
indexByGain[i] = indexByGain[j];
indexByGain[j] = temp;
}
}
}
}
|
|
|
|
|
The algorithm is something like this -
1. Take the first element in the array.
2. Compare with the rest of the elements below it to check if there is a lesser value.
3. If there is a lesser value, swap the elements.
4. Take the next element in the array.
5. Go to step 2.
The std::sort function would be more efficient if you can use it.
|
|
|
|