Thread: Dynamic array allocation

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

    Dynamic array allocation

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    
    int main()
    {
        int n, i;
        printf("\nEnter a number: \n");
        scanf("%d",&n);
        int * a = (int*) malloc(n*sizeof(int));
        int * b = (int*) malloc(n*sizeof(int));
        for(i=0;i<n;i++)
        a[i]=i*i;
    
        b=a;
    
    
        for(i=0;i<n;i++)
        {
        printf("%d: \n",b[i]);
        }
    free(a);
    free(b);
        return 0;
    }
    In the above code, I define pointers "a" and "b". Then I assign some values to "a". Then I say b = a. Here, does it mean that I have assigned memory locations of "a" to "b"? or I have assigned values stored in array "a" to array "b"?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > b=a;
    You can't assign arrays, and you can't copy one block of memory to another just through pointer assignment.

    You need another loop
    for ( i = 0 ; i < n ; i++ ) b[i] = a[i];
    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.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    The program works if you just comment out either of the lines free(a); and free(b);.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Quote Originally Posted by abhishekd18 View Post
    The program works if you just comment out either of the lines free(a); and free(b);.
    Well that makes it all ok then - bonus, you get two choices of lines to comment out to hack a working program - erm..
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    But I want to understand what is happening here!

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    By doing b=a you are only copying the address. This effectively destroys what 'b' contained... Therefore doing a free(a) and free(b) will attempt to free the same area twice. But the memory that the original 'b' pointed to continues to be allocated.
    I think what you meant to do was to copy the data from one array to the other. Use memcpy(b, a, n * sizeof(int)).

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    4
    Thanks for the reply! That was helpful. But then I have one more confusion. Then why can't we access the array elements by "*a[i]" I mean because "a" is pointer if I want to get value of "a", I should get it by dereferencing. But this gives error and a[i] works perfect. In other words, "a[i]" should print address itself not the value at address.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by abhishekd18
    Then why can't we access the array elements by "*a[i]" I mean because "a" is pointer if I want to get value of "a", I should get it by dereferencing. But this gives error and a[i] works perfect. In other words, "a[i]" should print address itself not the value at address.
    Indeed, a is a pointer: a is a pointer to int. Therefore *a is an int. Now, a[i] is equivalent to *(a + i), therefore, a[i] is also an int: in particular, *a == a[0]. However, *a[i] means *(a[i]), i.e., *(*(a + i)). But *(a + i) is an int, hence *a[i] attempts to dereference an int, not a pointer, which is wrong.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic array allocation
    By Maulrus in forum C++ Programming
    Replies: 7
    Last Post: 03-09-2010, 06:08 PM
  2. Dynamic array Allocation
    By deepak_j in forum C Programming
    Replies: 3
    Last Post: 08-17-2009, 07:18 AM
  3. dynamic 2D array allocation
    By deprekate in forum C Programming
    Replies: 5
    Last Post: 03-03-2009, 04:25 AM
  4. dynamic allocation of 2d array
    By owi_just in forum C Programming
    Replies: 4
    Last Post: 05-09-2005, 12:50 AM
  5. Dynamic 3D array allocation
    By Vulcan in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 02:51 AM