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

    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

    typedef struct 
           char word[30];
           char pos[3];
    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");
    	// 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]=='#') {
                    } else {
            /* allocate string array to write all the lines from the file */
    	list = malloc((*size)*sizeof(struct wordList*));
    	if (list==NULL) {
                    printf("\nCould not allocate  pointer array");
    	memset(temp_line, 0, 30);
            while (fgets(temp_line,sizeof(temp_line),fp)!=NULL) {
    		strncpy (list[i].word,temp_line,pch-temp_line+1);
    		memset(temp_line, 0, 30);
    main(argc, argv)
    int argc;
    char **argv;
    	int i;
      	for (i=0; i<e_size; i++) {
    	  printf("%s\t%s\n", epatterns1[i].word,epatterns1[i].pos);

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


    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

    m V
    re V
    e V
    een V
    an V

    please help... why this problem happened

    thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > 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.

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, 05:02 PM
  2. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 11:55 PM
  3. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 07:17 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

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