Uses of malloc()?

This is a discussion on Uses of malloc()? within the C Programming forums, part of the General Programming Boards category; I have seen this function quite a bit and I also see it is important in CUDA programming, so I ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    117

    Uses of malloc()?

    I have seen this function quite a bit and I also see it is important in CUDA programming, so I decided that learning it earlier rather than later is best.

    From what I read it allocates memory but without a type, why would you want to do that? Why not just use int or char normally?

    Thanks
    My Ctrl+S addiction gets in the way when using Code Blocks...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What if you want a char or an int but you don't know how many? Then you want to pass all of those around from one function to another to do things with them. Dynamically allocate a bunch of them, pass a pointer around to them.


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

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,548
    Learn pointers before trying to learn malloc.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  4. #4
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Malloc GIVES you a slab of memory. You can do what you like with it and it can be (virtually) as large as you like. It can even change size and type as and when you like. You also can have it so your program only takes up memory when it needs (i.e. a program that has to process 60Gb of data doesn't need 60Gb of statically-allocated char's all over the place!

    In contrast, allocating an int directly in the program will only ever give you an int's worth of memory and it's allocated from something called "the stack", which is far from able to hold all your data.

    Example: You write a game. You need to read in an image file. That image file can be tiny or huge, but you need to put it into memory in order to send it to the screen. You can't know until you run the program how large that image will be or how many bytes it will use (because the user is the one telling you what file to open). Thus you find out the filesize, malloc that amount of space, and read the file into memory. When you're done, you can "free" the memory and other programs can use it again.

    But, to be honest, if you don't understand that far, I wonder what you've ever done in terms of C programming (or even CUDA) at all.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-19-2010, 02:12 AM
  2. Replies: 7
    Last Post: 10-01-2008, 07:45 PM
  3. Using malloc
    By BigFish21 in forum C Programming
    Replies: 2
    Last Post: 11-19-2007, 01:05 AM
  4. malloc.h
    By chrismiceli in forum C Programming
    Replies: 1
    Last Post: 10-13-2003, 03:46 PM
  5. When and when not to use malloc()?
    By John.H in forum C Programming
    Replies: 5
    Last Post: 03-24-2003, 05:00 PM

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