Thread: about dynamic memory arrays

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    2

    about dynamic memory arrays

    I am trying to switch values stored inside arrays placed in heap. Program works like that.


    Code:
     
    void switchValue(int *a, int*b){
        
        int temp[]={1,2,3,4};
        for(int i=0; i<4; i++){
            temp[i]=a[i];
            a[i]=b[i];
            b[i]=temp[i];
        }
    }
    int main()
    {
        int *A=(int*)calloc(4,sizeof(int));
        int *B=(int*)calloc(4,sizeof(int));
        
        A[0]=1, A[1]=2, A[2]=3, A[3]=4;
        B[0]=9, B[1]=8, B[2]=7, B[3]=6;
        
       switchValue(A,B);
        
        for(int i=0;i<4;i++){
            printf("%d ",A[i]);
            }
    
    
        printf("\n");
        
        for(int i=0;i<4;i++){
            printf("%d ",B[i]);
            }}

    Compiler doesn't let me install my values like
    Code:
    A[]={1,2,3,4}
    , no surprises. But what if I have 100 or 1000 values, I have to install my values like
    Code:
    A[0]=1, A[1]=2..
    How can I store my values in heap easier?

    Another question is when I create an array like int temp[4]; program dont stop working. I have to define values inside it(can see it in 3rd row of the code). I don't understand the problem.
    Any help would be appreciated.
    Thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,541
    Well if you have 100's or 1000's of numbers, your best bet is to read them from a file, so you can assign them to your allocated array using a loop.

    > Another question is when I create an array like int temp[4];
    Because that is 'initialisation', not 'assignment'.
    You can initialise arrays as you have done here, but you can't declare an array and then later on assign the array elements en masse using a comma list.

    Also, you only need int temp;
    Re-use the same temp each time around the loop.
    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
    May 2010
    Posts
    4,458
    How can I store my values in heap easier?
    If your arrays are a "fixed" size then you could avoid using malloc/calloc and just use a static qualified array instead.

    Code:
        static int A[] = {1, 2, 3, 4};
    Another question is when I create an array like int temp[4]; program dont stop working.
    Why is temp an array instead of just a single instance?

  4. #4
    Registered User
    Join Date
    Sep 2018
    Posts
    2
    Thank you very much for both answers. And yes, I should definitely use single and static int for this code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic memory allocation (arrays)
    By newHansen in forum C++ Programming
    Replies: 8
    Last Post: 02-25-2014, 01:28 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Dynamic Memory and Arrays
    By Loctan in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2006, 04:09 AM
  4. Help with a problem regarding dynamic memory and arrays
    By Michty in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2006, 01:26 PM
  5. dynamic memory and arrays
    By jomns in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2004, 02:18 PM

Tags for this Thread