1

Let's assume that I have a char variable that is going to hold 1000 bytes.

char var[1000];

How would I use malloc to allocate that much memory for whatever is going to be in there?

I've tried reading up on malloc, but even K&R did not seem to have much info on it.

dikidera
  • 2,004
  • 6
  • 29
  • 36
  • 4
    "K&R did not seem to have much info on it" - Looks like you've read a different book :) – codaddict Nov 24 '11 at 10:55
  • have you considered [this question](http://stackoverflow.com/questions/1119134/how-do-malloc-and-free-work) ? – moooeeeep Nov 24 '11 at 10:55
  • 3
    I did not down vote this... but I am guessing the problem is that this seems like a relatively easily looked up question (not only [are there duplicates](http://stackoverflow.com/questions/1788655/when-to-use-malloc-for-char-pointers), but there are plenty of examples you can Google up). – Michael Dautermann Nov 24 '11 at 10:55
  • 4
    I've downvoted because your statement that there's little info on malloc is plainly ridiculous. – Alexander Kulyakhtin Nov 24 '11 at 10:57
  • Did a Google search this is the first thing to come up so please don't down vote useful information. – Katianie Dec 12 '14 at 19:06

5 Answers5

6

There should be millions of pages explaining the use of malloc, I doubt you did much searching. But here is how you call it:

char *var = malloc(sizeof(char) * 1000);

For other types, just change it:

int *int_var = malloc(sizeof(int) * 1000);

Edit Remember that you have to free the allocated memory after use! Or you will have a memory leak.

free(var);
free(int_var);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 4
    var = malloc( sizeof *var * 1000 ) is typically preferred over malloc( sizeof( char ) * 1000 ) – William Pursell Nov 24 '11 at 12:41
  • 2
    these comments prove the usefulness of the question. there is obviously some disagreement about how most properly to do this. for instance I've seen others comment that you should use the `sizeof` the variable being pointed to instead of repeating the type, like `char *var = malloc(sizeof(*var) * 1000);` – robisrob Feb 13 '17 at 23:19
2

You do not want to use malloc for this variable, because it's already allocated, but you may achieve similar results by using

char *var = malloc(sizeof(char[1000]));
// do stuff
free(var);
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
0

http://www.manpagez.com/man/3/malloc/

char *pVar = malloc(1000);
.... use pVar ....
free(pVar);
// DONT use pVar anymore
mah
  • 39,056
  • 9
  • 76
  • 93
0

If you have declared your variable as char var[1000]; you already have statically allocated that much memory. You don't need to call malloc for this.

If you want to use dynamic allocation and your variable is declared like this: char *var; you can allocate the memory as follows:

var = malloc(1000);

also don't forget to free your allocated memory when you're done with it:

free(var);
Constantinius
  • 34,183
  • 8
  • 77
  • 85
  • @dikidera if its not a pointer then it already has its space and no malloc is necessary. – mah Nov 24 '11 at 10:59
  • 1
    @dikidera: if you want to dynamically allocate memory, then you must use a pointer to store its address. If you want your variable to be an array, then you don't use malloc. – Steve Jessop Nov 24 '11 at 11:00
0

If you have char var[1000] you do not need malloc this definition allocates 1000*sizeof(char) for your var so you can do something like var[999] = 'x';

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158