Results 1 to 4 of 4

Thread: about malloc

  1. #1
    Junior Member
    Join Date
    Feb 2007
    Answers
    13

    about malloc

    Hi

    When we use malloc then the memory allocated is continuous or not?
    Is there anyway to know it?


  2. #2
    Junior Member
    Join Date
    Jun 2007
    Answers
    2

    Re: about malloc

    It is continous only why because u are giving array type var......


  3. #3
    Expert Member
    Join Date
    May 2008
    Answers
    100

    Thumbs up Re: about malloc

    Yes malloc() allocates memory in contiguous location.
    To verify it see this program

    #include<stdio.h>
    void main()
    {
    int *p;
    int i;
    p=(int *)malloc(5);
    i=1;
    while(i <5)
    printf("\t&#37;u",p++);
    }
    Now depending upon the computer processor architecture(32,64 bit).the specified size of memory would be allocated for an integer. Suppose 32 bit machine (sizeof of int = 2 bytes) and starting location of allocated space be 1000.
    Then the output would be 1000 1002 1004 1006 1008


  4. #4
    Contributing Member
    Join Date
    Jun 2010
    Answers
    55

    Re: about malloc

    Memory allocated by individual malloc() calls should be contiguous. That doesn't hold for multiple calls, though. For example, given the code

    Code:
    char *p = malloc(10);
    char *q = malloc(20);
    all 10 bytes of p should be contiguous and all 20 bytes of q should be contiguous, but there's no guarantee that p and q form a contiguous 30-byte block.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact