Registers (aka memory cells) in the VM are now numbered starting with 1
instead of 0.  A register number of 0 means "no such register". (CVS 4669)

FossilOrigin-Name: 0b849805c3a0f562d50623f406279b400d335639
diff --git a/src/analyze.c b/src/analyze.c
index 572fdb6..0b29aaa 100644
--- a/src/analyze.c
+++ b/src/analyze.c
@@ -11,7 +11,7 @@
 *************************************************************************
 ** This file contains code associated with the ANALYZE command.
 **
-** @(#) $Id: analyze.c,v 1.29 2008/01/03 09:51:55 danielk1977 Exp $
+** @(#) $Id: analyze.c,v 1.30 2008/01/03 18:03:09 drh Exp $
 */
 #ifndef SQLITE_OMIT_ANALYZE
 #include "sqliteInt.h"
@@ -241,7 +241,7 @@
   sqlite3BeginWriteOperation(pParse, 0, iDb);
   iStatCur = pParse->nTab++;
   openStatTable(pParse, iDb, iStatCur, 0);
-  iMem = pParse->nMem;
+  iMem = pParse->nMem+1;
   for(k=sqliteHashFirst(&pSchema->tblHash); k; k=sqliteHashNext(k)){
     Table *pTab = (Table*)sqliteHashData(k);
     analyzeOneTable(pParse, pTab, iStatCur, iMem);
@@ -263,7 +263,7 @@
   sqlite3BeginWriteOperation(pParse, 0, iDb);
   iStatCur = pParse->nTab++;
   openStatTable(pParse, iDb, iStatCur, pTab->zName);
-  analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem);
+  analyzeOneTable(pParse, pTab, iStatCur, pParse->nMem+1);
   loadAnalysis(pParse, iDb);
 }
 
diff --git a/src/build.c b/src/build.c
index f6faeb7..978cca9 100644
--- a/src/build.c
+++ b/src/build.c
@@ -22,7 +22,7 @@
 **     COMMIT
 **     ROLLBACK
 **
-** $Id: build.c,v 1.455 2008/01/03 09:51:55 danielk1977 Exp $
+** $Id: build.c,v 1.456 2008/01/03 18:03:09 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -2628,7 +2628,7 @@
   else if( db->init.busy==0 ){
     Vdbe *v;
     char *zStmt;
-    int iMem = pParse->nMem++;
+    int iMem = ++pParse->nMem;
 
     v = sqlite3GetVdbe(pParse);
     if( v==0 ) goto exit_create_index;
diff --git a/src/delete.c b/src/delete.c
index 6e7f85e..a2c4da2 100644
--- a/src/delete.c
+++ b/src/delete.c
@@ -12,7 +12,7 @@
 ** This file contains C code routines that are called by the parser
 ** in order to generate code for DELETE FROM statements.
 **
-** $Id: delete.c,v 1.143 2008/01/03 17:31:45 danielk1977 Exp $
+** $Id: delete.c,v 1.144 2008/01/03 18:03:09 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -65,8 +65,8 @@
 ** reads the key and data to insert from memory cells.
 */
 void sqlite3CodeInsert(Parse *p, int iCur, u8 flags){
-  int iData = p->nMem++;
-  int iKey = p->nMem++;
+  int iData = ++p->nMem;
+  int iKey = ++p->nMem;
   Vdbe *v = sqlite3GetVdbe(p);
   sqlite3VdbeAddOp2(v, OP_MemStore, iData, 1);
   sqlite3VdbeAddOp2(v, OP_MemStore, iKey, 1);
@@ -82,7 +82,7 @@
 */
 int sqlite3StackToReg(Parse *p, int nVal){
   int i;
-  int iRet = p->nMem;
+  int iRet = p->nMem+1;
   Vdbe *v = sqlite3GetVdbe(p);
   assert(v);
   p->nMem += nVal;
@@ -263,7 +263,7 @@
   ** we are counting rows.
   */
   if( db->flags & SQLITE_CountRows ){
-    memCnt = pParse->nMem++;
+    memCnt = ++pParse->nMem;
     sqlite3VdbeAddOp2(v, OP_MemInt, 0, memCnt);
   }
 
@@ -346,7 +346,7 @@
     sqlite3VdbeAddOp1(v, OP_StackDepth, -1);
 
     if( triggers_exist ){
-      int mem1 = pParse->nMem++;
+      int mem1 = ++pParse->nMem;
       if( !isView ){
         sqlite3VdbeAddOp1(v, OP_MemStore, mem1);
       }
diff --git a/src/expr.c b/src/expr.c
index 7761197..89b820b 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -12,7 +12,7 @@
 ** This file contains routines used for analyzing expressions and
 ** for generating VDBE code that evaluates expressions in SQLite.
 **
-** $Id: expr.c,v 1.326 2008/01/03 07:54:24 danielk1977 Exp $
+** $Id: expr.c,v 1.327 2008/01/03 18:03:09 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -307,7 +307,7 @@
     return 0;  /* Malloc failed */
   }
   depth = atoi((char*)&pToken->z[1]);
-  p->iTable = pParse->nMem++;
+  p->iTable = ++pParse->nMem;
   sqlite3VdbeAddOp1(v, OP_Dup, depth);
   sqlite3VdbeAddOp2(v, OP_MemStore, p->iTable, 1);
   return p;
@@ -1600,7 +1600,7 @@
     */
     assert(v);
     if( iCol<0 ){
-      int iMem = pParse->nMem++;
+      int iMem = ++pParse->nMem;
       int iAddr;
       Table *pTab = p->pSrc->a[0].pTab;
       int iDb = sqlite3SchemaToIndex(db, pTab->pSchema);
@@ -1635,7 +1635,7 @@
          && (!mustBeUnique || (pIdx->nColumn==1 && pIdx->onError!=OE_None))
         ){
           int iDb;
-          int iMem = pParse->nMem++;
+          int iMem = ++pParse->nMem;
           int iAddr;
           char *pKey;
   
@@ -1699,7 +1699,7 @@
   ** save the results, and reuse the same result on subsequent invocations.
   */
   if( !ExprHasAnyProperty(pExpr, EP_VarSelect) && !pParse->trigStack ){
-    int mem = pParse->nMem++;
+    int mem = ++pParse->nMem;
     sqlite3VdbeAddOp1(v, OP_MemLoad, mem);
     testAddr = sqlite3VdbeAddOp0(v, OP_If);
     assert( testAddr>0 || pParse->db->mallocFailed );
@@ -1804,7 +1804,7 @@
       SelectDest dest;
 
       pSel = pExpr->pSelect;
-      dest.iParm = pParse->nMem++;
+      dest.iParm = ++pParse->nMem;
       if( pExpr->op==TK_SELECT ){
         dest.eDest = SRT_Mem;
         sqlite3VdbeAddOp2(v, OP_MemNull, 0, dest.iParm);
@@ -2343,7 +2343,7 @@
   addr2 = sqlite3VdbeCurrentAddr(v);
   if( addr2>addr1+1
    || ((pOp = sqlite3VdbeGetOp(v, addr1))!=0 && pOp->opcode==OP_Function) ){
-    iMem = pExpr->iTable = pParse->nMem++;
+    iMem = pExpr->iTable = ++pParse->nMem;
     sqlite3VdbeAddOp2(v, OP_MemStore, iMem, 0);
     pExpr->op = TK_REGISTER;
   }
@@ -2364,7 +2364,7 @@
   if( v==0 ) return -1;
   sqlite3ExprCode(pParse, pExpr);
   if( target<0 ){
-    target = pParse->nMem++;
+    target = ++pParse->nMem;
   }
   sqlite3VdbeAddOp2(v, OP_MemStore, target, 1);
   return target;
@@ -2724,7 +2724,7 @@
               pCol->pTab = pExpr->pTab;
               pCol->iTable = pExpr->iTable;
               pCol->iColumn = pExpr->iColumn;
-              pCol->iMem = pParse->nMem++;
+              pCol->iMem = ++pParse->nMem;
               pCol->iSorterColumn = -1;
               pCol->pExpr = pExpr;
               if( pAggInfo->pGroupBy ){
@@ -2780,7 +2780,7 @@
           if( i>=0 ){
             pItem = &pAggInfo->aFunc[i];
             pItem->pExpr = pExpr;
-            pItem->iMem = pParse->nMem++;
+            pItem->iMem = ++pParse->nMem;
             pItem->pFunc = sqlite3FindFunction(pParse->db,
                    (char*)pExpr->token.z, pExpr->token.n,
                    pExpr->pList ? pExpr->pList->nExpr : 0, enc, 0);
diff --git a/src/insert.c b/src/insert.c
index 068bc8e..63dd967 100644
--- a/src/insert.c
+++ b/src/insert.c
@@ -12,7 +12,7 @@
 ** This file contains C code routines that are called by the parser
 ** to handle INSERT statements in SQLite.
 **
-** $Id: insert.c,v 1.206 2008/01/03 17:31:45 danielk1977 Exp $
+** $Id: insert.c,v 1.207 2008/01/03 18:03:09 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -146,7 +146,7 @@
 ** This routine returns the index of the mem[] cell that contains
 ** the maximum rowid counter.
 **
-** Two memory cells are allocated.  The next memory cell after the
+** Two memory cells are allocated.  The next memory cell befor the
 ** one returned holds the rowid in sqlite_sequence where we will
 ** write back the revised maximum rowid.
 */
@@ -163,8 +163,8 @@
     int addr;
     assert( v );
     addr = sqlite3VdbeCurrentAddr(v);
-    memId = pParse->nMem+1;
     pParse->nMem += 2;
+    memId = pParse->nMem;
     sqlite3OpenTable(pParse, iCur, iDb, pDb->pSchema->pSeqTab, OP_OpenRead);
     sqlite3VdbeAddOp2(v, OP_Rewind, iCur, addr+12);
     sqlite3VdbeAddOp2(v, OP_Column, iCur, 0);
@@ -618,7 +618,7 @@
   /* Initialize the count of rows to be inserted
   */
   if( db->flags & SQLITE_CountRows ){
-    iCntMem = pParse->nMem++;
+    iCntMem = ++pParse->nMem;
     sqlite3VdbeAddOp2(v, OP_MemInt, 0, iCntMem);
   }
 
diff --git a/src/pragma.c b/src/pragma.c
index 8ff68fe..37c6bab 100644
--- a/src/pragma.c
+++ b/src/pragma.c
@@ -11,7 +11,7 @@
 *************************************************************************
 ** This file contains code used to implement the PRAGMA command.
 **
-** $Id: pragma.c,v 1.156 2008/01/03 01:28:59 drh Exp $
+** $Id: pragma.c,v 1.157 2008/01/03 18:03:09 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -147,7 +147,7 @@
 */
 static void returnSingleInt(Parse *pParse, const char *zLabel, int value){
   Vdbe *v = sqlite3GetVdbe(pParse);
-  int mem = pParse->nMem++;
+  int mem = ++pParse->nMem;
   sqlite3VdbeAddOp2(v, OP_MemInt, value, mem);
   if( pParse->explain==0 ){
     sqlite3VdbeSetNumCols(v, 1);
@@ -492,11 +492,11 @@
       iLimit = 0x7fffffff;
     }
     sqlite3BeginWriteOperation(pParse, 0, iDb);
-    sqlite3VdbeAddOp2(v, OP_MemInt, iLimit, 0);
+    sqlite3VdbeAddOp2(v, OP_MemInt, iLimit, 1);
     addr = sqlite3VdbeAddOp2(v, OP_IncrVacuum, iDb, 0);
     sqlite3VdbeAddOp2(v, OP_Callback, 0, 0);
-    sqlite3VdbeAddOp2(v, OP_MemIncr, -1, 0);
-    sqlite3VdbeAddOp2(v, OP_IfMemPos, 0, addr);
+    sqlite3VdbeAddOp2(v, OP_MemIncr, -1, 1);
+    sqlite3VdbeAddOp2(v, OP_IfMemPos, 1, addr);
     sqlite3VdbeJumpHere(v, addr);
   }else
 #endif
@@ -830,7 +830,7 @@
     ** error message
     */
     static const VdbeOpList endCode[] = {
-      { OP_MemLoad,     0, 0,        0},
+      { OP_MemLoad,     1, 0,        0},
       { OP_Integer,     0, 0,        0},
       { OP_Ne,          0, 0,        0},    /* 2 */
       { OP_String8,     0, 0,        0},    /* 3 */
@@ -852,7 +852,7 @@
         mxErr = SQLITE_INTEGRITY_CHECK_ERROR_MAX;
       }
     }
-    sqlite3VdbeAddOp2(v, OP_MemInt, mxErr, 0);
+    sqlite3VdbeAddOp2(v, OP_MemInt, mxErr, 1);
 
     /* Do an integrity check on each database file */
     for(i=0; i<db->nDb; i++){
@@ -863,7 +863,7 @@
       if( OMIT_TEMPDB && i==1 ) continue;
 
       sqlite3CodeVerifySchema(pParse, i);
-      addr = sqlite3VdbeAddOp2(v, OP_IfMemPos, 0, 0);
+      addr = sqlite3VdbeAddOp2(v, OP_IfMemPos, 1, 0);
       sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
       sqlite3VdbeJumpHere(v, addr);
 
@@ -881,7 +881,7 @@
         }
       }
       if( cnt==0 ) continue;
-      sqlite3VdbeAddOp2(v, OP_IntegrityCk, 0, i);
+      sqlite3VdbeAddOp2(v, OP_IntegrityCk, 1, i);
       addr = sqlite3VdbeAddOp2(v, OP_IsNull, -1, 0);
       sqlite3VdbeAddOp4(v, OP_String8, 0, 0, 0,
          sqlite3MPrintf(db, "*** in database %s ***\n", db->aDb[i].zName),
@@ -899,17 +899,17 @@
         int loopTop;
 
         if( pTab->pIndex==0 ) continue;
-        addr = sqlite3VdbeAddOp2(v, OP_IfMemPos, 0, 0);
+        addr = sqlite3VdbeAddOp2(v, OP_IfMemPos, 1, 0);
         sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
         sqlite3VdbeJumpHere(v, addr);
         sqlite3OpenTableAndIndices(pParse, pTab, 1, OP_OpenRead);
-        sqlite3VdbeAddOp2(v, OP_MemInt, 0, 1);
+        sqlite3VdbeAddOp2(v, OP_MemInt, 0, 2);
         loopTop = sqlite3VdbeAddOp2(v, OP_Rewind, 1, 0);
-        sqlite3VdbeAddOp2(v, OP_MemIncr, 1, 1);
+        sqlite3VdbeAddOp2(v, OP_MemIncr, 1, 2);
         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
           int jmp2;
           static const VdbeOpList idxErr[] = {
-            { OP_MemIncr,    -1,  0,  0},
+            { OP_MemIncr,    -1,  1,  0},
             { OP_String8,     0,  0,  0},    /* 1 */
             { OP_Rowid,       1,  0,  0},
             { OP_String8,     0,  0,  0},    /* 3 */
@@ -929,21 +929,21 @@
         sqlite3VdbeJumpHere(v, loopTop);
         for(j=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, j++){
           static const VdbeOpList cntIdx[] = {
-             { OP_MemInt,       0,  2,  0},
+             { OP_MemInt,       0,  3,  0},
              { OP_Rewind,       0,  0,  0},  /* 1 */
-             { OP_MemIncr,      1,  2,  0},
+             { OP_MemIncr,      1,  3,  0},
              { OP_Next,         0,  0,  0},  /* 3 */
-             { OP_MemLoad,      1,  0,  0},
              { OP_MemLoad,      2,  0,  0},
+             { OP_MemLoad,      3,  0,  0},
              { OP_Eq,           0,  0,  0},  /* 6 */
-             { OP_MemIncr,     -1,  0,  0},
+             { OP_MemIncr,     -1,  1,  0},
              { OP_String8,      0,  0,  0},  /* 8 */
              { OP_String8,      0,  0,  0},  /* 9 */
              { OP_Concat,       0,  0,  0},
              { OP_Callback,     1,  0,  0},
           };
           if( pIdx->tnum==0 ) continue;
-          addr = sqlite3VdbeAddOp2(v, OP_IfMemPos, 0, 0);
+          addr = sqlite3VdbeAddOp2(v, OP_IfMemPos, 1, 0);
           sqlite3VdbeAddOp2(v, OP_Halt, 0, 0);
           sqlite3VdbeJumpHere(v, addr);
           addr = sqlite3VdbeAddOpList(v, ArraySize(cntIdx), cntIdx);
diff --git a/src/select.c b/src/select.c
index bc2f018..de8fd17 100644
--- a/src/select.c
+++ b/src/select.c
@@ -12,7 +12,7 @@
 ** This file contains C code routines that are called by the parser
 ** to handle SELECT statements in SQLite.
 **
-** $Id: select.c,v 1.380 2008/01/03 09:51:55 danielk1977 Exp $
+** $Id: select.c,v 1.381 2008/01/03 18:03:09 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -541,7 +541,7 @@
   }else{
     n = pEList->nExpr;
   }
-  iMem = pParse->nMem;
+  iMem = ++pParse->nMem;
   pParse->nMem += n+1;
   sqlite3VdbeAddOp2(v, OP_MemInt, n, iMem);
   if( nColumn>0 ){
@@ -1746,8 +1746,8 @@
   ** no rows.
   */
   if( p->pLimit ){
-    p->iLimit = iLimit = pParse->nMem;
-    pParse->nMem += 2;
+    p->iLimit = iLimit = ++pParse->nMem;
+    pParse->nMem++;
     v = sqlite3GetVdbe(pParse);
     if( v==0 ) return;
     sqlite3ExprCode(pParse, p->pLimit);
@@ -1758,7 +1758,7 @@
     sqlite3VdbeAddOp2(v, OP_MemLoad, iLimit, 0);
   }
   if( p->pOffset ){
-    p->iOffset = iOffset = pParse->nMem++;
+    p->iOffset = iOffset = ++pParse->nMem;
     v = sqlite3GetVdbe(pParse);
     if( v==0 ) return;
     sqlite3ExprCode(pParse, p->pOffset);
@@ -3449,11 +3449,11 @@
 
       /* Initialize memory locations used by GROUP BY aggregate processing
       */
-      iUseFlag = pParse->nMem++;
-      iAbortFlag = pParse->nMem++;
-      iAMem = pParse->nMem;
+      iUseFlag = ++pParse->nMem;
+      iAbortFlag = ++pParse->nMem;
+      iAMem = pParse->nMem + 1;
       pParse->nMem += pGroupBy->nExpr;
-      iBMem = pParse->nMem;
+      iBMem = pParse->nMem + 1;
       pParse->nMem += pGroupBy->nExpr;
       sqlite3VdbeAddOp2(v, OP_MemInt, 0, iAbortFlag);
       VdbeComment((v, "clear abort flag"));
diff --git a/src/trigger.c b/src/trigger.c
index 54129dd..c6ec0e0 100644
--- a/src/trigger.c
+++ b/src/trigger.c
@@ -243,8 +243,8 @@
     };
     int addr;
     Vdbe *v;
-    int iKey = pParse->nMem++;
-    int iData = pParse->nMem++;
+    int iKey = ++pParse->nMem;
+    int iData = ++pParse->nMem;
 
     /* Make an entry in the sqlite_master table */
     v = sqlite3GetVdbe(pParse);
diff --git a/src/update.c b/src/update.c
index 60fd195..ac48164 100644
--- a/src/update.c
+++ b/src/update.c
@@ -12,7 +12,7 @@
 ** This file contains C code routines that are called by the parser
 ** to handle UPDATE statements.
 **
-** $Id: update.c,v 1.153 2008/01/03 17:31:45 danielk1977 Exp $
+** $Id: update.c,v 1.154 2008/01/03 18:03:09 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -268,7 +268,7 @@
   if( v==0 ) goto update_cleanup;
   if( pParse->nested==0 ) sqlite3VdbeCountChanges(v);
   sqlite3BeginWriteOperation(pParse, 1, iDb);
-  mem1 = pParse->nMem++;
+  mem1 = ++pParse->nMem;
 
 #ifndef SQLITE_OMIT_VIRTUALTABLE
   /* Virtual tables must be handled separately */
@@ -353,7 +353,7 @@
   /* Initialize the count of updated rows
   */
   if( db->flags & SQLITE_CountRows && !pParse->trigStack ){
-    memCnt = pParse->nMem++;
+    memCnt = ++pParse->nMem;
     sqlite3VdbeAddOp2(v, OP_MemInt, 0, memCnt);
   }
 
diff --git a/src/vdbe.c b/src/vdbe.c
index 47079cf..3dd7598 100644
--- a/src/vdbe.c
+++ b/src/vdbe.c
@@ -43,7 +43,7 @@
 ** in this file for details.  If in doubt, do not deviate from existing
 ** commenting and indentation practices when changing or adding code.
 **
-** $Id: vdbe.c,v 1.670 2008/01/03 17:31:45 danielk1977 Exp $
+** $Id: vdbe.c,v 1.671 2008/01/03 18:03:09 drh Exp $
 */
 #include "sqliteInt.h"
 #include <ctype.h>
@@ -1040,8 +1040,8 @@
   Mem *pMem;
   int i;
   assert( p->nResColumn==pOp->p2 );
-  assert( pOp->p1>=0 );
-  assert( pOp->p1+pOp->p2<p->nMem );
+  assert( pOp->p1>0 );
+  assert( pOp->p1+pOp->p2<=p->nMem );
 
   /* Data in the pager might be moved or changed out from under us
   ** in between the return from this sqlite3_step() call and the
@@ -2391,10 +2391,10 @@
 
   if( pOp->opcode==OP_RegMakeRec || pOp->opcode==OP_RegMakeIRec ){
     Mem *pCount;
-    assert( nField>=0 && nField<p->nMem );
+    assert( nField>0 && nField<=p->nMem );
     pCount = &p->aMem[nField];
     assert( pCount->flags & MEM_Int );
-    assert( pCount->u.i>=0 && pCount->u.i+nField<p->nMem );
+    assert( pCount->u.i>0 && pCount->u.i+nField<=p->nMem );
     leaveOnStack = 1;
     nField = pCount->u.i;
     pData0 = &pCount[1];
@@ -3501,7 +3501,7 @@
 #ifndef SQLITE_OMIT_AUTOINCREMENT
       if( pOp->p2 ){
         Mem *pMem;
-        assert( pOp->p2>0 && pOp->p2<p->nMem );  /* P2 is a valid memory cell */
+        assert( pOp->p2>0 && pOp->p2<=p->nMem ); /* P2 is a valid memory cell */
         pMem = &p->aMem[pOp->p2];
         sqlite3VdbeMemIntegerify(pMem);
         assert( (pMem->flags & MEM_Int)!=0 );  /* mem(P2) holds an integer */
@@ -4452,7 +4452,7 @@
   aRoot = sqlite3_malloc( sizeof(int)*(nRoot+1) );
   if( aRoot==0 ) goto no_mem;
   j = pOp->p1;
-  assert( j>=0 && j<p->nMem );
+  assert( j>0 && j<=p->nMem );
   pnErr = &p->aMem[j];
   assert( (pnErr->flags & MEM_Int)!=0 );
   for(j=0; j<nRoot; j++){
@@ -4573,7 +4573,7 @@
 */
 case OP_MemStore: {        /* no-push */
   assert( pTos>=p->aStack );
-  assert( pOp->p1>=0 && pOp->p1<p->nMem );
+  assert( pOp->p1>0 && pOp->p1<=p->nMem );
   rc = sqlite3VdbeMemMove(&p->aMem[pOp->p1], pTos);
   pTos--;
 
@@ -4595,7 +4595,7 @@
 */
 case OP_MemLoad: {
   int i = pOp->p1;
-  assert( i>=0 && i<p->nMem );
+  assert( i>0 && i<=p->nMem );
   pTos++;
   sqlite3VdbeMemShallowCopy(pTos, &p->aMem[i], MEM_Ephem);
   break;
@@ -4614,7 +4614,7 @@
   int i = pOp->p1;
   Mem *pMem;
   assert( pTos>=p->aStack );
-  assert( i>=0 && i<p->nMem );
+  assert( i>0 && i<=p->nMem );
   pMem = &p->aMem[i];
   sqlite3VdbeMemIntegerify(pMem);
   sqlite3VdbeMemIntegerify(pTos);
@@ -4635,7 +4635,7 @@
 case OP_MemIncr: {        /* no-push */
   int i = pOp->p2;
   Mem *pMem;
-  assert( i>=0 && i<p->nMem );
+  assert( i>0 && i<=p->nMem );
   pMem = &p->aMem[i];
   assert( pMem->flags==MEM_Int );
   pMem->u.i += pOp->p1;
@@ -4652,7 +4652,7 @@
 case OP_IfMemPos: {        /* no-push */
   int i = pOp->p1;
   Mem *pMem;
-  assert( i>=0 && i<p->nMem );
+  assert( i>0 && i<=p->nMem );
   pMem = &p->aMem[i];
   assert( pMem->flags==MEM_Int );
   if( pMem->u.i>0 ){
@@ -4671,7 +4671,7 @@
 case OP_IfMemNeg: {        /* no-push */
   int i = pOp->p1;
   Mem *pMem;
-  assert( i>=0 && i<p->nMem );
+  assert( i>0 && i<=p->nMem );
   pMem = &p->aMem[i];
   assert( pMem->flags==MEM_Int );
   if( pMem->u.i<0 ){
@@ -4690,7 +4690,7 @@
 case OP_IfMemZero: {        /* no-push */
   int i = pOp->p1;
   Mem *pMem;
-  assert( i>=0 && i<p->nMem );
+  assert( i>0 && i<=p->nMem );
   pMem = &p->aMem[i];
   assert( pMem->flags==MEM_Int );
   if( pMem->u.i==0 ){
@@ -4705,7 +4705,7 @@
 */
 case OP_IfMemNull: {        /* no-push */
   int i = pOp->p1;
-  assert( i>=0 && i<p->nMem );
+  assert( i>0 && i<=p->nMem );
   if( p->aMem[i].flags & MEM_Null ){
      pc = pOp->p2 - 1;
   }
@@ -4717,7 +4717,7 @@
 ** Store a NULL in memory cell P2
 */
 case OP_MemNull: {
-  assert( pOp->p2>=0 && pOp->p2<p->nMem );
+  assert( pOp->p2>0 && pOp->p2<=p->nMem );
   sqlite3VdbeMemSetNull(&p->aMem[pOp->p2]);
   break;
 }
@@ -4727,7 +4727,7 @@
 ** Store the integer value P1 in memory cell P2.
 */
 case OP_MemInt: {
-  assert( pOp->p2>=0 && pOp->p2<p->nMem );
+  assert( pOp->p2>0 && pOp->p2<=p->nMem );
   sqlite3VdbeMemSetInt64(&p->aMem[pOp->p2], pOp->p1);
   break;
 }
@@ -4739,8 +4739,8 @@
 ** containing a NULL.
 */
 case OP_MemMove: {
-  assert( pOp->p1>=0 && pOp->p1<p->nMem );
-  assert( pOp->p2>=0 && pOp->p2<p->nMem );
+  assert( pOp->p1>0 && pOp->p1<=p->nMem );
+  assert( pOp->p2>0 && pOp->p2<=p->nMem );
   rc = sqlite3VdbeMemMove(&p->aMem[pOp->p1], &p->aMem[pOp->p2]);
   break;
 }
@@ -4771,7 +4771,7 @@
     storeTypeInfo(pRec, encoding);
   }
   ctx.pFunc = pOp->p4.pFunc;
-  assert( pOp->p1>=0 && pOp->p1<p->nMem );
+  assert( pOp->p1>0 && pOp->p1<=p->nMem );
   ctx.pMem = pMem = &p->aMem[pOp->p1];
   pMem->n++;
   ctx.s.flags = MEM_Null;
@@ -4810,7 +4810,7 @@
 */
 case OP_AggFinal: {        /* no-push */
   Mem *pMem;
-  assert( pOp->p1>=0 && pOp->p1<p->nMem );
+  assert( pOp->p1>0 && pOp->p1<=p->nMem );
   pMem = &p->aMem[pOp->p1];
   assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
   rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
diff --git a/src/vdbeaux.c b/src/vdbeaux.c
index 94032b4..8482863 100644
--- a/src/vdbeaux.c
+++ b/src/vdbeaux.c
@@ -971,7 +971,7 @@
     assert( nVar>=0 );
     assert( nStack<p->nOp );
     if( isExplain ){
-      nStack = 10;
+      nStack = 16;
     }
     p->aStack = sqlite3DbMallocZero(db,
         nStack*sizeof(p->aStack[0])    /* aStack */
@@ -979,12 +979,12 @@
       + nVar*sizeof(Mem)               /* aVar */
       + nVar*sizeof(char*)             /* azVar */
       + nMem*sizeof(Mem)               /* aMem */
-      + nCursor*sizeof(Cursor*)        /* apCsr */
+      + nCursor*sizeof(Cursor*) + 1    /* apCsr */
     );
     if( !db->mallocFailed ){
-      p->aMem = &p->aStack[nStack];
-      p->nMem = nMem;
-      p->aVar = &p->aMem[nMem];
+      p->aMem = &p->aStack[nStack-1];  /* aMem[] goes from 1..nMem */
+      p->nMem = nMem;                  /*       not from 0..nMem-1 */
+      p->aVar = &p->aMem[nMem+1];
       p->nVar = nVar;
       p->okVar = 0;
       p->apArg = (Mem**)&p->aVar[nVar];
@@ -1000,7 +1000,7 @@
       }
     }
   }
-  for(n=0; n<p->nMem; n++){
+  for(n=1; n<=p->nMem; n++){
     p->aMem[n].flags = MEM_Null;
     p->aMem[n].db = db;
   }
@@ -1089,7 +1089,7 @@
     p->pTos = &p->aStack[-1];
   }
   closeAllCursorsExceptActiveVtabs(p);
-  releaseMemArray(p->aMem, p->nMem);
+  releaseMemArray(&p->aMem[1], p->nMem);
   sqlite3VdbeFifoClear(&p->sFifo);
   if( p->contextStack ){
     for(i=0; i<p->contextStackTop; i++){
diff --git a/src/where.c b/src/where.c
index f2a8665..1211958 100644
--- a/src/where.c
+++ b/src/where.c
@@ -16,7 +16,7 @@
 ** so is applicable.  Because this module is responsible for selecting
 ** indices, you might also think of this module as the "query optimizer".
 **
-** $Id: where.c,v 1.269 2008/01/03 07:54:24 danielk1977 Exp $
+** $Id: where.c,v 1.270 2008/01/03 18:03:09 drh Exp $
 */
 #include "sqliteInt.h"
 
@@ -1808,7 +1808,7 @@
   ** value.  If there are IN operators we'll need one for each == or
   ** IN constraint.
   */
-  pLevel->iMem = pParse->nMem++;
+  pLevel->iMem = ++pParse->nMem;
   if( pLevel->flags & WHERE_COLUMN_IN ){
     pParse->nMem += pLevel->nEq;
     termsInMem = 1;
@@ -2260,8 +2260,7 @@
     ** row of the left table of the join.
     */
     if( pLevel->iFrom>0 && (pTabItem[0].jointype & JT_LEFT)!=0 ){
-      if( !pParse->nMem ) pParse->nMem++;
-      pLevel->iLeftJoin = pParse->nMem++;
+      pLevel->iLeftJoin = ++pParse->nMem;
       sqlite3VdbeAddOp2(v, OP_MemInt, 0, pLevel->iLeftJoin);
       VdbeComment((v, "init LEFT JOIN no-match flag"));
     }
@@ -2358,7 +2357,7 @@
         assert( pX!=0 );
         assert( pEnd->leftCursor==iCur );
         sqlite3ExprCode(pParse, pX->pRight);
-        pLevel->iMem = pParse->nMem++;
+        pLevel->iMem = ++pParse->nMem;
         sqlite3VdbeAddOp2(v, OP_MemStore, pLevel->iMem, 1);
         if( pX->op==TK_LT || pX->op==TK_GT ){
           testOp = bRev ? OP_Le : OP_Ge;
@@ -2450,7 +2449,7 @@
       }
       if( testOp!=OP_Noop ){
         int nCol = nEq + topLimit;
-        pLevel->iMem = pParse->nMem++;
+        pLevel->iMem = ++pParse->nMem;
         buildIndexProbe(v, nCol, pIdx);
         if( bRev ){
           int op = topEq ? OP_MoveLe : OP_MoveLt;
@@ -2489,7 +2488,7 @@
         int nCol = nEq + btmLimit;
         buildIndexProbe(v, nCol, pIdx);
         if( bRev ){
-          pLevel->iMem = pParse->nMem++;
+          pLevel->iMem = ++pParse->nMem;
           sqlite3VdbeAddOp2(v, OP_MemStore, pLevel->iMem, 1);
           testOp = OP_IdxLT;
         }else{