Thread: Malloc, Realloc and Calloc Help/Suggestion

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    23

    Malloc, Realloc and Calloc Help/Suggestion

    Code:
    #include <stdio.h>
    #include <stdbool.h>
    #include "project1.h"
    
    
    int main()
    {
    int i, r, NUM_ITEMS, t, ret, ava;
    int*  numbers;
    NUM_ITEMS = 0;
    ava = 100;
    
    
    
    numbers =  malloc(100 * sizeof(int));
    while(((ret = scanf("&#37;d",&t)) != EOF) && (ret != 0))
    {   
        if (NUM_ITEMS <= ava)
        {
    	numbers[NUM_ITEMS] = t;
    	NUM_ITEMS++;
    } else
    {
     numbers = realloc(numbers, sizeof(numbers) + sizeof(int)*100);
     ava = ava + 100 ;  
     printf("%d\n",t);
    	numbers[NUM_ITEMS] = t;
    	printf("\t Array at %d = %d\n",NUM_ITEMS, numbers[NUM_ITEMS]);
    	NUM_ITEMS++;
    }    	
     
     
    }
     
    r = bubbleSort(numbers, NUM_ITEMS);
    	 
    r =  insertionSort(numbers, NUM_ITEMS);
     
    }
    I am doing a proj for class. the point is to make some sort function and let them have at. The problem is I am having issues with malloc. I though I understood the memory alloction stuff pretty well but when ever I try to run this on a file with alot of numbers (say 100000) I run into memory issues and the computer I am compiling on gets all upset. I have the sorts running and sorting the same number of element when I use a a size I define in the code.

    So, my question is, is my problem a result of the way I am mallocing here or should I look at me calloc call in my sorts or is it the computer?

    Here is an example of a sort

    Code:
    /*********************************************************
    int bubbleSort(int * a, int n);
    
    *********************************************************/
    int bubbleSort(int * number, int array_size)
    {
    int i, j, r, temp;
    int * array_sort;	
    
    	//Allocating array of same size as passed array.
    	array_sort = (int *)calloc(array_size,sizeof (int));
    
    	if(array_sort != NULL)
    	{
    	//printf("Array created of size \n",array_size);
            }
            else
            {
                    //printf("Not enough memory\n");
                    exit(1);
            }
    // Bubble Code
    	
    // Clean-up
     free (array_sort);   
    	return(0);
    }
    Thanks for any suggestion
    Last edited by zensatori; 04-13-2007 at 06:50 PM.

  2. #2
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Code:
    -bash-3.1$ ./proj1 < 100000.txt
    463480570
             Array at 101 = 463480570
    *** glibc detected *** ./proj1: realloc(): invalid next size: 0x0969a008 ***
    ======= Backtrace: =========
    /lib/libc.so.6[0x94181b]
    /lib/libc.so.6(__libc_realloc+0xec)[0x943605]
    ./proj1[0x8048558]
    /lib/libc.so.6(__libc_start_main+0xdc)[0x8f1724]
    ./proj1[0x8048471]
    ======= Memory map: ========
    This is what I see.
    But is a send it 10000 elements it works fine.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Does this work?
    Code:
    #include<stdlib.h>
    int main( void )
    {
        int *numbers = malloc( 100000 * sizeof *numbers );
        if( number )
            free( numbers );
        return 0;
    }
    Or does it crash? I really doubt your issue is with malloc. You're probably blowing your stack space all to hell.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Here are a couple of Things Not To Do:
    Code:
    while(((ret = scanf("&#37;d",&t)) != EOF) && (ret != 0))
    {   
        if (NUM_ITEMS <= ava)
    It would be better to check the scanf attempt against success; i.e., check whether the return value is 1. But that's just my footnote; you do check, but in an unnecessary manner.

    And arrays of size N may be indexed from 0 to N-1. The <= goes one beyond the end of the array.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    23
    Quote Originally Posted by Dave_Sinkula View Post
    Here are a couple of Things Not To Do:
    Code:
    while(((ret = scanf("%d",&t)) != EOF) && (ret != 0))
    {   
        if (NUM_ITEMS <= ava)
    It would be better to check the scanf attempt against success; i.e., check whether the return value is 1. But that's just my footnote; you do check, but in an unnecessary manner.

    And arrays of size N may be indexed from 0 to N-1. The <= goes one beyond the end of the array.
    Thanks for the suggestion. I kind of forgot that scanf returns anything :/

    So, my malloc ing looks ok?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by zensatori View Post
    So, my malloc ing looks ok?
    That depends. What doe sizeof( int ) give you? It's possible that 100,000 won't fit in your int.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > numbers = realloc(numbers, sizeof(numbers) + sizeof(int)*100);
    Well this doesn't expand your array by 100 ints, not by a long way.

    sizeof(numbers) just tells you the pointer size, not how much memory you're pointing at.
    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.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Expanding on that: sizeof(numbers) is like saying sizeof(int *), which will probably be 2 or 4. You probably want to use ava * sizeof(int) or something.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difference BW malloc() and calloc() and realloc()
    By svelmca in forum C Programming
    Replies: 2
    Last Post: 03-31-2009, 05:59 AM
  2. malloc and realloc
    By jayfriend in forum C Programming
    Replies: 4
    Last Post: 01-05-2007, 02:25 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Linked list versus malloc and realloc.
    By Bajanine in forum C Programming
    Replies: 2
    Last Post: 06-20-2005, 08:08 PM
  5. confused about arrays
    By sal817 in forum C Programming
    Replies: 17
    Last Post: 09-20-2004, 03:45 PM