union will support dynamic memory allocation?

This is a discussion on union will support dynamic memory allocation? within the C Programming forums, part of the General Programming Boards category; Hi All, recently i studied one document for difference between structure and union. I that they said, union did not ...

  1. #1
    kotin
    Join Date
    Oct 2009
    Posts
    132

    union will support dynamic memory allocation?

    Hi All,

    recently i studied one document for difference between structure and union. I that they said, union did not support the dynamic memory allocation and structure should support dynamic memory allocation.

    I tried one program with dynamic memory allocation as below

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<malloc.h>
    union koti
    {
        int i;
        float f;
        char c;
    };
    int main()
    {
        union koti  *p;
        p= (union  koti *)malloc(1 * sizeof(union koti));
        p->f =12.3;
        printf ("%f\n",p->f);
        return 0;
    }
    Am i correct? above program can prove that union will support dynamic memory allocation?

    Please correct me if i am wrong.

    Thanks in advance.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Easy way to find out... compile it and test it.

  3. #3
    kotin
    Join Date
    Oct 2009
    Posts
    132
    HI ya, i compiled and i am getting the output as 12.300000.

  4. #4
    kotin
    Join Date
    Oct 2009
    Posts
    132
    i Got the my first thread statement from below URL

    C question : Difference between structure and union.

    I am not sure as it is correct point or wrong point above URL "4. memory can be allocated dynamically in structures,where as in union can not be alloacted dynamically."

    please let me know.

    Thnaks in advance.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Of course a union can be allocated dynamically... any variable type can.

  6. #6
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Means, the point what i read for unions as"memory can be allocated dynamically in structures,where as in union can not be alloacted dynamically" is wrong? from the URL C question : Difference between structure and union.

    I think some one posted wrong point? am i right?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You've tested it yourself. You have confirmation... what more do you need?

  8. #8
    kotin
    Join Date
    Oct 2009
    Posts
    132
    Thanks for your reply's.

  9. #9
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    612
    You know that guys response is slighty dodgy when his point numbering contains two 4's, provides 7 points when he says there are 6 differences, he fails to spell union correctly and his simple demos wouldn't compile. Not to mention it's an Indian C programming site, which should make you immediately doubt the veracity of its contents.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic memory allocation
    By silver_comet in forum C Programming
    Replies: 2
    Last Post: 05-30-2011, 01:05 PM
  2. Memory allocation in an UNION
    By NKP in forum C Programming
    Replies: 6
    Last Post: 06-13-2010, 11:58 PM
  3. Dynamic memory allocation
    By Luciferek in forum C++ Programming
    Replies: 118
    Last Post: 10-02-2008, 12:34 PM
  4. Dynamic Memory Allocation
    By BoneXXX in forum C Programming
    Replies: 11
    Last Post: 03-26-2007, 02:43 AM
  5. dynamic memory allocation
    By mag_chan in forum C Programming
    Replies: 13
    Last Post: 10-21-2005, 08:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21