• 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

    • Not such a great deal, Ultimate, which gives you full 5080 features is $181.99 CAD per month, that's $2183.88 per year, I can buy the 5080 for $1809.99 CAD, then it goes up to $279.99 per month after the first billing cycle. Typical cloud rental, costs more than buying the hardware.
    • Wow, spoken like a true blind hater, you don't even provide arguments. Please, go check my comment above to @seacaptain and you'll find out why what you say doesn't make sense in this context...
    • Get used to this, with AI tooling now uncovering new vulns and getting them exploitable far faster than has ever been possible before software is going to need to be updated far more frequently. Back in the day it may take reseachers weeks or months to do what AI can now do in hours. Once its a threat is discovered it's weaponsized far more quickly, meaning you simply can't be waiting 2, 3, 4 weeks to deploy a patch, it needs to be patched immediately. Going to be interesting handling this in the enterprise space where traditionally patching has been steady, but very staged (and rightly so up until now), that is going to have to change.
    • You don't need to "close all browser sessions constantly" or wait for updates to install. The updates download in the background while you use the browser, without interrupting you, they install automatically the next time you launch the app. And they install very fast (depending on your storage speeds, of course), you have to wait at most 2-3 extra seconds, if any. Seems like you haven't used Edge in a loooooooong time...
    • Segra 1.6.0 by Razvan Serea Segra is a free, open-source OBS-powered game recorder offering fast gameplay capture, instant clips, AI highlights, deep game integration, and seamless uploads—perfect for gamers, streamers, and content creators. Lightweight, fast, zero bloat. Segra key features: Automatic Game Recording: Begin capturing gameplay the moment your game launches, with zero manual setup. Instant Clipping: Save important moments instantly using a customizable hotkey—perfect for highlights, montages, or quick shares. Segra AI Highlights: Let Segra automatically detect kills, assists, deaths, and key events to generate polished highlight reels without manual editing. Gameplay Uploads: Upload recordings and clips directly to Segra.tv for fast sharing and cloud access. Deep Game Integration: Enjoy advanced game-data tracking across hundreds of supported titles, enabling smart highlight generation and stat-informed clipping. High-Performance Capture: Record up to 4K at 144 FPS using OBS-powered technology with minimal performance impact, supporting NVENC, AMD VCE, and custom quality controls. Segra Editor: Edit recordings easily with timeline controls, segment management, and event-based navigation to build the perfect clip. Customization Options: Adjust hotkeys, output formats, storage paths, codecs, capture quality, and performance settings for a tailored recording experience. Segra 1.6.0 changelog: Recording: Added HDR support. Grand Theft Auto: Added game integration for deaths (FiveM and RAGE MP supported). Highlights: Added customizable padding for highlights. Replay Buffer: Added a shockwave visual effect when a replay buffer clip is saved. Audio: Increased the maximum sound effects volume from 100% to 200%. Hotkeys: Fixed hotkeys not triggering while unrelated keys were held. Installer: Added code signing to verify publisher identity, branded the installer, and reduced OS security warnings. OBS: Updated the supported OBS version to 32.1.2. Download: Segra 1.6.0 | 74.4 MB (Open Source) View: Segra Homepage | Github | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
    • Week One Done
      Timaximus earned a badge
      Week One Done
    • Rookie
      FBSPL went up a rank
      Rookie
    • First Post
      davidbazooked earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      PsYcHoKiLLa
      175
    3. 3
      +Edouard
      163
    4. 4
      Steven P.
      86
    5. 5
      ATLien_0
      80
  • Tell a friend

    Love Neowin? Tell a friend!