Thread: malloc/reallocing struct fields

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    2

    malloc/reallocing struct fields

    Hi, need to dynamicaly create/resize a char array
    which is a field in a struct. The struct is an element
    in an array of structs. the code compiles ok but
    the program crashes whenever I run it, i.e. doesn't
    end or exit.

    Code:
    #include <stdio.h>
    #include <errno.h>
    #include <stdlib.h>
    #include <string.h>
    
    extern int errno;
    
    
    struct stuff {
        char *dave, *temp_dave;
        int steve;
    };
    
    int main() {
    
    
        struct stuff *mate=NULL, *temp_mate;
    
    
        mate=(struct stuff *)malloc(sizeof(struct stuff));
        if(mate=NULL) {
            perror("malloc\n");
            exit(1);
       }
    
        mate[0].dave=(char *)malloc(sizeof(char));
        if(mate[0].dave==NULL)  {
            perror("malloc\n");
            exit(1);
        }
    	
    
        temp_mate=(struct stuff *)realloc(mate, 
                             sizeof(structstuff+sizeof(struct stuff));
        if(temp_mate==NULL) {
            perror("realloc\n");
            exit(1);
        }
    
        mate=temp_mate;
        free(mate[0].dave);
        free(temp_mate);
        printf("\nexit ok\n");
        return 0;
    }
    any ideas, or suggestions? Cos I'm all out. Any help would be greatly appreciated.

  2. #2
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Here is what I did with your code. I left a note when there was error checking to be done. I did not resize the struct, but it is similiar to how I did the char* dave.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    struct stuff {
        char *dave, *temp_dave;
        int steve;
    };
    
    int main(void) {
        struct stuff *mate = NULL;
    
    	mate = malloc(sizeof(struct stuff));
    
    	mate->dave = malloc(sizeof(char)*16);
    	strcpy(mate->dave, "Dave");
    	printf("%s\n", mate->dave);
    
    	mate->temp_dave = realloc(mate->dave, sizeof(char)*32);
    	// Error check
    
    	mate->dave = mate->temp_dave;
    
    	printf("%s\n", mate->dave);
    
    	free(mate->dave);
    	return 0;
    }
    Notice that you need to make the char* bigger then just sizeof(char). That is because you want it to hold several char's, not just one. In this case I used 16, but you can change it to any number.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>the code compiles ok but
    Hmm, it doesn't for me, did you cut/paste this? This was an obvious typo:
    >>sizeof(structstuff+sizeof(struct stuff));
    Did you mean something like:
    >>temp_mate=realloc(mate, sizeof(struct stuff)*2);
    (you probably didn't, but hopefully you see my point)

    Also, this is an error:
    >>if(mate=NULL)
    You've used a single equals instead of a double.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    2
    Thanks for the help guys, I did find the code worked much better when I corrected the equality test

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. union in struct with bit fields
    By sagarnewase in forum C Programming
    Replies: 4
    Last Post: 05-12-2008, 07:30 AM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM