Thread: allocating memory problems

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    11

    allocating memory problems

    i am having trouble allocating space in my program.
    and arent getting the results i would like.
    here is my code:

    Code:
    int
    main(int argc, char **argv)
    {
            int  input;
    	
    	/* Parse Arguments */
    	clo_t *iopts = parse_args(argc, argv);
    	
    	FILE *fp = fopen(iopts->input_file,"r");
    	
    	/* Use fscanf to read all data values into new set_t */
    	
    	/*data_set*/
            set_t * data_set = set_create(1000);
    
    
            while(fscanf(fp, "%d", &input) != EOF)
    	{
    	    printf("data items: %i\n", data_set->items);
    	    printf("data: %i\n", data_set->data[data_set->items]);
    	    
    	    set_insert(data_set, input, data_set->data);
                  
    
    	}
    
    
    	return (EXIT_SUCCESS);
    }

    safe_malloc function i was provided with and the safe_realloc function i tried to make,
    old_mem + 1000 dosnt seem right and i get a segfault when i try to use safe_realloc.
    Code:
    void *
    safe_malloc(size_t size)
    {
    	void *mem_block = NULL;
    
    	if ((mem_block = calloc(1, size)) == NULL) {
    		fprintf(stderr, "ERROR: safe_malloc(%zu) cannot allocate memory.",
    			size);
    		exit(EXIT_FAILURE);
    	}
    	return (mem_block);
    }
    
    
    
    void *
    safe_realloc(void *old_mem, size_t new_size)
    {
           if(realloc(new_size, (old_mem + 1000) * sizeof(int)) == NULL)
           {
                   printf("stderr, ERROR: safe_realloc cannot allocate memory.\n");
    	       exit(EXIT_FAILURE);
           }
    
          
       
       return(new_size);
    }
    this is where i call safe_realloc
    Code:
    int
    set_insert(set_t *set, int item, int *set_size)
    {
      
    
      if(set->lock == TRUE)
      {
          printf("Set locked. cannot be altered at this time\n");
          return (TRUE);
      }
      
      if(set->items >= set->n_max)
      {
          /*realloc more space*/
          set->data = safe_realloc(set_size, 1000 * sizeof(int));
          
          set n_max to the new size*/
          set->n_max = *set_size + 1000;
    
          
      }
      
      set->data[set->items] = item;
    
      set->items++;
      
    	return (TRUE);
    }
    Code:
    set_t *
    set_create(int size)
    {
      printf("size: %d\n", size);
      
    
    
      set_t * set = safe_malloc(sizeof(set_t));
      
      set->data = safe_malloc(size * sizeof(int));
      
      set->items = 0;
      
      printf("items: %d\n", set->items);
      
      set->n_max = size;
      
      set->lock = FALSE;
      
      return(set);
    
    }
    and the set_t struct is:
    Code:
    typedef struct 
    {
      int *data;
      int items;
      int n_max;
      int lock;
    } set_t;
    my program prints the data items from 1 - 1000, data = 0 every time except for the last when it equals 8201, and n_max = 1000. the numbers in the file go from 0 to 11000, and im supposed to keep reallocating more space until it is done reading them all. and i cant get data to equal the right thing. Any ideas?

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    The thing that jumped out at first was your call to realloc. The declaration of realloc is like this:
    Code:
    void * realloc ( void * ptr, size_t size );
    So change your code to reflect this, ie:

    Code:
    void *
    safe_realloc(void *old_mem, size_t new_size)
    {
           if(realloc(old_mem, (new_size + 1000) * sizeof(int)) == NULL)
           {
                   printf("stderr, ERROR: safe_realloc cannot allocate memory.\n");
    	       exit(EXIT_FAILURE);
           }   
       return(new_size);
    }
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    thanks bernt, i have the same result though, and a warning that return makes pointer from integer in safe_realloc, what does this mean?

    i forgot to mention that when i call safe_realloc i want to increase the memory block by 1000 ints every time until it all fits. im thinking now that if i set new_size to: 1000 * sizeof(int) like i am when i send it to safe_realloc the best result i can hope of with how my code is now is that new_size will be returned as 2000 int's, instead of the size increasing by 1000 every time.
    how can i send the current size of the memory block to safe_realloc?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by bernt View Post
    The thing that jumped out at first was your call to realloc. The declaration of realloc is like this:
    Code:
    void * realloc ( void * ptr, size_t size );
    So change your code to reflect this, ie:

    Code:
    void *
    safe_realloc(void *old_mem, size_t new_size)
    {
           if(realloc(old_mem, (new_size + 1000) * sizeof(int)) == NULL)
           {
                   printf("stderr, ERROR: safe_realloc cannot allocate memory.\n");
    	       exit(EXIT_FAILURE);
           }   
       return(new_size);
    }
    how are you planning to access memory allocated by realloc? you have not stored the returned pointer anywhere
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    11
    i got it working,


    Code:
    void *
    safe_realloc(void *old_mem, size_t new_size)
    {
    
           void * new_address = realloc(old_mem, new_size);
    
    
           if(new_address == NULL)
           {
                   printf("stderr, ERROR: safe_realloc cannot allocate memory.\n");
    	       exit(EXIT_FAILURE);
           }
    
       
       return(new_address);
    }
    thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with custom dynamic memory allocation routines
    By BLauritson in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2010, 07:26 AM
  2. Memory Allocation Problems
    By amac in forum C Programming
    Replies: 9
    Last Post: 12-10-2009, 06:49 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Allocating Memory for a Structure
    By surfxtc79 in forum C Programming
    Replies: 4
    Last Post: 06-05-2003, 11:40 AM
  5. Tile Engine Problems with Video Memory
    By Tommaso in forum Game Programming
    Replies: 8
    Last Post: 03-07-2003, 08:26 PM