pointer-copying

This is a discussion on pointer-copying within the C Programming forums, part of the General Programming Boards category; probably with writing one more row it accessing to memory where it shouldnt be because output gives this Code: 2. ...

  1. #16
    Registered User
    Join Date
    Dec 2010
    Posts
    30
    probably with writing one more row it accessing to memory where it shouldnt be
    because output gives this

    Code:
    2.
    2.1
    2.2
    2.3
    2.4
    Value 4
    Value 5
    Value 9
    2.5
    2.6
       He    Be    Ce
        4     5     9
    2.7
       He    Be    Ce
        4     5     9
    2.8
       He    Be    Ce
        4     5     9
    2.1
       He    Be    Ce
        4     5     9
    2.2
    2.3
       He    Be    Ce
        4     5     9
    2.4
    Value 2
    Value 7
    Value 3
       He    Be    Ce
        4     5     9
    2.5
    2.6
       He    Be    Ce
        4     5     9
       He    Be    Ce
        2     7     3
    2.7
       He    Be    Ce
        2     7     3
    2.8
       He    Be    Ce
        2     7     3
    2.1
       He    Be    Ce
        2     7     3
    2.2
    2.3
       He    Be    Ce
        2     7     3
    2.4
    Value 2
    Value 5
    Value 1
       He ♥├ľŕ‼   ě►/

  2. #17
    Registered User
    Join Date
    Dec 2010
    Posts
    30
    And my changes in code :

    Code:
    row *create_row(int values[], char tab_name[]) {
        printf("2.1\n");
        if (find_tab(tab_name)->first_row != NULL) {
            print_row(find_tab(tab_name),find_tab(tab_name)->first_row);
        }
        tab *tab_temp = find_tab(tab_name);
    
        printf("2.2\n");
        row *actual = malloc(sizeof(row));
        item *first_item;
    
        printf("2.3\n");
        item *last_added = NULL;
        int i;
        if (tab_temp->first_row != NULL) print_row(tab_temp,tab_temp->first_row);
    
        printf("2.4\n");
    
        for (i = 0; i < tab_temp->columns_count; i++) {
            item *iter = malloc(sizeof(item));
            iter->value = values[i];
            iter->next = NULL;
            if (last_added) {
                last_added->next = iter;
            }
            else {
                first_item = iter;
            }
            last_added = iter;
            printf("Value %d\n",iter->value);
        }
    
        if (tab_temp->first_row != NULL) print_row(tab_temp,tab_temp->first_row);
    
        printf("2.5\n");
        actual->first_item = first_item;
        actual->next = NULL;
    
        printf("2.6\n");
        if (tab_temp->first_row != NULL) print_row(tab_temp,tab_temp->first_row);
        if (tab_temp->first_row == NULL) {
            tab_temp->first_row = actual;
        }
        else {
            actual->next = tab_temp->first_row;
            tab_temp->first_row = actual;
        }
        if (tab_temp->first_row != NULL) print_row(tab_temp,tab_temp->first_row);
    
        printf("2.7\n");
        print_row(tab_temp,tab_temp->first_row);
    
        printf("2.8\n");
        return actual;
    }

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are the "He Be Ce" coming from a hard-coded string, or are those stored in memory somewhere? If so, we may be unmasking a bug from those strings being stored somewhere you didn't have access to.

  4. #19
    Registered User
    Join Date
    Dec 2010
    Posts
    30
    these are column names saved in structs which are in list too.


    here is part of my header file

    Code:
    typedef struct Column {
    	char name[10];
    	struct Column *next;
    	//struct Column *previous;
    } column;
    
    // item of row, items in row complete list
    typedef struct Item {
    	int value;
    	//struct Column *itemof;
    	struct Item *next;
    } item;
    
    // row is pointer on list of items
    typedef struct Row {
    	item *first_item;
    	//struct Column *itemof;
    	struct Row *next;
    } row;
    
    // tab is "pointer" on head row inculde count of columns and names of them
    typedef struct Tab {
    	char name[10];
    	int columns_count;
    	int rows_count;
    	column *first_col_name;
    	struct Tab *next;
    	row *first_row;
    	row *last_row;
    	row *spec_iter_row;
    	//struct List *previous;
    } tab;
    Last edited by Holymanus; 12-28-2010 at 10:51 AM.

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    typedef struct Column {
    	char name[10];
    	struct Column *next;
    	//struct Column *previous;
    } column;
    Yeah, so if you did that linked list anything like you did the linked-list here .... You may want go back and check that.

  6. #21
    Registered User
    Join Date
    Dec 2010
    Posts
    30
    I hope im not overweighting this structure with adding new rows (i mean from memory side)

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Holymanus View Post
    I hope im not overweighting this structure with adding new rows (i mean from memory side)
    That's not really possible. Each of these structs is, what, eight bytes? 24 bytes a row is not a huge memory deal. I'm still betting on "messed up linked list of column headings".

  8. #23
    Registered User
    Join Date
    Dec 2010
    Posts
    30
    No i meaned that OS allocated for this tab struct place and im giving there more and more but there is only pointer so it is not what getting bigger

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Holymanus View Post
    No i meaned that OS allocated for this tab struct place and im giving there more and more but there is only pointer so it is not what getting bigger
    You're not making the tab bigger, so that seems pretty irrelevant. (That is to say, the tab inly contains a pointer to the column headers and a pointer to the first row. You're not trying to put anything else in there.)
    Last edited by tabstop; 12-28-2010 at 12:02 PM.

  10. #25
    Registered User
    Join Date
    Dec 2010
    Posts
    30
    Im trying put there only things which belong there. Now it is all right. One more time thank you for help

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory error when copying char pointer
    By sismail in forum C Programming
    Replies: 8
    Last Post: 03-08-2010, 11:00 AM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 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