Pointer Help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dereksun
    New Member
    • Mar 2007
    • 2

    Pointer Help

    Anything wrong with this code? I got an error message "user breakpoint called from code at "0x7c901230 " after excute the line "delete a", please help.

    int main()
    {

    FILE *fp;
    FILE *stream = fopen("ExchID.t xt", "w");

    if((fp = fopen( "testInput.txt" , "r" )) != NULL)
    {
    char line[128];
    int i = 0;
    int *a = new int;
    int *b = new int;
    int *c = new int;
    int *d = new int;
    int *e = new int;
    int *f = new int;

    while(fgets(lin e, 128, fp))
    {
    if(sscanf(line, "%d, %d, %d, %d, %d, %d", &a[i], &b[i], &c[i], &d[i], &e[i], &f[i]) == 6)
    {
    fprintf(stream, "I:%d, %d, %d, %d, %d, %d\n",a[i], b[i], c[i], d[i], e[i], f[i]);

    dm_exch_delta_c lo((DBA_XCHG*)& a[i], (dba_clorg*)&b[i], (DBA_DATE*)&c[i], (SYS_TIME*)&d[i], (short*)&e[i], (unsigned char*)&f[i]);
    fprintf(stream, "O:%d, %d, %d, %d, %d, %d\n",a[i], b[i], c[i], d[i], e[i], f[i]);
    ++i;
    }
    else
    {
    assert(0);
    }
    }
    fclose(fp);
    fclose(stream);

    delete a;
    delete b;
    delete c;
    delete d;
    delete e;
    }

    return 0;
    }
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    a, b, c, etc are arrays so try allocating arrays, e.g.
    Code:
    int i = 0;
    int *a = new int[10];
    int *b = new int[10];
    int *c = new int[10];
    int *d = new int[10];
    int *e = new int[10];
    int *f = new int[10];

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      HI,

      The real problem in your code was you are allocating only for one block of int, after that you are incrementing the block using ++i.
      You can follow any one of these methods to solve the problem
      1)For the elements a,b etc allocate using malloc or try to use it as a array like int a[<size u require>].
      2)instead of using pointer or array for a u can declare as int a. and then pass the address of it to the sscanf funtion

      Thanks
      Raghuram

      Comment

      • dereksun
        New Member
        • Mar 2007
        • 2

        #4
        Thanks guys.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Just a message to let you know I've changed the title to be a bit more descriptive.

          Comment

          Working...