Thread: Delete an array

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    20

    Delete an array

    Can someone explain to me the process of the array deletion.
    steg by step so that i understand the whole process thank you. please dont forget to explain what happens in every loop and what happens to the delted array. does it become 0 or does it vanish and so forth

    Code:
     int i, varunummer;
        soka(antal_varor, inventory);
        for (;;)
            
        {
            int ja;
        
            printf("\nVill du avregistrera en vara ja(1) eller nej(2) ");
            scanf("%d", &ja);
            if (ja==1)
            {
            
        // printf("du har valt att avregistrera en vara\n");
        printf("Ange varunumret som du vill avregistrera?\n");
        scanf("%d", &varunummer);
        i = find_part(varunummer, antal_varor, inventory);
        
        
       
        
        
        --*antal_varor;
        for ( ; i < *antal_varor; i++)
            inventory[i] = inventory[i + 1];
                
                
            }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,590
    Arrays don't vanish until they go out of scope.

    Since you didn't actually post an array, it's hard to say what you're doing.

    Non English variable names add no clarity either.
    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
    Dec 2017
    Posts
    20
    okay but what happens to an array when it is deleted

    for example

    [10, 20, 30, 40 , 50]

    how does it work when we delete index position nr 2 = [30]

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    You don't delete an array, you delete an element inside an array. That happens by simply shifting all the elements after the one you want to delete one place to the left, overwriting it in the process. For example, if you know the index you can do:
    Code:
    for (i = index; i < arraySize - 1; ++i) {
        array[i] = array[i + 1];
    }
    --arraySize;
    The true, allocated size of the array though doesn't change, unless you use some realloc trickery.
    Devoted my life to programming...

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,590
    Quote Originally Posted by abmoh View Post
    okay but what happens to an array when it is deleted

    for example

    [10, 20, 30, 40 , 50]

    how does it work when we delete index position nr 2 = [30]
    Your array will end up containing
    [10, 20, 40 , 50, 50]
    With 4 valid elements.

    The last array position doesn't go anywhere. As far as your program is concerned, it doesn't exist as a valid array slot, but the memory is still there for as long as the array exists. You can re-add a 5th element any time you like.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2016, 01:23 AM
  2. Delete an array from memory?
    By Babkockdood in forum C Programming
    Replies: 11
    Last Post: 08-28-2010, 03:09 PM
  3. new/delete vs array on stack
    By underthesun in forum C++ Programming
    Replies: 8
    Last Post: 07-13-2009, 03:15 AM
  4. using delete to delete an array
    By iain in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2002, 03:53 PM
  5. How to delete something from the array?
    By alex6852 in forum C++ Programming
    Replies: 6
    Last Post: 11-11-2001, 07:36 PM

Tags for this Thread