Thread: how to create adjacent arrays in memory?

  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
    28,413
    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.
    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

  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, 01: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, 02: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