memory allocation error

This is a discussion on memory allocation error within the C Programming forums, part of the General Programming Boards category; hi every one i have a problem with memory overlap for malloced struct. I am trying to load dictionaries but,, ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    10

    memory allocation error

    hi every one
    i have a problem with memory overlap for malloced struct.
    I am trying to load dictionaries but,, after loading them i found that they have some problem

    here is the example

    seg.c
    ================
    Code:
    typedef struct 
       {
           char word[30];
           char pos[3];
       }wordList;
    
    wordList *epatterns1=NULL;
    wordList *auxillary1=NULL;
    int p_size=0,e_size=0,a_size=0;
    
    
    load_dict(wordList **list1,char * filename,int *size){
    	FILE * fp;
    	char * pch;
    	wordList *list=NULL;
    	char temp_line[40];
    	unsigned int nrows=0;                   /* number of data rows in the file */
            unsigned int comment=0;                 /* number of comment lines in file */
            unsigned int i=0;                           /* counters */
    
    	fp = fopen( filename, "r");
    	if (fp == NULL)
    	{
    			printf("\nError, file not found\n");
    	}
    	else
    	{
    	// Process & close file
    	/* count the number of comment lines, and data lines */
            while (fgets(temp_line,sizeof(temp_line),fp)!=NULL) {
                    if (temp_line[0]=='#') {
                            comment++;
                    } else {
                            nrows++;
                    }
            }
    
            /* allocate string array to write all the lines from the file */
    	*size=comment+nrows;
    	list = malloc((*size)*sizeof(struct wordList*));
    	if (list==NULL) {
                    printf("\nCould not allocate  pointer array");
            }
    
    	i=0;
            rewind(fp);
    	memset(temp_line, 0, 30);
            while (fgets(temp_line,sizeof(temp_line),fp)!=NULL) {
    		pch=strchr(temp_line,'\t');
    		strcpy(list[i].pos,pch+1);
    		list[i].pos[strlen(pch)-2]='\0';
    		strncpy (list[i].word,temp_line,pch-temp_line+1);
    		list[i].word[pch-temp_line]='\0';
    		i++;
    		memset(temp_line, 0, 30);
            }
    	fclose(fp);
    	}
    	*list1=list;
    }
    
    main(argc, argv)
    int argc;
    char **argv;
    {
    	load_dict(&epatterns1,"epatterns.dict",&e_size);	
    	load_dict(&auxillary1,"auxilllary.dict",&a_size);
    
    	int i;
      	for (i=0; i<e_size; i++) {
    	  printf("%s\t%s\n", epatterns1[i].word,epatterns1[i].pos);
    	 }
    
    }
    ===========
    epatterns.dict

    able JJ
    al JJ
    ance N
    ate V
    ation N
    cy N

    auxilllary.dict

    am AV
    are AV
    be AV
    been AV
    can AV

    when i try to print the ausillary dict stored in the wordList struct
    program outputs

    able
    m V
    re V
    e V
    een V
    an V


    please help... why this problem happened

    thanks in advance
    prakash

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > list = malloc((*size)*sizeof(struct wordList*));
    You want the size of a wordList, not a pointer to a wordlist

    > main(argc, argv)
    > int argc;
    > char **argv;
    This syntax is archaic. How old is the book you're reading?

    > memset(temp_line, 0, 30);
    a) it's the wrong size
    b) it serves no purpose, as fgets always adds a \0 anyway.

    > strcpy(list[i].pos,pch+1);
    This will also copy a \n, thus blowing away the length of your small array, and trashing something else.
    Consider strncpy(), or removing the \n (see the FAQ)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  2. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 06:17 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21