Thread: Memory Access Error when using Strcpy()

  1. #1
    fgs3124
    Guest

    Unhappy Memory Access Error when using Strcpy()

    The following code compiles ok but I get the general 'memory access violation' error when executing it. I believe the error occurs with the Strcpy() function when I attempt to copy a pointer variable (token) to a char variable (pat). Any way to accomplish this? Thanks for the help:

    #include <sys/types.h>
    # include <iostream.h>
    # include <string.h>
    //These two include files are mandatory.

    int bmsearch(char *text,char *pat,int *no,int *pos)
    {
    int len=strlen(pat);
    int compcount=0;
    int ptct=len-1;
    int i,table[256];
    *no=0;
    for(i=0;i<256;i++)table[i]=len;
    for(i=0;i<len;i++)table[pat[i]]=len-(i+1);
    while(ptct<strlen(text))
    {
    int count=0;
    while(count<len)
    {
    if(text[ptct-count]!=pat[len-1-count]){compcount++;break;}
    else count++;
    }
    if(count==len)
    {
    (*no)++;
    *(pos+(*no)-1)=(ptct-count+1);
    ptct+=len;
    }
    else
    {ptct+=table[text[ptct-count]];}
    }
    return compcount;
    }


    /****This is a test program for the bm algorithm
    ************************************************/

    # include <conio.h>
    # include <stdio.h>

    main(void)
    // int argc;
    // char *argv[];

    {
    FILE *fptrin;

    char pat[100];
    int no,count,pos[20],i;
    int pos_ave = 0;
    char txtline[140];
    char txtfile[]=" ";
    int length,len;
    char *token;
    char seps[] = " ,\t\n";


    length=strlen("e003stmt.dat");
    strncpy(txtfile,"e003stmt.dat",length);
    if((fptrin=fopen(txtfile,"rb"))==NULL)
    printf("Can't Open In File !\n");
    // printf("Enter the pattern string:");
    // gets(pat);
    while(fgets(txtline,139,fptrin)!=NULL)
    {
    printf("%s\n\nTokens:\n",txtline);
    /* Establish string and get the first token: */
    token = strtok(txtline,seps);
    while(token != NULL)
    {
    /* While there are tokens in "string" */
    printf(" %s\n",token);
    /* Get next token: */
    token = strtok(NULL, seps);
    }

    strcpy(pat,token);
    count = bmsearch(txtline,pat,&no,pos);
    printf("\n\nPattern string occurred %d times",no);
    printf("\n%d comparisons required",count);
    printf("\nPositions of occurrence:\n");
    for(i=0;i<no;i++)
    {
    printf("%d\n",pos[i]);
    switch(i)
    {
    case 0:
    break;
    default:
    pos_ave = pos_ave + pos[i];
    break;
    }
    }
    if(pos_ave!=NULL)printf("pos_ave = %d\n",(pos_ave)/i++);
    printf("Thank you\n\n");
    return 0;
    }
    }

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633

    Re: Memory Access Error when using Strcpy()

    Originally posted by fgs3124
    Code:
    while(token != NULL)
    {
      /* While there are tokens in "string" */
      printf(" %s\n",token);
      /* Get next token: */
      token = strtok(NULL, seps);
    }
    
    strcpy(pat,token);
    Well, this might be a problem. You intend to call strtok() until it returns NULL (and assign the NULL to token), and then you attempt to copy whatever is at token (0x0) to pat. You are passing a NULL pointer as the source for your strcpy().

    Also, consider using strncpy() instead of strcpy() in the event whatever token points to is larger than the 100 bytes reserved for pat.
    Jason Deckard

  3. #3
    fgs3124
    Guest

    Talking

    Thanks . . . that does help alot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  2. typical access time of memory
    By Lind in forum Tech Board
    Replies: 5
    Last Post: 10-17-2007, 04:12 PM
  3. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  4. Memory access I can't get
    By caroundw5h in forum C Programming
    Replies: 25
    Last Post: 03-04-2005, 05:42 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM