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

    • Can we bring back manual save?
    • Wow, imagine you dump hundreds of hours into completing things and unlocking stuff and you lose it all. Back in the day when cheats were built into games, you could at least unlock things again that way without spending hundreds of hours again. But those days are long gone for some reason as no one builds cheats into games anymore. So it's even more painful that studio that's on its 6th installment **** it up so badly.
    • Spotify finally removes the disco ball app icon in the latest update by Ivan Jenic Image: Spotify Spotify has just released an update that removes its now infamous disco ball icon. The update reverts the app icon to the familiar flat green logo after weeks of mixed reactions online. The icon arrived on May 13 as part of the company's 20th anniversary celebration and was always intended to be temporary, though Spotify only confirmed that after the backlash started. The disco ball took the internet by storm, as the reception was split. A vocal group of users called it ugly and disorienting, with some iOS users noting that the 3D glowing effect made the app look like it was stuck mid-update. On the other end, the icon picked up a following of its own. Its retro, three-dimensional look immediately stood out against the flat, minimalist aesthetic that has dominated app design for years. It even started a small movement, spawning what people started calling "discomorphism," a mashup of disco and skeuomorphism. Other brands started posting disco ball versions of their own logos, probably in an effort to ride the wave of memes that flooded the internet during late May. Spotify has had a turbulent relationship with its user base lately. Besides the disco ball icon, which certainly wasn't appreciated by everyone, the company has also received backlash for its willingness to include AI-generated music on its platform. On May 17, Spotify promised the old icon would return “in a few weeks.” And now it looks like that time has finally arrived. So, whether you liked the disco ball or it made you uncomfortable, it’s now gone for good. The next time you update the Spotify app on your phone, the old, flat-design icon will return.
  • Recent Achievements

    • One Year In
      slackerzz earned a badge
      One Year In
    • One Year In
      highriskpaym earned a badge
      One Year In
    • One Month Later
      highriskpaym earned a badge
      One Month Later
    • Week One Done
      highriskpaym earned a badge
      Week One Done
    • Week One Done
      FBSPL earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      519
    2. 2
      PsYcHoKiLLa
      197
    3. 3
      +Edouard
      157
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!