• 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

    • I'm not aware of this issue, but to help the other guys.  What version of Android are you using? Did you try a different browser? To see if Edge is the issue here.
    • I agree when are you going to read this (really poor BTW) article? Here is a better article so you actually know what is going on and answers questions you had in other comments --> https://arstechnica.com/gadgets/2026/05/speed-boosting-low-latency-profile-is-one-of-the-improvements-coming-to-windows-11/ It is unclear if one will be able to disable the new profile at this point but I am not seeing any reason why one would.
    • I disagree; they come off very "bitchy" and "whiny". Make a great product and combine that with a great price (free) and people will come over to your side. Or build it and they will come as they say. Constantly trying to get attention by complaining all the time, will turn people off to your product.
    • It use to be a nightmare, with LibreOffice supporting a newer draft ODF standard by default, and Microsoft Office supporting the older non-draft standard. Now that they both support the same version of ODF, they should be interoperable.
    • Brave Browser 1.91.171 by Razvan Serea Brave Browser is a lightning-fast, secure web browser that stands out from the competition with its focus on privacy, security, and speed. With features like HTTPS Everywhere and built-in tracker blocking, Brave keeps your online activities safe from prying eyes. Brave is one of the safest browsers on the market today. It blocks third-party data storage. It protects from browser fingerprinting. And it does all this by default. Speed - Brave is built on Chromium, the same technology that powers Google Chrome, and is optimized for speed, providing a fast and responsive browsing experience. Brave Browser also features Brave Rewards, a system that rewards users with Basic Attention Tokens (BAT) for viewing opt-in ads. This innovative system provides an alternative revenue model for content creators and a way to support the Brave community. SlimBrave Neo takes all the good things about Brave and makes them even better by keeping everything clean, light, and privacy-focused. It removes the extra clutter, turns off features you might not need, and cuts down on anything that could slow you down or collect unnecessary data. Because it relies on simple settings and policies instead of modifying the browser itself, you still get full Brave compatibility—just in a smoother, lighter, and more privacy-friendly package. Brave Browser 1.91.171 changelog: General Fixed Cardano not being disabled on upgrade to Brave Origin. Upgraded Chromium to 149.0.7827.103. Origin Removed “Survey Panelist” setting from brave://settings/privacy. Fixed P3A and usage ping under brave://settings/privacy being displayed on first launch on Linux. Upgraded Chromium to 149.0.7827.103. Download: Brave Browser 64-bit | 1.2 MB (Freeware) Download: Brave Browser 32-bit View: Brave Homepage | Offline Installers | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • One Month Later
      Jamswaz earned a badge
      One Month Later
    • Week One Done
      Jamswaz earned a badge
      Week One Done
    • Rookie
      Marzoid went up a rank
      Rookie
    • Community Regular
      coch went up a rank
      Community Regular
    • One Year In
      slackerzz earned a badge
      One Year In
  • Popular Contributors

    1. 1
      +primortal
      518
    2. 2
      PsYcHoKiLLa
      190
    3. 3
      +Edouard
      156
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      75
  • Tell a friend

    Love Neowin? Tell a friend!