• 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

    • It does invite Linux because MS should have been improving their products all along these years instead chose to ignore the users now this year they’re making all of these improvements to their products where as Linux doesn’t have this kind of problems in fact it has gotten so good I can even play Windows games in Steam that I no longer ever needed Windows to carry on with my life, unlike you still use Windows and chose to try beat down down those who don't use Windows because they love their LG TV. (Surprisingly I actually own a 55” LED LG TV and it has been going strong for almost 14 years, longer than any Sony TV”)
    • As much as I love owning my own hardware, it's hard to argue with the value. I'm not a huge gamer, I'd actually be interested in a cheaper plan with limited monthly hours, or even a pay-by-the-hour plan.
    • Well, they (LibreOffice/The Document Foundation) are bitchy and whiny, yes, but they're right, at least this time. It doesn't make sense to market something as "free and open source to thwart dependency on foreign companies' software" but at the same time, using the formats of said companies (Microsoft) by default. That way, you are changing nothing, essentially, you're just using another UI. We all know that users just use the defaults and almost never change them. I'm not saying they should drop other formats altogether, but they shouldn't default to the thing they're trying to run away from in the first place. If you're gonna do something, just go all the way in, don't stop in the middle, IMO. Otherwise, shut up and move along.
    • Words cannot express how much garbage this app is.
    • Vivaldi 8.0.4033.46 by Razvan Serea Vivaldi is a cross-platform web browser built for – and with – the web. A browser based on the Blink engine (same in Chrome and Chromium) that is fast, but also a browser that is rich in functionality, highly flexible and puts the user first. A browser that is made for you. Vivaldi is produced with love by a founding team of browser pioneers, including former CEO Jon Stephenson von Tetzchner, who co-founded and led Opera Software. Vivaldi’s interface is very customizable. Vivaldi combines simplicity and fashion to create a basic, highly customizable interface that provides everything a internet user could need. The browser allows users to customize the appearance of UI elements such as background color, overall theme, address bar and tab positioning, and start pages. Vivaldi features the ability to "stack" and "tile" tabs, annotate web pages, add notes to bookmarks and much more. Vivaldi 8.0.4033.46 fixes: [Chromium] Update to 148.0.7778.263 ESR (includes security fixes from 149.0.7827.102/103) Download: Vivaldi 64-bit | 139.0 MB (Freeware) Download: Vivaldi 32-bit | ARM64 View: Vivaldi Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      FBSPL earned a badge
      Week One Done
    • One Year In
      Jim Dugan earned a badge
      One Year In
    • One Month Later
      Tommi118 earned a badge
      One Month Later
    • One Month Later
      sjbousquet earned a badge
      One Month Later
    • Week One Done
      sjbousquet earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      487
    2. 2
      PsYcHoKiLLa
      196
    3. 3
      +Edouard
      155
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      69
  • Tell a friend

    Love Neowin? Tell a friend!