Help - Search - Members - Calendar
Full Version: [C++] Few questions
Neowin Forums > Help & Discussion Center > Programming (C#, C++, JAVA, VB, .NET etc.)
Xinok
- Is it possible to set up references to arrays? ex:
int array[15];
int& var = array;
var[5] = 0;
// Just an example...

- Is it possible to trigger a thread if the function is a member of a class? ex:

class MyClass{
void funct(void* var); // trigger a thread using this function
}
A friend told me its impossible, just want to be absolutely sure.

- If the previous is impossible, then I would like to know a good method for doing this:
Triggering a thread which can also access the private members of a class, using references or pointers if needed.

Edit: One more question I forgot to ask...

- Can I preallocate files without having to write data to the harddrive first? (e.g. Allocate a 300mb file without having to write 300mb of data to the HD) If its complicated, it might be best not to explain...
zeta
What do you mean by "triggering a thread"?

For the array question:
CODE

int array[15];
int *var = array;
var[5] = 0;


I'm not sure about the last question, depending on the OS and FS, you could try using sparse files...
Xinok
I guess "creating a thread" is a better way to put it. That is using a function like _beginthread().

I'm using XP SP2 and NTFS.
zeta
You should be able to create a thread with a class function if it's declared static, I think. However, I'm not sure about accessing private members... perhaps you should just use the getter/setter functions defined on the class?
kjordan2001
Well, in Linux I know the only way to use something like pthreads is to make the function you want to execute static. Not sure if this applies to Windows. Last one I'm not sure about...I know like some BT clients allocate HD space, but I'm not sure if they actually write the data or if it's just allocating that much and doesn't have to write anything (but I do remember it does take a little to allocate the space, so maybe it does write data).
Xinok
QUOTE(zeta @ Jun 28 2005, 12:42)
For the array question:
CODE

int array[15];
int *var = array;
var[5] = 0;
[right][snapback]586132991[/snapback][/right]
Not quite what I'm hoping for. I was already aware how to do it with pointers. I want to know if its possible with references because their address is constant, so the code is faster.
Sucks to know that threads in classes are rather limited sad.gif
I'll see what I can find out about 'allocating' files in C++, but if anyone has any more info on this, please speak up.
kjordan2001
QUOTE(xinok @ Jun 28 2005, 12:28)
Not quite what I'm hoping for. I was already aware how to do it with pointers. I want to know if its possible with references because their address is constant, so the code is faster.
Sucks to know that threads in classes are rather limited sad.gif
I'll see what I can find out about 'allocating' files in C++, but if anyone has any more info on this, please speak up.
[right][snapback]586133475[/snapback][/right]

Well, you can always do a:
int array[15];
const int *var = array;
var[5] = 0;
Xinok
QUOTE(kjordan2001 @ Jun 28 2005, 14:40)
Well, you can always do a:
int array[15];
const int *var = array;
var[5] = 0;
[right][snapback]586133539[/snapback][/right]
Ah cool, I never knew you could make a pointer constant.

That'll do for all my questions, thanks for the help yes.gif
kjordan2001
QUOTE(xinok @ Jun 28 2005, 12:44)
Ah cool, I never knew you could make a pointer constant.

That'll do for all my questions, thanks for the help yes.gif
[right][snapback]586133564[/snapback][/right]

Yep, const pointers are pretty handy for arguments as well.
Oogle
QUOTE(xinok @ Jun 28 2005, 08:05)
- If the previous is impossible, then I would like to know a good method for doing this:
Triggering a thread which can also access the private members of a class, using references or pointers if needed.
[right][snapback]586132836[/snapback][/right]

Do something like the following...
CODE
class CThreadClass
{
public:
   bool StartThread();

private:
   int Run();
   static int ThreadProc(void* pData);

   int a, b, c;
};

bool CThreadClass::StartThread()
{
   ...
   _beginthreadex(..., ThreadProc, this, ...);
   ...
}

int CThreadClass::Run()
{
   // Whatever private members you wanna modify
}

int CThreadClass::ThreadProc(void* pData)
{
   CThreadClass* pClass = reinterpret_cast<CThreadClass*>(pData);
   return pClass->Run();
}

Now, you'll have a CThreadClass that can spawn a thread. That thread that can access member variables via the Run method.
Xinok
Thanks for that Oogle.

I want to bring back up the topic about allocating files...
I was finally able to find a function, SetFileValidData(). Link on MSDN
First, I'm not even sure if this is the function I'm looking for. Reading through the descriptions, I'm not exactly sure what its supposed to do.
Second, I'm using VC++ 6, and this function is defined nowhere in the files. It should be in windows.h.

So... any ideas?
Oogle
SetFileValidData is XP only. That means that you need to set #defines to tell the compiler to compile for XP only.

Instead, use SetFilePointer to set your desired file size. Then call SetEndOfFile to make the OS create the actual file with that size. Obviously, all the bytes created will be uninitialized (i.e. contain random garbage).
Xinok
Thanks again Oogle, the functions seem to work fine.
zeta
You could also try using the undocumented NtCreateFile function--apparently, it has a AllocationSize argument. I've never used it =P
Andareed
Call SetFilePointer to your target file size (it's not an error to set file pointer past eof). Then call SetEndOfFile.

NtCreateFile is officially documented in MSDN. The best source of Nt* functions is ReactOS though.
Xinok
Don't worry people, I got it tongue.gif

I put together this function for myself. It'll create the file if it doesn't already exist, and can resize existing files without losing any of the original data. (Well, it'll lose data at the end of the file if you shrink it, obviously...)
CODE
int AllocateFile(char* ifile, __int64 size){
if(size < 0) return -1;
__int64 freediskspace;
GetDiskFreeSpaceEx(ifile, (_ULARGE_INTEGER*)&freediskspace, NULL, NULL);
if(size >= freediskspace) return -2;
OFSTRUCT filedata;
HANDLE file;
if(OpenFile(ifile, &filedata, OF_EXIST) == HFILE_ERROR)
 file = (HANDLE) OpenFile(ifile, &filedata, OF_CREATE);
else
 file = (HANDLE) OpenFile(ifile, &filedata, OF_READWRITE);
if(int(file) == HFILE_ERROR) return -3;
if(SetFilePointer(file, long(size), (long*)(&size)+1, FILE_BEGIN) == 4294967295 && GetLastError() != NO_ERROR){
 CloseHandle(file);
 return -4;
}
if(SetEndOfFile(file) == NULL){
 CloseHandle(file);
 return -5;
}
CloseHandle(file);
return 0;
}
Tabs don't quite work in the CODE tags pinch.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.