Results 1 to 6 of 6

Thread: Of Pointers

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Arrow Of Pointers

    Since I delve in C# I might as well familiarize myself with is 'advantage' over VB.Net.

    I just wish to have opinions on Pointers, its drawbacks, when it should be used, etc...

    Anything that would be valuable to someone who wishes to employ Pointers are welcome...

    TIA
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Of Pointers

    Using pointers is a way to get at the data in memory directly rather than going through managed references. You can write much more efficient code because you can get and set bytes in memory directly rather than having to go through a long call stack of managed methods.

    The drawback with using pointers is that you have to take full responsibility for every bit of data you get and change. C# code using pointers is called unsafe because you can really make a mess of things if you write to the wrong memory. Because everything is just bytes it's more difficult to keep track of what's what. That's why C/C++ is so powerful yet so difficult.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Of Pointers

    The only reason I can see for you to use pointers would be when working with Win32 APIs. You know, those sucky ones.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Of Pointers

    One place where using pointers can speed things up is string manipulation. Accessing characters in a string directly rather than through managed types and members is orders of magnitude faster. Having said that, your average application won't be doing enough string manipulation for the difference to be noticeable to the user.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Addicted Member
    Join Date
    May 2000
    Location
    Wellington NZ
    Posts
    153

    Re: Of Pointers

    Remember pointers cannot be used with managed reference types, structs are fine! I've used heaps of pointer arithmetic for image manipulation.
    You dont need eyes to see, you need vision.

  6. #6
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Of Pointers

    Quote Originally Posted by RohanWest
    Remember pointers cannot be used with managed reference types, structs are fine! I've used heaps of pointer arithmetic for image manipulation.
    You can use pointers on managed objects (classes) if you Marshal them properly. But thats another story.

    Mendhak makes a good point about the API, however you could and should use pointers when you need high-performance in general processing usage. I frequently use pointers when the built-in generalised functions are simply not fast enough. One prime example is the string.Split() function. Probably the most badly-abused functions in the framework.

    Lets say you have a large text file (tab-delimited flat file with 50 fields per line for arguments sake) and you want to extract fields 0, 8 and 12. If you used split (like 95% of programmers would) then you'd effectively be doing the following...

    1. Get a line from the file
    2. Allocating a dynamic array of dynamic-length strings.
    3. Then looping through the characters in the current line, and each time you find a tab character, copy the string into the array.
    4. Do whatever you need to with array[0], array[8] and array[12].
    5. Throw away the entire array.
    -- repeat 1 through 5 until the file is done.

    This is because Split is highly generic and therefore by definition unsuitable for most of the uses it is put to.

    Doing the same thing with pointers would do away with the arrays completely (instant massive memory and time saving). In total, you'd need probably 3 integers and 3 strings to store the fields you want to keep.

    The following image shows the process I have just described. The spikes labelled "S" denote the usae of Split() within the reading loop, those marked "P" denotes looping with pointers.

    The pointer version uses less than 1 percent of the RAM overhead of the split() method and is approximately 3.9 times faster.

    (Pointers took 335ms and Split() took 1354ms on average, to process a 50mb tab delimited file after loading the entire file into RAM first, 3 strings were extracted from each line of text. The output data of both methods was identical.

    So you see, pointers can be of huge benefit if used wisely.
    Attached Images Attached Images  
    Last edited by wossname; Mar 30th, 2007 at 06:50 AM.
    I don't live here any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width