Noob C++ Questions

Coming from c# world its sometimes hard to find my way in the C/C++ world so i will try to take the shame and ask the OSR gods for help :stuck_out_tongue:

Question 1

Can i have a dynamic size collection that can be re sized whenever needed.
Currently in my driver i can only have an initially allocated array of specified size containing handles.

I have something like this PVOID processes[128];

Also i wonder how can i check if the array at specified index is populated. Should i check if it equals NULL?

You can init first the array with NULL pointers. memset() can help you with
this. In each access you must check if there is a valid pointer at the
desired position:

if ( processes[i] != NULL )
{
… do whatever you want…
}

Have you considered using linked lists? Read about PLIST_ENTRY (OSR has
some useful articles about this subject)

Also, are you learning C writting drivers? I would start writting and
debugging usermode code first. I think you will learn faster and easier.

Hope it helps

Julian

El mi?rcoles, 15 de mayo de 2013, escribi?:

Coming from c# world its sometimes hard to find my way in the C/C++ world
so i will try to take the shame and ask the OSR gods for help :stuck_out_tongue:

Question 1

Can i have a dynamic size collection that can be re sized whenever needed.
Currently in my driver i can only have an initially allocated array of
specified size containing handles.

I have something like this PVOID processes[128];

Also i wonder how can i check if the array at specified index is
populated. Should i check if it equals NULL?


NTDEV is sponsored by OSR

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

> Coming from c# world its sometimes hard to find my way in the C/C++ world

Offtopic here, but some people like me would answer.

Can i have a dynamic size collection that can be re sized whenever needed.

In general C++, this is std::vector (or the similar classes like std::deque or std::list).

In Windows kernel (which is a special env without full C++ support), nothing of this can be used, since they rely on C++ exceptions not supported by Windows kernel.

Currently in my driver i can only have an initially allocated array of specified size containing handles.

Write the collection class yourself without using any exceptions. Base it on new operator.

Also i wonder how can i check if the array at specified index is populated. Should i check if it equals
NULL?

For pointer array - yes.

C/C++ have no .NET’s notion of “null”, and no T? datatypes. They only have numbers, pointers and structures/classes as datatypes. For numbers, there is no notion of “null”, only 0. For pointers, there is only NULL (which is (PVOID)0 and is different from any valid pointer). For structures/classes, the notion of “being null” is just not applicable.

–
Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

>Have you considered using linked lists? Read about PLIST_ENTRY

I second this. This is usually the best way to implement sets in OS kernels.

–
Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

On 15/05/2013 13:52, Maxim S. Shatskih wrote:

For pointers, there is only NULL (which is (PVOID)0 and is different
from any valid pointer).

