Thread: Expand an array

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    24

    Expand an array

    I have a simple array for math functions, but when adding larger numbers, i will need to add a column to the array. i know you can remove a slot/column using a[i]=NULL, but is there a way to add spaces to an array without re-initializing the array?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by SlyVixsky View Post
    I have a simple array for math functions, but when adding larger numbers, i will need to add a column to the array. i know you can remove a slot/column using a[i]=NULL, but is there a way to add spaces to an array without re-initializing the array?
    I think the things you know aren't necessarily so. The number of slots in an array is fixed when you declare it, and cannot be adjusted in either direction (up or down).

    If you need dynamic memory management, then you'll have to use dynamic memory management (malloc/realloc/free).

  3. #3
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by tabstop View Post
    I think the things you know aren't necessarily so. The number of slots in an array is fixed when you declare it, and cannot be adjusted in either direction (up or down).

    If you need dynamic memory management, then you'll have to use dynamic memory management (malloc/realloc/free).
    it was a code sample online, and after some modifying, i did prove my initial assumption was incorrect.

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    heres the code sample I've drawn up, showing you can easily add numbers to an array, but you have to tell the program that you added numbers. Is there any function to tell toe program you expanded the array, or should i just code in another variable counter?

    CODE:
    Code:
    #include<conio.h>
    #include<stdio.h>
    int main()
    {
    int arry[10];                   //create arry and counter
    int i;
    printf("The origional \n");     //display and fill the origional array
    for(i=0; i<10; i++) {
        arry[i]=i;
    }
    for(i=0; i<10; i++)
        printf("%d  ",arry[i]);
    
    
    arry[9]=12;                     //add more to the array
    arry[10]=15;
    arry[11]=18;
    printf("\n\nThe new array \n");         //display the new, longer array with exact numbers
    for(i=0; i<12; i++)
        printf("%d  ",arry[i]);
    
    printf("\n\nThe new array using size testing \n");   //display new array, but let the program decide its length
    for(i=0; i<(sizeof(arry)/sizeof(arry[0])); i++)
        printf("%d  ",arry[i]);
    
    getch();
    }
    OUTPUT:
    Code:
    The origional
    0  1  2  3  4  5  6  7  8  9
    
    The new array
    0  1  2  3  4  5  6  7  8  12  15  18
    
    The new array using size testing
    0  1  2  3  4  5  6  7  8  12

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Except arry[10]=15 doesn't work. You got "lucky", in that array[10] didn't cause a segfault, or memory access error, or similar. It "should" do so. (By "should" I mean morally, not that it actually will.)

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by tabstop View Post
    Except arry[10]=15 doesn't work. You got "lucky", in that array[10] didn't cause a segfault, or memory access error, or similar. It "should" do so. (By "should" I mean morally, not that it actually will.)
    True, which is why i think this program working is odd, and am looking into it

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    "arry[10] = 15" gives undefined behaviour (as does "arry[11] = 18"). Undefined behaviour means anything is allowed to happen. "Anything" includes many possibilities, including the possibility that the program might appear to behave correctly. The catch is, the actual behaviour can change (eg if you feed your code to a different compiler).

    The basic rule is that array elements should only be accessed for valid elements (in your case, arry[i] for i in the range 0 to 9 only). If you need to access other elements (i.e. indices outside the range 0 to 9) you need to specifically ensure the array is large enough to allow that or, alternatively, dynamically create the array of required size at run time BEFORE accessing it's elements.
    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.

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    24
    Quote Originally Posted by grumpy View Post
    If you need to access other elements (i.e. indices outside the range 0 to 9) you need to specifically ensure the array is large enough to allow that or, alternatively, dynamically create the array of required size at run time BEFORE accessing it's elements.
    thats the issue, i need an array that can be flexible, expanding as desired by the user.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by SlyVixsky
    thats the issue, i need an array that can be flexible, expanding as desired by the user.
    As such, use say, malloc to create the array, and realloc to expand it.
    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

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In C, that never occurs automatically. If you want an array to expand, you need to implement the process by which expansion occurs. One way is that, when a user attempts to modify a non-existent element, that you create a new (bigger) array, copy the elements across to the new array, and destroy the original. Another (assuming your arrays are dynamically created with malloc/realloc/calloc) to do that is with realloc() - but you still need to implement the logic by which realloc() is called.

    In C++, you can use standard containers to simplify some of that. However, the C++ standard containers need to be used in a particular manner - they still don't resize themselves simply through an attempt to modify or access an element.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimensional Array Addressing
    By BlackOps in forum C Programming
    Replies: 11
    Last Post: 07-21-2009, 09:26 PM
  2. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM