|
Raggamuffin wrote:
How can I ensure that a program which creates a file is the only one allowed to access that file?
See the dwShareMode param of CreateFile(). Sharemode can be also spec'd in some other functions that wrap CreateFile(). See: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/createfile.asp[^]
Raggamuffin wrote:
And furthermore how can I make windows clean up my temporary files (in case the program crashes or something)?
1) Use C++ and/or Structured Exception Handling (SEH). There are several good articles here on CP about Exception Handling.
2) Use RAII to manage the file handle and simply rely on destructors to cleanup for you. Boost scoped_ptr will do this for you very nicely. See: http://www.boost.org/libs/smart_ptr/scoped_ptr.htm[^]
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
Hiya I have to structure and wanted to make a pointer to it WITH 10 INSTANCES.
So I have:
struct s_Test
{
char name[30];
}
struct s_Test* Test[10];
void main()
{
memcpy( Test[0]->name,"Paul",30 ); // system error here
return;
}
My problem happens when I go to access an element of the structure. I gives me the system error that the app must close.
Any ideas how to access them properly??
Thanks;
|
|
|
|
|
Try to use a std:vector of std:string . That works.
If you really cant use C++ and MUST continue to use C, then this one works:
#include "stdafx.h"
#include < memory.h >
struct s_Test
{
char name[30];
};
int main(int argc, char* argv[])
{
struct s_Test Test[10];
memcpy( Test[0].name,"Paul",30 );
return 0;
}
My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
Meeeep. System error. Copying 30 characters from a buffer which is only 5 characters ("Paul"+zero) long.
-Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
|
|
|
|
|
OOOPS!
But it did compile.....
That type of errors is the reason why I prefer std::vector and std::string .
My opinions may have changed, but not the fact that I am right.
|
|
|
|
|
.. and you rightly deserve an error message!
The variable Test has been defined as a pointer to the structure:
struct s_Test* Test[10]
Therefore the memory location for your structure has not been allocated as yet.
try:
<br />
..<br />
..<br />
struct s_Test Test[10];<br />
<br />
void main()<br />
{<br />
strcpy( Test[0].name, "Paul" ); <br />
return;<br />
}<br />
"Wise men talk because they have something to say; fools, because they have to say something."
Plato
|
|
|
|
|
yup Plato.. what you said is correct ! there the array is infact a array of pointers to "struct s_Test " rather then a array of "struct s_Test ".
"Think big, think fast, think ahead. Ideas are no one's monopoly"
|
|
|
|
|
1) The source buffer for memcpy is too small. It is just 5 characters ("Paul"+zero), but you specify 30. This will result in a memory access error. A valid value for memcpy source len would be strlen("Paul")+1.
2) Declare the Test array as array and not as pointer array. So instead of struct s_Test* Test[10]; do a struct s_Test Test[10];
3) If you have done step 2 you don't need a dereferenced access to the name member variable any more. So the memcpy would look like:
memcpy(Test[0].name,"Paul",strlen("Paul")+1);
-Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
|
|
|
|
|
Thanks for all of those suggestions and help. I know I am being awkward but I really want to get it working using a pointer to the structure. I could do it the normal way no problem but I only want to allocate the memory as I need it. I don't want it to be a static array.
Is it not possible to have a pointer to a structure with instances aswell??
|
|
|
|
|
Sure, this is possible too:
struct s_Test
{
char name[30];
};
s_Test *Test;
void main()
{
Test = new s_Test[10];
memcpy(Test[0].name,"Paul",strlen("Paul")+1);
delete []Test;
Test = NULL;
return;
}
-Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
|
|
|
|
|
If you need a table, you need a table. How do you understand a pointer to a structure with instances as well ? To be able to point at something, you need that the something exist in the memory. It is therefor impossible to have a table without allocating some memory for it.
~RaGE();
|
|
|
|
|
Thanks, have it sorted and know the reason y it can't just do it like that.
thanks for all ur help.
grahamoj
|
|
|
|
|
What you really need is a vector or list of structures but this is a little too complicated to start off with. You can look up vector or list in the help for examples if you want. I assume that you will not know how many structures you will have at compile time and that is why you want a dynamic array. You can create a large array of pointers to your structure. This will take 4 bytes per element. Then allocate your structures when you need them. Have an integer keep track of the current number of structures. Try this:
struct s_Test
{
char name[30];
}
struct s_Test* Test[10];
int nTestCount = 0;
void main()
{
// Add a struct and initialize
Test[nTestCount] = new Test;
strcpy( Test[nTestCount]->name,"Paul");
nTestCount++;
// Add a struct and initialize
Test[nTestCount] = new Test;
strcpy( Test[nTestCount]->name,"John");
nTestCount++;
for(int i=0; i < nTestCount;i++) {
printf("%s\n",Test[i]->name);
}
return;
}
|
|
|
|
|
Hello,
Is an access to a variable with static modifier faster than a normal variable?
I have a recursive function:
void recursiveSolve(char *pszInput, int nItems)
{
int pArray[10];
} The initial contents of pArray doesn't matter, it gets overwritten in all cases. If I declare pArray as static int[], would the recursive function be faster? I don't know, perhaps a static variable has a fixed position in memory and doesn't get allocated again and again when the function calls itself recursively. Do you know?
-Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
|
|
|
|
|
I think so.
Because a saying said that the more space, the less time.
When stored as static varibles the more space is needed (the none-static varibles will be freed when they won't be needed), so I think the function needs less time to run.
Do you think I am right, don't you?
LeonOrient
|
|
|
|
|
I don't know if that saying applies to this problem
-Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
|
|
|
|
|
It shouldn't matter. It would be better to try and optimise using a better algorithm than changing to static variables
Using static variables is a C concept rather than a C++ concept.
It can be very problematic if you have to port the code into a DLL (look at the MSVC6 STL problems with DLL's for the perfect example)
|
|
|
|
|
i am sorry my friend... static variables is not only a concept in C but it is extensibly used in C++ too. infact "static var" concept is not confined to any language or OS. its a generic concept, which is used in many languages and operating systems. there are some solutions, which are best implemented with static concept.
"Think big, think fast, think ahead. Ideas are no one's monopoly"
|
|
|
|
|
BhaskarBora wrote:
static variables is not only a concept in C but it is extensibly used in C++
Perhaps I worded it badly. But being able to do it doesn't mean you should.
If you have to do something like this why are there so many discussion about the 'meyers singleton' pattern and order of destruction. Dynamic allocation of memory through a static pointer can be walking a dangerous road. Especially if you need to control the order of destruction.
|
|
|
|
|
dear friend ! its up to you how you perceive things !
"Think big, think fast, think ahead. Ideas are no one's monopoly"
|
|
|
|
|
hi there .. u r right partly...
all static variables (no matter where they are defined) are not created on stack like local vars. and also static vars are created once for a process/program and remain till the end. this is the reason when your function has static var array defined rather than a local var array, the execution is faster. as static var is created during the program startup and in your case not everytime when you invoke the function (recursive or otherwise). if it would have been a local array then execution would have been little slower as compare to the static array version (as OS will allocate the array on stack everytime you call the function and removes it from stack when function returns).
static var array is shared in all calls to the function. Also, there is a wastage of memory when you are not using the array. to over come this, you can define a 'int pointer' as static and allocate the memory when you need to use the array. when you no longer want the array, relase the allocated memory. this would be a smart solution !
"Think big, think fast, think ahead. Ideas are no one's monopoly"
|
|
|
|
|
Thanks! I will try the static pointer idea!
-Dominik
_outp(0x64, 0xAD);
and
__asm mov al, 0xAD __asm out 0x64, al
do the same... but what do they do??
|
|
|
|
|
Now I have been assigned a task to do a FDD test. During the project I don't know how to make the Floppy-Disk-Device's LED blinking. Could you tell me some solutions to it, please.
Thank you!
LeonOrient
|
|
|
|
|
You cannot access the LED of a floppy disk through software!!! It is an integral part of the floppy disk and it has nothing to do with the operating system. Maybe, you could flash the HDD LED (since the BIOS does it) but not that of a floppy.
// Afterall I realized that even my comment lines have bugs
|
|
|
|
|
Oh, Thanks.
Now I have another question, could you help me?
How can I carry out a Sequential-Verify test on floppy disk in VC?
Thank you very much!
LeonOrient
|
|
|
|