Thread: Error copying a slice of one array to another array

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    7

    Error copying a slice of one array to another array

    Hello, I have done quite a bit of searching for a solution, and have implemented something similar, but am still getting a bug. The function that I think is giving me problems is:

    Code:
    int copyFromChar(const char * buff, int begin, int end) {
        char * temp = NULL;
        temp = malloc(end-begin);
        
        int j = 0;
        for (int i=begin; i<end; i++, j++) {
            temp[j] = buff[i];
        }
    
    //    memmove(temp, buff + begin, (end-begin));
    
    if ((strcmp(temp, "Other Picnic Table") == 0) || strcmp(temp, "Aggregate") == 0) {
            free(temp);
            return 0;
        }
        
    if ((strcmp(temp, "Round Picnic Table") == 0) || strcmp(temp, "Composite") == 0) {
            free(temp);
            return 1;
    }
    
    if ((strcmp(temp, "Square Picnic Table") == 0) || strcmp(temp, "Concrete") == 0) {
            free(temp);
            temp = NULL;
            return 2;
        }
    ...
    I have a buff
    Code:
    char *buff = NULL;
        buff = malloc(250);
    declared in the parent function.

    Then I malloc a bunch of memory for my other structs, and I iterate through a .csv file with
    Code:
    while ((c=fgetc(IN)) != EOF) {
            if (c == ',' && row > 0) {
                DB->picnicTableTable->entry[dbIndex] = malloc(sizeof(Entries));
                switch (column) {
                    case 0: // id
                        id = numberfy(buff, 0, i);
                        begin = i+1;    // this sets up the counter so "begin" starts right after the
                        column++;       // comma and I have the correct range of buff for inserting
                        break;
                    case 1: // Table Type
                        tableType = copyFromChar(buff, begin, i);
                        begin = i+1;
                        column++;
                        break;
    ...
    Sometimes my return value from copyFromChar() is correct (should be 2), and sometimes it returns my error flag (int 6). Stepping through line by line, I can see that the string copied to temp sometimes contains more data from buff than it's supposed to, but other times it is perfect.

    The for loop and the memmove are both prone to the same error, which makes me think that my problem is the area of buff that is being grabbed, but I can't for the life of me see what is wrong.

    Could a kind soul point me in the right direction?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well one problem is, your temp copy doesn't have an explicit \0 at the end (nor does it have room for one).
    Which causes all your strcmp calls to compare against garbage.

    The other is the copy is a waste of time when you can do this.
    Code:
    int copyFromChar(const char * buff, int begin, int end) {
        const char * temp = buff + begin;
        int len = end - begin;
    
        if ((strncmp(temp, "Other Picnic Table",len) == 0) || strncmp(temp, "Aggregate",len) == 0) {
            return 0;
        }
    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
    Mar 2017
    Posts
    7
    Ah, that makes sense. For some reason I forgot about using strncmp to limit how many characters were read, so any junk at the end would be ignored. Thank you very much, that fixed my bug!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To slice or not to slice? That is the question..
    By PillsburryBarry in forum General Discussions
    Replies: 8
    Last Post: 07-12-2016, 03:38 PM
  2. need help in copying unsigned char array to bit array
    By lovesunset21 in forum C Programming
    Replies: 8
    Last Post: 10-29-2010, 07:23 AM
  3. copying values of an array to another array?!
    By webznz in forum C Programming
    Replies: 8
    Last Post: 10-24-2007, 10:59 AM

Tags for this Thread