Memory allocation with malloc

This is a discussion on Memory allocation with malloc within the C Programming forums, part of the General Programming Boards category; I got a problem with the malloc function. The purpose of this program is to try to allocate memory to ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    Question Memory allocation with malloc

    I got a problem with the malloc function. The purpose of this program is to try to allocate memory to an extend when the system won't allocate memory anymore.

    Code:
    int main ()
    Code:
    {
            int * buffer;
            int counter;
    
    
            counter = 0;
            /*buffer = (int*) malloc (102400*sizeof(int));*/
            buffer = (int *)malloc(sizeof(int));
    
    
            *buffer = 1024;
    
    
            printf("buffer: %d\n", *buffer);
    
    
    
    
            while(buffer!=NULL)
            {
                    counter++;
    
    
                    *buffer = *buffer + 1024;
                    printf("count: %d\n", *buffer);
    
    
            }
    
    
            if (buffer==NULL)
            {
                    printf("Error allocating memory!\n");
                    exit (1);
            }


    When I compile and run the code it keeps on going and it will eventually run in -(number). While it should give an error with the allocation of memory.

    So I don't know what I did now, maybe someone else has a solution for it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,748
    I notice that you only call malloc once. You probably want to replace the hard coded constant 102400 with a variable, then increase the value of that variable instead, in a loop.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Quote Originally Posted by laserlight View Post
    I notice that you only call malloc once. You probably want to replace the hard coded constant 102400 with a variable, then increase the value of that variable instead, in a loop.
    So that means I need to declare a new variable and initialize it. Then replace the hardcoded 102400 with the variable. There is also one thing I don't understand and that is when i try to use the following code and try to print the output, I receive a "0":

    Code:
    *buffer = (int*) malloc (102400*sizeof(int));
    printf("buffer: %d\n", *buffer);
    How come the output isn't 102400?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    Why should it? I guess you think that 102400 will be stored where pointer buffer is pointing to! Wrong!

    Let's see the ref of malloc

    This line you have will allocate memory for 102400 ints. The contents of this memory are yet not initialized by you! But check the ref, it is explaining them better than me

    I sugest you not to cast what malloc returns .

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    4

    Question

    This is what I changed now:

    Code:
    {
            int * buffer;
            int counter;
            long test;
    
    
            test = 1024;
            counter = 0;
    
    
            buffer = (int*) malloc (test*sizeof(int));
            while(buffer!=NULL)
            {
                    counter++;
                    test+=1024;
                    printf("test: %ld\n", test);
                    printf("counter: %d\n", counter);
            }
    
    
            if (buffer==NULL)
            {
                    printf("Error allocating memory!\n");
                    exit (1);
            }
    
    
            /*free (buffer);*/
            return 0;
    
    
    }
    If i enter a very high number the if statement will be triggered but when i use a low number and use the while loop, it will continuously keep adding the number and the if statement will not be triggered so did I forget something?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,748
    Well...
    Quote Originally Posted by laserlight
    I notice that you only call malloc once.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Thank you it works!

    By the way, sorry if I asked any obvious questions..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! Using dynamic allocation and malloc
    By jh294 in forum C Programming
    Replies: 5
    Last Post: 04-01-2011, 01:13 PM
  2. Dynamic Memory Allocation (malloc vs calloc)
    By lostandconfused in forum C Programming
    Replies: 10
    Last Post: 09-01-2010, 01:56 PM
  3. Memory allocation without malloc
    By messi89 in forum C Programming
    Replies: 5
    Last Post: 05-30-2010, 07:27 AM
  4. maximum allocation using malloc
    By sarathius in forum C Programming
    Replies: 4
    Last Post: 03-03-2008, 02:15 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 08:08 AM

Tags for this Thread


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