Thread: expanding an array with malloc

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

    expanding an array with malloc

    i want to ask how does malloc works on an array for instance can i dao this
    Code:
    p=100;
    int array[10];
    array = malloc(p * sizeof(int));
    then will the size of int be 100 so will i be able to do like
    Code:
    array[11] = 12 ;
    ???
    also if i have a 2d array how can i use malloc on it and how does this works with pointers????

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    For your first example, you would be better off doing

    Code:
    int *array = malloc(...)
    As for your second question, you can look in this thread:

    c malloc for two dimensional array - Stack Overflow

    Then you can access the array using array[i][j]

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by CsdJohn View Post
    i want to ask how does malloc works on an array for instance can i dao this
    Code:
    p=100;
    int array[10];
    array = malloc(p * sizeof(int));
    If you had actually tried that, you would have found all C compilers reject your code. Using an array as an lvalue (i.e. the left hand side of an assignment) is illegal.

    malloc() does not reallocate existing arrays. You use it with a pointer to allocate memory that you can then use as if it is an array. The "as if" in bold reflects the fact a pointer (and the memory allocated for it to point at) is not genuinely an array - it just behaves in a compatible manner.

    Once memory has been allocated using malloc(), another call of malloc() does not affect it. realloc() can be used to resize the memory allocated using malloc(). Like malloc(), realloc() cannot be used to resize an array - it can only be used to reallocate memory allocated using malloc() (or calloc())..

    Code:
        int *x = malloc(10*sizeof(int));
        int *temp;
        int i;
        for (i = 0; i < 10; ++i)
           x[i] = i;
        x = malloc(100*sizeof(int));    /* x can now be used as if it is an array of 100 ints */
                                                /*   The catch is that the memory allocated by the first malloc() call is not released */
    
        for (i = 0; i < 10; ++i)
           printf("%d ", x[i]);           /*  will probably not print out values 0 to 9 */
    
        temp = realloc(x, 1000*sizeof(int));
        if (temp == NULL)    /* realloc() failed */
        {
            free(x);
            exit(0);
        }
    
        for (i = 0; i < 1000; ++i)
            x[i] = i;
        free(x);    /*  clean up.   Don't use x any more after this statement */
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help with Expanding an array
    By min2max in forum C Programming
    Replies: 2
    Last Post: 06-03-2013, 02:49 PM
  2. Dynamically Expanding an Array
    By Vespasian in forum C++ Programming
    Replies: 1
    Last Post: 05-28-2013, 07:46 AM
  3. Array Expanding
    By Mcdom34 in forum C Programming
    Replies: 1
    Last Post: 06-26-2012, 10:59 PM
  4. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  5. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM