• 0

[C++] Few questions


Question

- 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...

Edited by xinok
Link to comment
https://www.neowin.net/forum/topic/337811-c-few-questions/
Share on other sites

15 answers to this question

Recommended Posts

  • 0

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).

  • 0
For the array question:

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

586132991[/snapback]

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 :(

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.

  • 0
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 :(

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.

586133475[/snapback]

Well, you can always do a:

int array[15];

const int *var = array;

var[5] = 0;

  • 0
Well, you can always do a:

int array[15];

const int *var = array;

var[5] = 0;

586133539[/snapback]

Ah cool, I never knew you could make a pointer constant.

That'll do for all my questions, thanks for the help :yes:

  • 0
- 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.

586132836[/snapback]

Do something like the following...

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.

Edited by Oogle
  • 0

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?

  • 0

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).

  • 0

Don't worry people, I got it :p

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...)

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:

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.