Thread: Array of Linked Lists: Status Access Violation!!

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    5

    Array of Linked Lists: Status Access Violation!!

    I'm building a 'simulated' web browsing experience that simply intakes character strings, pretends their URLs, and does what a web browser would do. It creates a cache and moves strings that have already been encountered to the front of the list etc. Note it begins with an array of structures that contain pointers to linked lists.

    The problem i'm having is when i call any of my functions all i get is:
    Code:
    0 [main] WebBrowse 5456 exception::handle: Exception: STATUS_ACCESS_VIOLATION
    307 [main] WebBrowse 5456 open_stackdumpfile: Dumping stack trace to WebBrowse.exe.stackdump
    here is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define HASHSIZE 101
    #define MAXLIST 5
    struct hashlist{
    	int listsize;//number of items in linked list
    	struct linklist *head;//pointer to head of linked list
    	struct linklist *tail;//pointer to tail of linked list
    };
    struct linklist{
    	struct linklist *next; // pointer to next entry in chain
    	char *str; // page name
    	int count; // count for occurrence
    };
    static struct hashlist *hashtab[HASHSIZE]; // pointer table
    
    /*hash: form hash value for string s */
    unsigned int hash(char *s){
    	unsigned hashval;
    
    	for(hashval = 0; *s != '\0'; s++)
    		hashval = *s + 31 * hashval;
    	return hashval % HASHSIZE;
    }
    /*builds the structure with input string*/
    struct linklist *buildNode(char *s){
    	struct linklist *temp = NULL;
    	temp = malloc(sizeof(struct linklist));
    
    	temp->str = s;
    	temp->count = 0;
    	temp->next = NULL;
    	return temp;
    }
    /*searches linked list to see if string already exists*/
    struct linklist *search(char *s){
    	struct linklist *np;
    
    	for(np = hashtab[hash(s)]->head; np != NULL; np = np->next){
    		if(strcmp(s, np->str) == 0)
    			return np; //found it
    	}
    	return NULL; //didn't find it
    }
    /*inserts structure into list*/
    struct linklist *insert(char *url){
    	struct linklist *test = NULL;
    	struct linklist *ins = NULL;
    
    	if((test = search(url)) == NULL) //doesnt yet exist
    		ins = buildNode(url);
    	hashtab[hash(url)]->head = &ins;
            return ins;
    }

    when i use:
    Code:
    int main(){
    	setvbuf(stdout, NULL, _IONBF, 0);
    	char s[] = "test";
    	insert(s);
    	return 0;
    }
    the above error results. I understand the conceptual aspect of the linked list and how the hash function works, but i can't get my code to stop giving me this error.

    Am i using pointers incorrectly, or using values when pointer parameters are called? What does Status_Access_Violation signify? Any help would be greatly appreciated. If i was unclear with any of these let me know. Please help!

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You are taking address of local variable.

    hashtab[hash(url)]->head = &ins;

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    5
    But it happens when i call the search function as well, which only has a local structure. From what i can gather the address of something is being accessed simultaneously, but i can't see it. Can you see it or walk me through where i went wrong?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Also in your buildNode() function, you might want to make a copy of string.
    Btw, Are you ignoring your compiler warning?

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    5
    There is no compiler warning. In fact it compiles fine, it's only in runtime that it gives me the Status Access Violation error. I've been messing around with linked lists all weekend and i've had no problems. It's only when i try to make an array of structures where each element points to a linked list where i run into issues.

    Anyways on my machine there is no compiler warning or error. I'm using Eclipse Helios on Windows 7 Ultimate 64.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Zyreal687
    But it happens when i call the search function as well, which only has a local structure.
    Bayint Naung is absolutely right about this problem. Fix this problem first and see if both your problems are solved. If not, solve the remaining problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2010
    Posts
    5
    Okay here is the revised version:

    Code:
    struct node *buildNode(char *s){
    	struct node *temp = NULL;
    	struct hashlist *headList = hashtab[hash(s)];
    	temp = malloc(sizeof(struct node));
    	char x[strlen(s)+1];
    	strcpy(x, s);
    
    	temp->str = x;
    	temp->occur = 0;
    	temp->next = headList->head;
    	headList->head = temp;
    	return temp;
    }
    I copied the input string and dealt with it in local. However calling this function still results in a STATUS_ACCESS_VIOLATION. Commenting out these lines:
    Code:
    	temp->next = headList->head;
    	headList->head = temp;
    Removes the error. So anytime i deal with any array element in the array hashtab[] i receive this error. What is the correct way to call an element from an array of structures?

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    char x[strlen(s)+1]; // this is local variable! again!!!

    You are using C99 Variable Length Array(VLA).
    Read Question 7.5a.

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    5
    Thank you Bayint, that link was very helpful.
    I've been working on it all week and that bit of information really solved all my segfaults.
    It works now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 08-01-2007, 06:17 AM
  2. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. Array, Linked List, or Binary Tree?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 01-05-2002, 10:07 PM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM

Tags for this Thread