Thread: Shrinking/trimming an array?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    161

    Shrinking/trimming an array?

    I'm trying to cut data from the beginning and end of an array, but my idea isn't working. The first memcpy line compiles but crashes the program. I can't figure out the right way to do it. This array pointer crap always confused me. It should be possible to memcpy from a location in the array though. Any ideas?

    Code:
    	unsigned char *tmpResults;
        if (!(tmpResults = (unsigned char*)malloc((RamInfo.ResHigh - RamInfo.ResLow)/Search.Size/8 ))) {
            sprintf(ErrTxt, "Unable to allocate shrink results memory () -- Error %u", GetLastError());
            MessageBox(NULL, ErrTxt, "Error", MB_OK);
            goto RUN_SEARCH_ERROR;
        }
        memcpy(&tmpResults, &RamInfo.Results[RamInfo.ResLow >> 3], (RamInfo.ResHigh - RamInfo.ResLow)/Search.Size/8);
    	free(RamInfo.Results); RamInfo.Results = NULL;
        if (!(RamInfo.Results = (unsigned char*)malloc((RamInfo.ResHigh - RamInfo.ResLow)/Search.Size/8))) {
    		sprintf(ErrTxt, "Unable to allocate results memory (DO_SEARCH_CMD) -- Error %u", GetLastError());
            MessageBox(NULL, ErrTxt, "Error", MB_OK); goto RUN_SEARCH_END;
        }
        memcpy(&RamInfo.Results, &tmpResults, (RamInfo.ResHigh - RamInfo.ResLow)/Search.Size/8);
        free(tmpResults);

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's a problem with complex code in general, and especially when it involves things like pointers. Even if you get it to work right by really focusing on it, what will happen in 3 years time when you want to modify the code a bit?

    If it's like hell now, it will be like hell X 2, after you've been away from it for a few years.

    Try and make your code as simple and intuitive as feasible, and do use index numbers for working in arrays. Modern compilers and hardware are heavily optimized for them, and they ARE definitely standard C. You'll never run into a compiler that doesn't support them.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well it would help if you had some vertical white space (blank lines) and decent indentation to start with.

    > memcpy(&tmpResults, &RamInfo.Results[RamInfo.ResLow >> 3], (RamInfo.ResHigh - RamInfo.ResLow)/Search.Size/8);
    FFS, create some extra temporary variables to store the intermediate results.
    Just look at how many times you've copy/pasted RamInfo.ResHigh - RamInfo.ResLow)/Search.Size/8 for example.

    If you're trying to get to AAACC from AAABBBBBBBBCC, then 4 variables called say aStart, aLen, cStart, cLen will help you figure it out.

    Oh, and just in case you're trying to copy memory within the same block, use memmove() instead of memcpy.
    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.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Quote Originally Posted by Salem View Post
    Well it would help if you had some vertical white space (blank lines) and decent indentation to start with.

    > memcpy(&tmpResults, &RamInfo.Results[RamInfo.ResLow >> 3], (RamInfo.ResHigh - RamInfo.ResLow)/Search.Size/8);
    FFS, create some extra temporary variables to store the intermediate results.
    Just look at how many times you've copy/pasted RamInfo.ResHigh - RamInfo.ResLow)/Search.Size/8 for example.

    If you're trying to get to AAACC from AAABBBBBBBBCC, then 4 variables called say aStart, aLen, cStart, cLen will help you figure it out.

    Oh, and just in case you're trying to copy memory within the same block, use memmove() instead of memcpy.
    No, 4 vars won't help me figure anything out. I know what I'm trying to get (BBBBBBBBBB by your example, but mines not a string it's data). What I don't know is how to copymem from the middle of the array.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Post some hard to make work, examples of your data, I'd like to study them a bit.

    The program below, produces this output. Yes, it's from a string, but it could be non string data, with a few tweaks:

    Code:
    Buffer is: Hello world
    Buffer is: Hello*world
    Buffer is: Hell**world
    Buffer is: Hell***orld
    Buffer is: Hel****orld
    Buffer is: Hel*****rld
    Buffer is: He******rld
    Buffer is: He*******ld
    Buffer is: H********ld
    Buffer is: H*********d
    Buffer is: **********d
    Buffer is: ***********
    
    
    			    press enter when ready
    Code:
    #include <stdio.h>
    #include <string.h> //or mem.h
    
    int main(void)
    {
       char buffer[] = "Hello world";
       int i, len;
       len = strlen(buffer);
    
       for(i=len-1;i>-1;i--) {
         printf("Buffer is: %s\n", buffer);
         memset(buffer+(i/2),'*',len-i);  //also try buffer+i, etc.
       }
       printf("Buffer is: %s\n", buffer);
       printf("\n\n\t\t\t    press enter when ready");
       (void) getchar();
       return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Quote Originally Posted by Adak View Post
    Post some hard to make work, examples of your data, I'd like to study them a bit.

    The program below, produces this output. Yes, it's from a string, but it could be non string data, with a few tweaks:

    Code:
    Buffer is: Hello world
    Buffer is: Hello*world
    Buffer is: Hell**world
    Buffer is: Hell***orld
    Buffer is: Hel****orld
    Buffer is: Hel*****rld
    Buffer is: He******rld
    Buffer is: He*******ld
    Buffer is: H********ld
    Buffer is: H*********d
    Buffer is: **********d
    Buffer is: ***********
    
    
    			    press enter when ready
    Code:
    #include <stdio.h>
    #include <string.h> //or mem.h
    
    int main(void)
    {
       char buffer[] = "Hello world";
       int i, len;
       len = strlen(buffer);
    
       for(i=len-1;i>-1;i--) {
         printf("Buffer is: %s\n", buffer);
         memset(buffer+(i/2),'*',len-i);  //also try buffer+i, etc.
       }
       printf("Buffer is: %s\n", buffer);
       printf("\n\n\t\t\t    press enter when ready");
       (void) getchar();
       return 0;
    }
    You're missing the point completely. If I wanted to play around with loops, I'd do a function for it. I simply want to know the proper syntax to send copymem a pointer to the middle of the array (i.e. array[blah]) as the source to copy from, assuming it can be done, because copymem should be faster. I'm trying to save only the data from the middle of the array based on the first and last result of my search (RamInfo.ResLow and high) dynamically shrinking my search area after each search. Actually, the way you did the first argument for memset probably tells me what I needed to know.
    Last edited by Viper187; 03-20-2011 at 08:06 AM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    It doesn't matter whether it's a string or data - do the maths.

    > What I don't know is how to copymem from the middle of the array.
    Code:
    char buff[100];
    int bStart = 10;
    int bLen = 20;
    memcpy( destination, &buff[bStart], bLen );
    Figuring out bStart and bLen is up to you, but assuming you can do that, the rest is easy.
    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.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    161
    Works now. I really hate pointers sometimes. Been a year or more since I bothered to program anything. Trying to pick up where I left off on an old project.

    Code:
    	unsigned char *tmpResults; u32 NewResSize = (RamInfo.ResHigh - RamInfo.ResLow)/Search.Size/8;
        if (!(tmpResults = (unsigned char*)malloc(NewResSize))) {
            ShowError("Unable to allocate shrink results memory () -- Error %u", GetLastError());
            goto RUN_SEARCH_ERROR;
        }
        memcpy(tmpResults, &RamInfo.Results[(RamInfo.ResLow/Search.Size) >> 3], NewResSize);
    	free(RamInfo.Results); RamInfo.Results = NULL;
        if (!(RamInfo.Results = (unsigned char*)malloc(NewResSize))) {
    		ShowError("Unable to allocate results memory (DO_SEARCH_CMD) -- Error %u", GetLastError());
            goto RUN_SEARCH_END;
        }
        memcpy(RamInfo.Results, tmpResults, NewResSize);
        ShowError("Original: %X\nNew: %X\nResLow: %X", RamInfo.NewResultsInfo.DumpSize/Search.Size/8, NewResSize, (RamInfo.ResLow/Search.Size)>>3);
        free(tmpResults);
        RamInfo.NewResultsInfo.DumpSize = RamInfo.ResHigh - RamInfo.ResLow;
    Last edited by Viper187; 03-20-2011 at 05:32 PM.

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