pointer to a structure

From: Ravi kumar.N (ravikumar.n_at_sunlux-india.com)
Date: 11/21/04

  • Next message: Siddharth Taneja: "Re: pointer to a structure"
    Date: 20 Nov 2004 22:21:57 -0800
    
    

    Respected Experts,
                          Iam Ravikumar.N, working as a juniour software
    Engineer.I have a query regarding pointer to a structure.

      
       1) Memory will be allocated to a structure when a variable is
    declared to it.

       2) Consider the following example.

             struct a{
                 int c;
                 int d;
            }

            struct a e; (When we declare 'e' as a structure variable,
    memory will be allocated to structure. Here no need of malloc for
    memory allocation)

       3)When we use a pointer to a structure . eg. struct a *g;
            The following things has to be done to access the structure
    member.
      
            3.1) Once we declare a pointer to the structure, memory
    allocation has to be done using malloc.
         
              e.g. g = (struct a*)malloc(sizeof(struct a));

           3.2)If we try to access the structure member without the above
    step done, during run-time an error saying "Memory could not be
    referenced" will be displayed.

      My query is as follows

        4)Since memory to the structure will be allocated as soon as we
    declare a structure variable, why malloc has to be done for pointer to
    the structure and why not for the structure variable like "struct a
    e";


  • Next message: Siddharth Taneja: "Re: pointer to a structure"

    Relevant Pages

    • Re: trying to make my "fillvalues" function work...
      ... void checkPtr_error(const char *location, const void *pointer) ... fprintf(stderr, "Memory allocation failure (%s). ...
      (comp.lang.c)
    • Re: Policy on rebooting?
      ... >> appropriate sizes of the pools statically was impossible. ... The problem is not the use of dynamic memory, ... so that the allocation can be ... If you can guarantee that only one pointer to a particular block is ...
      (comp.arch.embedded)
    • Re: various objects in my VB6 project - Calling IUnknown
      ... memory allocation and deallocation is half ... Doing something like this with a pointer would be a permanent leak in C++. ... Visual C++ has heap checking built ...
      (microsoft.public.vb.general.discussion)
    • Re: Linked List & Dynamic Memory Allocation
      ... you have a memory leak. ... that storage will not be available any longer. ... You must call freefor every pointer you got via malloc. ... There are three serious errors you can make using dynamic allocation: ...
      (microsoft.public.vc.mfc)
    • Re: memory
      ... statement caused a corruption of the free memory pool or allocated ... allocation routines so I could have access to the free memory pool and ... removing your internal copy of the pointer as you do. ... Dig the even newer still, yet more improved, sig! ...
      (comp.lang.c)