Thread: malloc

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    22

    malloc

    What is the use of malloc()?
    How to use it? And, what is the return of malloc()?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    this function reservs a certain amount of memory for the char type and delivers a pointer to the beginneing of the space reserved.

    Code:
    int n;
    char *s;
    ....
    scanf("%d",&n);
    s=malloc(n);
    asking for n bytes

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    22

    Thank you! How abt this?

    What does it mean?

    #define Malloc(type,n) (type *)malloc((n)*sizeof(type))

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    22
    I'm sorry that I haven't read the FAQ before posting question. Thank you for reminding me.

    What is n?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by siubo
    What is n?
    Naturally n in this case would represent the number of objects you wish to allocate.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    .........
    Join Date
    Nov 2002
    Posts
    303
    N is variable.
    Code:
    int *pointer;
    int N;
    
    N = 5;
    
    /* Allocates room for 5 integers */
    pointer = malloc(N * sizeof int);
    
    /* You can use it now like an array */
    pointer[0] = 22;
    pointer[1] = 34;
    The reason for using sizeof is because malloc needs to know how many bytes to make room for. If your wondering how malloc handles any data type it is because it's return is of type void, thus no need for a cast like in the macro you posted before. The FAQ explains it better. Goodluck.

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    22
    When I follow the FAQ to change
    #define Malloc(type,n) (type *)malloc((n)*sizeof(type))
    to
    #define Malloc(ptr,n) malloc(n*sizeof *ptr),
    however, error messages are come out:
    cannot convert 'void *' to 'double *' in initialization
    OR
    cannot convert 'void *' to 'double *' in assignment.
    Why is this so?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you changing it? Just to break it? Congradulations, you've done what you set out to do.

    The problem is that you're not using parenthesis correctly. As such you're running into problems with the * operator.

    * is for math.
    * is for pointers.

    Are you dereferencing a pointer, or trying to do math? If it's not broke, don't "fix" it.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    22
    I change it because I have a segmentation fault when I run my program. I have posted this message before.
    Somebody said that I did sth. wrong with the memory. So, I try to change it to fix the segmentaion fault. However, it seems that it's not work.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Originally posted by Salem
    If you're really writing C++, then you should be using the new operator to allocate memory... [/B]
    Not necessarily. If 'new' fails the program exits immediately, whether you had a backup plan or not. Not sure if there's a workaround for that (I'd be interested to here about one), but otherwise you can use malloc() and free() wrappers that ensure proper constructor/destructor invocations.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    OK Thanks. I totally overlooked catching the exception.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM