-5

I came to know the concept of malloc in C, but syntax somewhat confusing. Can some one explain following syntax step by step?

   data_type *ptr;
   ptr=(data_type*)malloc(size in bytes);
  *ptr=some_value;

What is i understood is: First we declare a pointer, which is still pointing nowhere. Then the pointer, not the content but the pointer itself is equal to a pointer type that contains the memory address space for an data_type.

Can some one expand (or clear) my statement to understand better?

Cœur
  • 37,241
  • 25
  • 195
  • 267
beginner
  • 5
  • 2
  • 2
    First of all casting to `malloc` is not good.It's automatically prompted to other type.see http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – Jayesh Bhoi Aug 21 '14 at 10:29
  • 2
    This code isn't correct. The cast should be `(data_type*).` I don't know what's confusing about the syntax. It's a declaration followed by an assignment to the cast result of a function taking one argument. Break it down. You can write the first two lines on one line, and you should. – user207421 Aug 21 '14 at 10:29
  • 1
    A C answer will differ from a C++ one. Can I remove the C++ tag? – Bathsheba Aug 21 '14 at 10:31
  • Hi EJP, sorry for the mistake,can u read my statement and kindly justify it -chethan – beginner Aug 21 '14 at 10:34
  • Hi Bathsheba,yes you can...-chethan – beginner Aug 21 '14 at 10:36
  • Was the missing asterisk (`*`) your problem? If yes, _don't_ fix it in the question (i.e. roll-back your edit). Otherwise: Please clarify what's your question. – mafso Aug 21 '14 at 10:52
  • Hi mafso,yes it was a mistake,plz tel me why that asterisk(*) should appears with the type casting. – beginner Aug 21 '14 at 12:01
  • Lose the cast; it isn't necessary, and under older versions of C can suppress a useful diagnostic. Simply write ptr = malloc( sizeof *ptr * number_of_elements ) instead. – John Bode Aug 21 '14 at 12:15
  • Hi John,ok since i was familiar with that only!!!! can u explain me the arguments inside the malloc??? – beginner Aug 21 '14 at 12:26
  • @user3928280: sizeof * ptr is equivalent to sizeof (data_type), and it returns the number of bytes in a single instance of data_type; number_of_elements is the number of data_type elements you want to allocate. This way you don't have to worry about the number of *bytes*, which makes your code easier to port and maintain. It also more closely matches the form of a calloc call. – John Bode Aug 21 '14 at 13:27
  • @user3928280: Something I should have mentioned earlier is that while the cast is unnecessary in C, it is required in C++; this is one of several areas where the two languages are not compatible. Then again, if you're writing C++, you should use the new operator instead of malloc. – John Bode Aug 21 '14 at 13:35
  • Hi John, it was nice explanation!!! why std books usually guide us to cast??? exp: Understanding and using C pointers by O'REILLY. – beginner Aug 22 '14 at 04:42

2 Answers2

1

Your code is slightly wrong, it should be:

data_type *ptr;
ptr=(data_type *)malloc(size in bytes);
*ptr=some_value;

ptr contains a memory address that says where the data is. it starts off pointing at a random location. malloc reserves a chunk of memory for use by your program and returns the address of the start of this memory, you store this address in ptr. When you do *ptr = value sou are saying 'set the memory at the address of ptr equal to value'. You don't need to cast malloc so your code would be better like this:

data_type *ptr = malloc(size in bytes);
*ptr=some_value;
James Snook
  • 4,063
  • 2
  • 18
  • 30
  • Hello james, based on ur ans i understood that,ptr created at STACK memory and that address returns to HEAP memory right?? Kindly justify this... – beginner Aug 21 '14 at 11:25
  • ptr is an area of stack memory that holds an address to somewhere else. This means that the size of ptr is likely 32 or 64 (to match the chip in your system) 'data_type *ptr;' means 'make me a variable that contains the address of something of type data_type'. ptr = malloc (size) takes the address returned by malloc and copies it into the stack area of memory that is ptr. malloc will have reserved an area on the heap and the address returned will point at this area. ptr is in stack memory and contains an address, this address points to heap memory. – James Snook Aug 21 '14 at 12:49
0

malloc function returns generic pointer (void *) so that it can be assigned to the pointer of any type and accepts the size (i.e how much memory to be allocated).

And it is not data type but its the pointer to the data_type.

ptr=(data_type *) malloc (sizeof (data_type));
Adarsh
  • 883
  • 7
  • 18