There’s also nullptr in C++11
(http://msdn.microsoft.com/en-us/library/vstudio/4ex65770.aspx).

–
Bruce Cran

>> For pointers, there is only NULL (which is (PVOID)0 and is different

> from any valid pointer).

There’s also nullptr in C++11
(http://msdn.microsoft.com/en-us/library/vstudio/4ex65770.aspx).

Is it supported by MS’s compiler already?

–
Maxim S. Shatskih
Microsoft MVP on File System And Storage
xxxxx@storagecraft.com
http://www.storagecraft.com

On 15/05/2013 20:38, Maxim S. Shatskih wrote:

Is it supported by MS’s compiler already?

It was supported in VS 2010, and VS 2012 added more C++11 features -
http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx .

–
Bruce Cran

On Wed, May 15, 2013 at 5:52 AM, Maxim S. Shatskih
wrote:

> In Windows kernel (which is a special env without full C++ support), nothing of this can be used, since they rely on C++ exceptions not supported by Windows kernel.
>
>> Currently in my driver i can only have an initially allocated array of specified size containing handles.
>
> Write the collection class yourself without using any exceptions. Base it on new operator.

Careful… depending on which new you call, it may throw.

Nik

Nik Bougalis wrote:

On Wed, May 15, 2013 at 5:52 AM, Maxim S. Shatskih
wrote:
>
>> Write the collection class yourself without using any exceptions. Base it on new operator.
> Careful… depending on which new you call, it may throw.

There is no default “operator new” for kernel, so you’re always going to
be supplying your own. The standard kernel include file <kcom.h>
contains a perfectly functional set of “operator new” functions that
call ExAllocatePool and do not throw.

–
Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.</kcom.h>

Hmm, I just compiled a barebones drivers for testing, which called
“new char[128]” without including kcom.h and it compiled just fine. I
wonder if kcom.h is indirectly included somewhere, and if not, what
exactly is happening. I’ll have to go spelunking after work.

On Thu, May 16, 2013 at 9:37 AM, Tim Roberts wrote:
> Nik Bougalis wrote:
>> On Wed, May 15, 2013 at 5:52 AM, Maxim S. Shatskih
>> wrote:
>>
>>> Write the collection class yourself without using any exceptions. Base it on new operator.
>> Careful… depending on which new you call, it may throw.
>
> There is no default “operator new” for kernel, so you’re always going to
> be supplying your own. The standard kernel include file <kcom.h>
> contains a perfectly functional set of “operator new” functions that
> call ExAllocatePool and do not throw.
>
> –
> Tim Roberts, xxxxx@probo.com
> Providenza & Boekelheide, Inc.
>
>
> —
> NTDEV is sponsored by OSR
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer</kcom.h>

If you try to use a new() that throws and has a throw clause in its prototype, it will fail to.compile with the w8 wdk.

d

Bent from my phone


From: Tim Robertsmailto:xxxxx
Sent: ?5/?16/?2013 9:39 AM
To: Windows System Software Devs Interest Listmailto:xxxxx
Subject: Re: [ntdev] Noob C++ Questions

Nik Bougalis wrote:
> On Wed, May 15, 2013 at 5:52 AM, Maxim S. Shatskih
> wrote:
>
>> Write the collection class yourself without using any exceptions. Base it on new operator.
> Careful… depending on which new you call, it may throw.

There is no default “operator new” for kernel, so you’re always going to
be supplying your own. The standard kernel include file <kcom.h>
contains a perfectly functional set of “operator new” functions that
call ExAllocatePool and do not throw.

–
Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

—
NTDEV is sponsored by OSR

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer</kcom.h></mailto:xxxxx></mailto:xxxxx>

Step into the new call and see where it comes from

d

Bent from my phone


From: Nik Bougalismailto:xxxxx
Sent: ?5/?16/?2013 9:55 AM
To: Windows System Software Devs Interest Listmailto:xxxxx
Subject: Re: [ntdev] Noob C++ Questions

Hmm, I just compiled a barebones drivers for testing, which called
“new char[128]” without including kcom.h and it compiled just fine. I
wonder if kcom.h is indirectly included somewhere, and if not, what
exactly is happening. I’ll have to go spelunking after work.

On Thu, May 16, 2013 at 9:37 AM, Tim Roberts wrote:
> Nik Bougalis wrote:
>> On Wed, May 15, 2013 at 5:52 AM, Maxim S. Shatskih
>> wrote:
>>
>>> Write the collection class yourself without using any exceptions. Base it on new operator.
>> Careful… depending on which new you call, it may throw.
>
> There is no default “operator new” for kernel, so you’re always going to
> be supplying your own. The standard kernel include file <kcom.h>
> contains a perfectly functional set of “operator new” functions that
> call ExAllocatePool and do not throw.
>
> –
> Tim Roberts, xxxxx@probo.com
> Providenza & Boekelheide, Inc.
>
>
> —
> NTDEV is sponsored by OSR
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

—
NTDEV is sponsored by OSR

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer</kcom.h></mailto:xxxxx></mailto:xxxxx>

That was the plan - just not on somebody else’s dime :wink:

On Thu, May 16, 2013 at 10:25 AM, Doron Holan wrote:
> Step into the new call and see where it comes from
>
>
> d
>
> Bent from my phone
> ________________________________
> From: Nik Bougalis
> Sent: ‎5/‎16/‎2013 9:55 AM
>
> To: Windows System Software Devs Interest List
> Subject: Re: [ntdev] Noob C++ Questions
>
> Hmm, I just compiled a barebones drivers for testing, which called
> “new char[128]” without including kcom.h and it compiled just fine. I
> wonder if kcom.h is indirectly included somewhere, and if not, what
> exactly is happening. I’ll have to go spelunking after work.
>
> On Thu, May 16, 2013 at 9:37 AM, Tim Roberts wrote:
>> Nik Bougalis wrote:
>>> On Wed, May 15, 2013 at 5:52 AM, Maxim S. Shatskih
>>> wrote:
>>>
>>>> Write the collection class yourself without using any exceptions. Base
>>>> it on new operator.
>>> Careful… depending on which new you call, it may throw.
>>
>> There is no default “operator new” for kernel, so you’re always going to
>> be supplying your own. The standard kernel include file <kcom.h>
>> contains a perfectly functional set of “operator new” functions that
>> call ExAllocatePool and do not throw.
>>
>> –
>> Tim Roberts, xxxxx@probo.com
>> Providenza & Boekelheide, Inc.
>>
>>
>> —
>> NTDEV is sponsored by OSR
>>
>> OSR is HIRING!! See http://www.osr.com/careers
>>
>> For our schedule of WDF, WDM, debugging and other seminars visit:
>> http://www.osr.com/seminars
>>
>> To unsubscribe, visit the List Server section of OSR Online at
>> http://www.osronline.com/page.cfm?name=ListServer
>
> —
> NTDEV is sponsored by OSR
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer
>
>
>
> —
> NTDEV is sponsored by OSR
>
> OSR is HIRING!! See http://www.osr.com/careers
>
> For our schedule of WDF, WDM, debugging and other seminars visit:
> http://www.osr.com/seminars
>
> To unsubscribe, visit the List Server section of OSR Online at
> http://www.osronline.com/page.cfm?name=ListServer</kcom.h>

> Coming from c# world its sometimes hard to find my way in the C/C++ world

so i will try to take the shame and ask the OSR gods for help :stuck_out_tongue:

Question 1

Can i have a dynamic size collection that can be re sized whenever needed.
Currently in my driver i can only have an initially allocated array of
specified size containing handles.

I have something like this PVOID processes[128];

Also i wonder how can i check if the array at specified index is
populated. Should i check if it equals NULL?

Is it really a PVOID or is it a pointer to a known type, in which case
that typename would be a better chioce.

C has no provision for dynamic arrays. C++ collections do not work well
in the kernel because they can throw exceptions, which are currently not
supported in the kernel. You could write your own collection, but you
would have to be certain everything you do is compatible with what C++ in
the kernel is capable of supporting.

As already pointed out, lists are a better choice under most
circumstances. If there is no order relationship among the elements,
adding a new item is a fixed constant time. Allocating the space to
represent the item is the major performance cost, and Microsoft is
constantly improving the performance of the kernel allocator, so other
than actually measuring the performance, it is hard to estimate. If there
is an ordering relationship, a list is potentially very slow, but the
name of your variable suggests that it is more likely an unordered
collection of values. Adding to a list can be reduced to O(log2(n) + k)
where k is generally a small constant, but that is a complex algorithm I
don’t want to go into here if you don’t need it, and I suspect you don’t.

You did not give any context as to where that declaration appears. It
might be a local variable (not a great idea because of the limited stack
space that drivers have, in your device extension (generally the best
choice) or a global (generally, in fact, nearly always, the worst possible
place). In the latter two choices, you must either guarantee, by how your
code works, that it cannot ever be accessed from multiple threads in a way
that would find the contents would be invalid due to concurrent access, or
provide a lock appropriate to the IRQL level and time constraints you must
live with. In general, a spin lock would suffice, but then you must not
call any functions from within the spin lock that are not valid at
DISPATCH_LEVEL. So it is difficult to give an answer to the question
without knowing a lot more about the problem domain than you have given.
joe


NTDEV is sponsored by OSR

OSR is HIRING!! See http://www.osr.com/careers

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

> Coming from c# world its sometimes hard to find my way in the C/C++ world so i will try to take

the shame and ask the OSR gods for help :stuck_out_tongue:

No, you don’t have to “take the shame”…

What you have to take are:

A. A class on C language. When it comes to writing drivers, in-depth knowledge of C is just an
absolute prerequisite. At this point you will already know that all functionality of C comes from the
libraries that you link against, rather than language itself, so that you will not be surprised to hear that
UM C library routines are of zero use in the kernel

B. OS course, both theory and practice. Concerning the latter, I would strongly recommend writing a Linux driver for your device as a study project. You have to get familiar with kernel-level concepts like system calls, interrupts, understand the difference between schedulable context and atomic one, understand that physical memory and virtual one are totally different things, etc, etc, etc…

C. After having learned the above, you can already attempt Windows drivers. At this level of knowledge
you will be already in a position to take OSR course and learn the specifics of writing Windows drivers.

For the time being, it is of zero use to you, as well as the answers that you get here. For example,
look at Joe’s latest post - I bet you don’t understand a thing when he speaks about IRQL constraints and spinlocks…

Anton Bassov

Let’s go back to first principles, shall we?

The OP, Mr. Savelos, asked:

If you’re looking for a COLLECTION and not an ARRAY per se, then the answer is simple: Use a WDF Collection (WDFCOLLECTION). It’s do whatever you want (not size restricted, able to access data at random indexes).

It’s not a C# ArrayList (what is, right?)… but given we’re using C or C++, we typically solve these “I want a dynamic collection” problems with functions. Your alternative in C++ would be to wrap something like a WDFCOLLECTION with your own class that implemented familiar methods such as Add, Insert, IndexOf, and the like.

Peter
OSR