Thread: C buffer management

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    45

    C buffer management

    Hello,

    I have buffer which is 2d array implemented in C, with fixed size m x n. Buffer is filled with zeros and ones. I also defined window size for example 5 which means that I'm searching for columns with all ones in the first 5 position of the buffer. When I found columns with all ones in the window size, for example columns 2 and 4, I want to remove them and to add to my window size two new columns 6 and 7 (because window size is 5 and it must be 5 elements there, so if I remove 1 element I add another , if i remove two elements I add another two etc.) and to continue with procedure of searching columns with all ones and moving forward until end of the buffer. How can I implement in C this kind of buffer so I can remove and add items dynamically depending on what I found. Also maybe exist some solution where I don't need to delete elemnts but just to reshape my window size. Any advice will be helpful.
    Thank you

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    What have you tried? How did it work or not work?

    Generally speaking, it is not possible to remove rows or columns from a true 2D array. By true 2D array, I mean something like;
    Code:
         /*  assume m and n hold valid values */
        int array[m][n];
    You will need to move data between elements of the array (for example, move data you would put into columns 6 and 7 into columns 2 and 4 respectively). That effectively reuses rows/columns rather than removing and replacing them. Of course, you need to do bookkeeping to keep track of data (so, after moving data between array elements, you know what the result means).

    However, if you really need to remove rows or columns from an array (in the sense of explicitly allocating and deallocating them) use pointers and dynamic memory allocation/deallocation. Pointers behave in some ways that you would expect of an array, while permitting some behaviours, such as those you describe. Similarly, pointers to pointers can be used to emulate something that behaves (somewhat) like a 2D array, but allow things to be done dynamically.

    Either approach is possible, depending on how determined you are to do things dynamically. Both have trade-offs (copying data between array elements has an expense, as does dynamic memory allocation/deallocation) so it is not always obvious (without a fair amount of thought, or measuring performance) to predict which is "better" for any measure you might pick to mean "better".
    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.

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    45
    Until now I have 2d array filled with zeros and ones. It is in the form:

    unsigned long ** array; // with malloc


    0 0 1 1 0 0 1 0 0 0 0 0 0 1 0 0
    0 1 1 1 1 0 1 0 0 0 0 1 0 0 0 1
    1 0 1 1 0 0 1 0 0 0 0 0 1 0 0 1

    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15


    1) Defined window size is 5, so I'm searching elements with all ones at the positions 0,1,2,3,4
    2) When I found columns with all ones that are 2 and 3, I want now to observe new window size with elements 0,1,2,5,6
    3) In this new windows size I found one column with all ones which is 6 so I resize the window size again with elements 0,1,2,5,7 etc.


    Is there maybe some way to use 2 buffers ? I will have more comparision between the columns , this example with all ones is just
    because simplicity. And I don't need to remove elements but if it is easier I don't care.

    I can't move data from columns 2 and 4 to columns 6 and 7 because I need data from columns 6 and 7 for new comparisions.

    Thx

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Sorry, but that description doesn't illustrate what you've tried. It is simply a restatement of your original question.
    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.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    45
    I just want an advice how can I reshape my buffer adding new elements and making new window . From your answer before I didn't understand which how can I achieve this . I will have a big matrix maybe 10 000 colums and 20 rows so I thought that exist some elegant way to do this. I said that moving data between elements is not what I want because I need new data for further comaprison.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class for both dynamic allocated buffer and static buffer
    By TotalTurd in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2012, 09:09 PM
  2. regarding thread management in C
    By zahid990170 in forum C Programming
    Replies: 3
    Last Post: 04-24-2011, 12:25 PM
  3. Memory Management
    By black_spot1984 in forum C++ Programming
    Replies: 9
    Last Post: 10-08-2008, 11:25 AM
  4. Document Management
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 12-05-2004, 07:04 AM
  5. fgets(buffer,sizeof(buffer),stdin);
    By linuxdude in forum C Programming
    Replies: 2
    Last Post: 10-28-2003, 10:41 AM

Tags for this Thread