Thread: Help me.. (Dynamic memory allocation )

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    11

    Help me.. (Dynamic memory allocation )

    i am beginner in programming. I want to understand dynemic memory allocation function. i search it google but i dont satisified with it... Can anybody plz explain me dynamic memory allocation function.. malloc(),realloc(),calloc() and freee()..


    In school mam do this example ..
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<alloc.h>
    #define NULL 0
    
    
    void main()
    {
        int *t,*p,size;
        clrscr();
    
    
        printf("Enter size");
        scanf("%d",&size);
    
    
        t=(int *)malloc(size * sizeof(int));
    
    
        if(t==NULL)
        {
            printf("No space available");
    
    
        }
    
    
        for(p=t;p<=t+size;p++)
        {
            scanf("%d",p);
        }
    
    
        printf("Display table value");
    
    
        for(p=t+size-1;p>=t;p--)
        {
            printf("%d values at %u address",*p,p);
        }
    getch();
    }


    Plzz explain me this example ..

    Thank you

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    google 'dynamic memory allocation in c'

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > #include<alloc.h>
    malloc etc are prototyped in stdlib.h

    > #define NULL 0
    NULL is declared in stddef.h

    > void main()
    main returns an int, not void.

    > t=(int *)malloc(size * sizeof(int));
    You don't need to cast malloc in a C program.
    If you get warnings without a cast, then either
    - make sure you're including stdlib.h
    - make sure you're compiling using a C compiler (and not a C++ compiler)

    > Plzz explain me this example ..
    What exactly is confusing you?
    Did you run the program and observe anything of note?

    Like for example printing the list of numbers backwards?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    956
    In addition to what others have pointed out, you continue to use an invalid (null) pointer if the memory allocation fails:
    Quote Originally Posted by Devil211272 View Post
    Code:
        if(t==NULL)
        {
            printf("No space available");
    
    
        }
    
    
        for(p=t;p<=t+size;p++)
        {
            scanf("%d",p);
        }
    Those memory accesses result in undefined behavior (usually a segmentation fault or maybe even a system crash).

    The proper format specifier for a pointer is %p, not %u:
    Quote Originally Posted by Devil211272 View Post
    Code:
            printf("%d values at %u address",*p,p);
    Using %u is not portable and may fail on systems where a pointer is a different size than an unsigned int (such as x86-64, where a pointer is 64 bits and unsigned is typically 32 bits). Then again, you also use getch(), #include <alloc.h>, #include <conio.h>, and void main, which probably already limits the program's portability to ancient DOS compilers and systems where pointers and unsigned int are always the same size.
    Last edited by christop; 10-16-2012 at 05:23 PM. Reason: add more context to quoted code

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    11
    thankx

    Output is ok. i want to print all 5 number which i entered with address.. i dont understand what our teacher want from this program ..
    i dont understand why malloc() is used.. we can print this by simple using for loop.

    here output screenshot. Help me..  (Dynamic memory allocation )-untitled-png

    in many website which i searched in google tells malloc include in <stdlib.h> . but our teacher write <alloc.h>
    i think i have c++ complier not sure .. how to check ?
    my teacher alwys write void main().. grom very first program of printing "hello world"

    i dont like my teacher because they don't explain very well. & don't like my univercity also
    if you have any good tutorial site please tell me

    thankx again in advance

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    11
    @christop
    ..
    Then what i used for
    getch(), #include <alloc.h>, #include <conio.h>, and void main ???

    i learn form first program to use getch(), void main() and <conio.h>

    HERE IS FIRST PROGRAM THAT I LEARNT.

    Help me..  (Dynamic memory allocation )-image0002-jpg


  7. #7
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    Quote Originally Posted by Devil211272 View Post
    Output is ok. i want to print all 5 number which i entered with address.. i dont understand what our teacher want from this program ..
    i dont understand why malloc() is used.. we can print this by simple using for loop.
    Yes you can perform the operation using simple loops. But, the point here is to learn how to use the dynamic memory allocation that is why your teacher has instructed you to use malloc

    here output screenshot. Help me..  (Dynamic memory allocation )-untitled-png

    in many website which i searched in google tells malloc include in <stdlib.h> . but our teacher write <alloc.h>
    i think i have c++ complier not sure .. how to check ?
    my teacher alwys write void main().. grom very first program of printing "hello world"
    From the screenshot i can see you are using TurboC which is a very old compiler and this explains why your teacher has instructed you to use <conio.h>, <alloc.h>, clrscr(), getch() all these are specific to TurboC. And i learned C programming in the same environment 10 years back and still it is used in many schools/university without any knowledge about C standards. As @christop pointed out whatever code you are writting in TurboC is going to work in only DOS and its not portable.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    11
    Thankx

    Can you give me the link of new complier / software for c language for window 7.

  9. #9
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    There are many compiler available. I am aware of GCC as that is default in linux environment but setting it up in Windows requires more work (It is not suitable for the beginners). Whereas someone else might give you a link on other compilers.

    I guess you are from INDIA(As a matter of fact I am) I certainly know most of the teachers (Of-course there are always exceptions) who thought C in schools/university have exposure to TurboC alone and they don't know any other compiler.

  10. #10
    Registered User
    Join Date
    Oct 2012
    Posts
    11
    Yes me from India. I am not living in big and popular city.. me from jamnagar,Gujarat(india). i study in saurstra university.

  11. #11
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    There is nothing wrong in learning C programming via TurboC (Provided you know the limitations it imposes). You can start learning in that environment itself and later on move to some modern compilers. One handy tip I remembered is TurboC had a very nice help section which explains all the details along with example codes. Not sure whether this is available in your case but you can give it a try and see for yourself.

    For example, place your cursor over the word malloc in your code and press Ctrl+F1 it will open a interactive help section which has details about the call.

  12. #12
    Registered User
    Join Date
    Oct 2012
    Posts
    11
    Thankx for the tip. Its work well. example is also given there

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Memory Allocation
    By slash.hack in forum C Programming
    Replies: 9
    Last Post: 09-30-2011, 04:31 AM
  2. Help with dynamic memory allocation
    By malooch in forum C Programming
    Replies: 2
    Last Post: 12-13-2006, 01:26 PM
  3. Dynamic memory allocation...
    By dicorr in forum C Programming
    Replies: 1
    Last Post: 06-24-2006, 03:59 AM
  4. dynamic memory allocation
    By inquisitive in forum C++ Programming
    Replies: 5
    Last Post: 03-13-2004, 02:07 AM
  5. Dynamic Memory Allocation?
    By motocross95 in forum C++ Programming
    Replies: 11
    Last Post: 12-03-2002, 08:52 PM