CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2012
    Posts
    1

    Question malloc for union inside structure fails.

    Hi Guru's,
    I have some problem while allocating memory to a union inside structure. Below is the code i am using

    ON SYSTEM1:
    This works fine

    ON SYSTEM2:
    compiler complains saying "need structure or union type" while allocating MYSTRUCT1.
    if i change,
    shreyas[0].UnionAttr.struct1 = (MYSTRUCT1 *) malloc (sizeof(MYSTRUCT1)
    to
    shreyas[0].UnionAttr->struct1 = (MYSTRUCT1 *) malloc (sizeof(MYSTRUCT1)

    This compiler on SYSTEM2 is happy. but second way does not look correct to me and compiler on system 1 complains about it.

    Can someone pls help know which is the correct way to allocate memory?
    If first one is correct then what should i look in for to avoid this error?
    Could this be an issue with compiler on SYSTEM2?

    NOTE: if i use second method on SYSTEM2 code segfaults during malloc.

    Please help ASAP, i am stuck because of this for long time.

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>


    typedef struct mystruct1 {
    int a;
    int b;
    } MYSTRUCT1;

    typedef struct mystruct2 {
    int a;
    int b;
    } MYSTRUCT2;

    typedef union myunion {
    MYSTRUCT1 *struct1;
    MYSTRUCT2 *struct2;
    } MYUNION;

    typedef struct mystruct {
    int a;
    MYUNION UnionAttr;
    } MYSTRUCT;

    int main()
    {
    int flag = 1;
    int i;
    // Allocate memory
    MYSTRUCT *shreyas;
    if ((shreyas = (MYSTRUCT *) calloc (2,sizeof(MYSTRUCT))) == NULL)
    fprintf(stderr, "Error allocating mystruct\n");
    // Allocate to union elements
    if ((shreyas[0].UnionAttr.struct1 = (MYSTRUCT1 *) malloc (sizeof(MYSTRUCT1))) == NULL) {
    fprintf(stderr, "Error allocating mystruct1\n");
    free(shreyas);
    }

    if ((shreyas[1].UnionAttr.struct2 = (MYSTRUCT2 *) malloc (sizeof(MYSTRUCT2))) == NULL) {
    fprintf(stderr, "Error allocating mystruct2\n");
    free(shreyas);
    }
    // Access elements
    shreyas[0].UnionAttr.struct1->a = 10;
    shreyas[1].UnionAttr.struct2->a = 20;
    shreyas[0].UnionAttr.struct1->b = 100;
    shreyas[1].UnionAttr.struct2->b = 200;

    printf("Struct 1 elements a = %d b = %d\n", shreyas[0].UnionAttr.struct1->a, shreyas[0].UnionAttr.struct1->b);
    printf("Struct 2 elements a = %d b = %d\n", shreyas[1].UnionAttr.struct2->a, shreyas[1].UnionAttr.struct2->b);
    printf("Success\n");
    return;


    }

  2. #2
    Join Date
    May 2009
    Posts
    2,413

    Re: malloc for union inside structure fails.

    VS2010 compiles the posted code (except that main() must return a value).

    Your code is so C-ish. Why not graduate to C++? Rather than a union of pointers you could use inheritance (two (or more) structs/classes derived from a common base struct/class). And you could use new/delete for allocations.
    Last edited by nuzzle; December 27th, 2012 at 03:33 AM.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: malloc for union inside structure fails.

    Quote Originally Posted by shreyaswaghmare View Post
    Hi Guru's,
    I have some problem while allocating memory to a union inside structure.
    If your problem is a compilation issue, it would help if you posted the name and version of the compiler you're using. The issue as you've described is not a run-time issue with malloc(), it is an issue with source code management.

    Secondly, if it is the same compiler, then obviously your code is different on the two systems. A compiler just doesn't give out errors because it is run on another computer -- the code must be different in some way on the machines, or the compilers are different (different compiler brand and/or version).

    Regards,

    Paul McKenzie

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: malloc for union inside structure fails.

    I would throw away the compiler you are using for system2!! You don't say what compilers you are using but your code works fine for me using Visual Studio 2003 onwards. Note that if the call to calloc fails, then your program doesn't terminate as it probably should but just outputs an error to stderr and trys to continue with all sorts of bad memory problems!!! Also if the malloc call for MYSTRUCT2 fails then you are not freeing the memory already allocated for MYSTRUCT1. Personally when I use pointers I nearly always typedef a pointer definition so that I don't have a load of * cluttering up my code. As below

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    typedef struct mystruct1 {
    	int a;
    	int b;
    } MYSTRUCT1, *PMYSTRUCT1;
    
    typedef struct mystruct2 {
    	int a;
    	int b;
    } MYSTRUCT2, *PMYSTRUCT2;
    
    typedef union myunion {
    	PMYSTRUCT1 struct1;
    	PMYSTRUCT2 struct2;
    } MYUNION;
    
    typedef struct mystruct {
    	int          a;
    	MYUNION UnionAttr;
    } MYSTRUCT, *PMYSTRUCT;
    
    int main()
    {
    PMYSTRUCT shreyas;
    
    	//Allocate memory
    	if ((shreyas = (PMYSTRUCT)calloc(2, sizeof(MYSTRUCT))) == NULL) {
    		fprintf(stderr, "Error allocating mystruct\n");
    		return (1);
    	}
    
    	// Allocate to union elements
    	if ((shreyas[0].UnionAttr.struct1 = (PMYSTRUCT1)malloc(sizeof(MYSTRUCT1))) == NULL) {
    		fprintf(stderr, "Error allocating mystruct1\n");
    		free(shreyas);
    		return (2);
    	}
    
    	if ((shreyas[1].UnionAttr.struct2 = (PMYSTRUCT2)malloc(sizeof(MYSTRUCT2))) == NULL) {
    		fprintf(stderr, "Error allocating mystruct2\n");
    		free(shreyas[0].UnionAttr.struct1);
    		free(shreyas);
    		return (3);
    	}
    
    	// Access elements
    	shreyas[0].UnionAttr.struct1->a = 10;
    	shreyas[1].UnionAttr.struct2->a = 20;
    	shreyas[0].UnionAttr.struct1->b = 100;
    	shreyas[1].UnionAttr.struct2->b = 200;
    
    	printf("Struct 1 elements a = %d b = %d\n", shreyas[0].UnionAttr.struct1->a, shreyas[0].UnionAttr.struct1->b);
    	printf("Struct 2 elements a = %d b = %d\n", shreyas[1].UnionAttr.struct2->a, shreyas[1].UnionAttr.struct2->b);
    	printf("Success\n");
    
    	return (0);
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured