Like Tree2Likes

Delete an element in a dynamic array?

This is a discussion on Delete an element in a dynamic array? within the C Programming forums, part of the General Programming Boards category; That is what I meant, which is conveniently also what the variable "data" in your function represents, so that's nice....

  1. #31
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That is what I meant, which is conveniently also what the variable "data" in your function represents, so that's nice.

  2. #32
    C <3er
    Join Date
    Jul 2011
    Posts
    46
    Okeeey then now I understand it thanks a lot! I will try to get the delete_column to work now and I will come soon if I have problems, but I think the idea is pretty similar.. Thanks again!

  3. #33
    C <3er
    Join Date
    Jul 2011
    Posts
    46
    Ok got the delete_column function coded now but I just have one doubt, here it is:
    Code:
    /* delete_column: delete column n from table t */
    void delete_column(table *t, int n){
        int i, j;
        --(t->cols);
        char ***data = malloc(t->rows * sizeof(char **));
        if(data == NULL){
            printf("\n\t\tERROR: Out of memory\n");
            printf("\n\t\tPress [Enter] to exit...\n");
            getchar();
            exit(EXIT_FAILURE);
        }
        for(i = 0; i < t-> rows; ++i){
            data[i] = (char **)malloc(t->cols * sizeof(char *));
            if(data[i] == NULL){
                printf("\n\t\tERROR: Out of memory\n");
                printf("\n\t\tPress [Enter] to exit...\n");
                getchar();
                exit(EXIT_FAILURE);
            }
            for (j = 0; j < t->cols; j++){
                data[i][j] = (char *)malloc(NAME_MAX_LEN * sizeof(char));
                if(data[i][j] == NULL){
                    printf("\n\t\tERROR: Out of memory\n");
                    printf("\n\t\tPress [Enter] to exit...\n");
                    getchar();
                    exit(EXIT_FAILURE);
                }
            }
        }
        for(i = 0; i < t->rows; ++i){
            for(j = 0; j < n; ++j)
                data[i][j] = t->cell[i][j];
            for(j = n; j < t->cols; ++i)
                data[i][j] = t->cell[i][j+1];
            free(t->cell[i][n]);
        }
        free(t->cell);
        t->cell = data;
    }
    My doubt is should I add the line I added in red? it isn't there now and it seems to work perfectly but I don't know if it could be a memory leak not adding them and I should just free each of the strings for the cell that is being removed in each row?
    Thanks guys!

  4. #34
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That is memory that you no longer want, so freeing is appropriate.

  5. #35
    C <3er
    Join Date
    Jul 2011
    Posts
    46
    Hahah ok that's what I thought
    Thanks then! everything is working as expected thank you very much for the help!

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array and losing data of the first element
    By fx69 in forum C++ Programming
    Replies: 9
    Last Post: 02-25-2010, 05:15 AM
  2. how delete 1th element of a sorted array
    By vicky_4040 in forum C Programming
    Replies: 4
    Last Post: 10-11-2009, 07:12 AM
  3. delete same array element in C
    By tasosa in forum C Programming
    Replies: 12
    Last Post: 04-09-2009, 10:36 AM
  4. Delete Samllest Array Element
    By ptysell in forum C Programming
    Replies: 5
    Last Post: 11-22-2004, 07:27 PM
  5. Cannot delete last element in array
    By Simon in forum C Programming
    Replies: 10
    Last Post: 09-12-2002, 09:29 PM

Tags for this Thread


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