how to create adjacent arrays in memory?

This is a discussion on how to create adjacent arrays in memory? within the C++ Programming forums, part of the General Programming Boards category; Hi i'm new to programming. I wanted to create two integer arrays which are adjacent in memory. i had written ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    how to create adjacent arrays in memory?

    Hi i'm new to programming. I wanted to create two integer arrays which are adjacent in memory. i had written the following code, but i'm getting segmentation fault when i run it.

    #include<iostream>
    using namespace std;

    void printarray(int* a,int size)
    {
    for(int i=0;i<size;i++)
    cout<<a[i]<<endl;
    }
    Code:
    int main()
    {
            int a[3]={0,1,2,}; //the first array
            int* pa;
            pa = &(a[2]);
            pa++;
             int* b = pa; //the second array
    
    
            for(int i=0;i<3;i++)
            {
                    b[i]=i+2;
             }
            printarray(a,3);
            printarray(b,3);
    
    }
    any help would be greatly appreciated..thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    You can create one big array, then maintain pointers to the start and middle of that array, treating it as if there are two arrays contained in one.
    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
    Jul 2008
    Posts
    2
    thanks a lot

  4. #4

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    Your code looks logically correct, except you are not allocating memory for the second array (hence the seg fault). What you can do instead (in addition to laserlight's suggestion), is use a 2-dimensional array:
    Code:
    int array2D [2] [3];             // Create 2 arrays next to each other
    int (&a) [3] = array2D [0];  // 'a' is a reference to the first array
    int (&b) [3] = array2D [1];  // 'b' is a reference to the second array
    This creates a 2D array, and then gives the names 'a' and 'b' to the sub-arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  2. Help with a problem regarding dynamic memory and arrays
    By Michty in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2006, 02:26 PM
  3. Clarification of arrays and memory use
    By OOPboredom in forum C Programming
    Replies: 4
    Last Post: 03-25-2004, 02:25 PM
  4. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 03:15 PM
  5. does memcpy create a new block of memory?
    By Diamonds in forum C++ Programming
    Replies: 2
    Last Post: 02-20-2003, 12:19 AM

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