Results 1 to 4 of 4

Thread: [RESOLVED] First time using pointers - is this the right way to do this?

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Resolved [RESOLVED] First time using pointers - is this the right way to do this?

    I need to get a pointer to a particular spot in an array - it's an __int64 array. So I googled around and this seems to be what I need to do - please tell me if I'm missing something or doing something wrong.

    The array I need a pointer to is passed into this C++ function as cwlayout

    Code:
    	int processCW(WCHAR *strSearch, int *smarkerskt, int *emarkerskt..., __int64 *cwlayout...) {
    Comes from VB as Dim cwLayout(cwValue - 1) As Long - but that doesn't really matter...

    Back in the C++ I've got this class that I've added cwPointerLayout to - declaring it to be a "pointer to" an "__int64" memory spot (I think I've done that!). I've also initialized the pointer to nullptr - that made it look good while stepping through the code in the debugger...

    Code:
    class cwObject {
    	public:
    		WCHAR cwR;
    .
    .
    .
    		__int64 * cwPointerLayout;
    		int dbgS;
    		int dbgE;
    		bool dbgStarted;
    		cwObject () {cwRun = 0...
    			cwPointerLayout = nullptr;
    			dbgOn = false, dbgStarted = false;
    			dbgS = -1, dbgE = -1;
    			cwR = L'', cwRXOff = L'';
    		};
    	};
    Back in the function I do this

    Code:
    cwlayout[loopCW] = 0;						// Clear the layout
    cwX.cwPointerLayout = & cwlayout[loopCW];	// Get a pointer to that spot
    And that seems to load the address of the [loopCW] spot of the array into the cwPointerLayout variable.

    And in the immediate window I did this

    Code:
    *cwX.cwPointerLayout = 123
    And that seemed to change the value of cwlayout[loopCW].

    Is that the way this is supposed to be setup? Am I using the referencing and dereferencing properly?

    Thanks!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: First time using pointers - is this the right way to do this?

    That looks fine to me.

    Another way to write this...
    Code:
    cwlayout[loopCW] = 0;						// Clear the layout
    cwX.cwPointerLayout = & cwlayout[loopCW];	// Get a pointer to that spot
    is this...
    Code:
    cwx.cwPointerLayout = cwlayout + loopCW; //get a pointer to layout at index loopCW
    *cwx.cwPointerLayout = 0 //clear the layout
    just another way to do the same thing
    W o t . S i g

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: First time using pointers - is this the right way to do this?

    Thanks - I've been using it all day and it's working really well so far.

    I would have to have more confidence to do this

    Code:
    cwx.cwPointerLayout = cwlayout + loopCW;
    I can see why it works - but would not want to rely on thinking in that syntax.

    I'm having a huge pain dealing with the apparent lack of two-dimensional arrays in C++ - I make these arrays in VB and use them in the C++ code.

    I've spent hours chasing typo's in offset variables which clobber memory - how can I know it clobbers memory when it actually happens as opposed to many functions later when you get the "you've been a bad boy with your marshaling" error...

    When I coded on mainframe/mini's back 30 years ago you were told you touched a bad spot in memory when you did it.

    [edit] btw - code like this is allowed - right?

    Code:
    *cwX.cwPointerLayout = *cwX.cwPointerLayout << 1;
    
    *cwX.cwPointerLayout |= (__int64)(1 << (cwX.cwWordCount - cwX.cwLeftWord));
    
    *cwX.cwPointerLayout |= 1;
    [/edit]
    Last edited by szlamany; Mar 24th, 2013 at 04:41 PM.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    Re: First time using pointers - is this the right way to do this?

    right
    W o t . S i g

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