-2

When is it appropriate to use

void* space_to_use = malloc(size); 
Karina Kozarova
  • 1,145
  • 3
  • 14
  • 29
  • 2
    Impossible to copy-paste whole C book. Get any book, answer is at first 10 pages. – Jacek Cz Sep 26 '17 at 14:00
  • 3
    can be anything you want – Jean-François Fabre Sep 26 '17 at 14:00
  • It is difficult to infer the purpose of using memory chunks from this code piece. Perhaps it is not intended for specific use. – BLUEPIXY Sep 26 '17 at 14:02
  • Yup - maybe it's just 'data'. Maybe the code is just loading it from some stream, and passing it on. It doesn't need to know what's in the buffer. – Martin James Sep 26 '17 at 14:08
  • 2
    _Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [Ask] page for help clarifying this question._ – Sourav Ghosh Sep 26 '17 at 14:12
  • probably always, probably never....what's the requirement? – Sourav Ghosh Sep 26 '17 at 14:12
  • 3
    It's kind of rude to completely change your question after answers have been submitted, because now the answers do not make sense. – John Bode Sep 26 '17 at 14:12
  • 1
    Some context of where you found this code is necessary to answer. – Lundin Sep 26 '17 at 14:13
  • @JohnBode Can't agree more, but the fundamental problem is, questions like this should not be answered, at all. It's waste of effort, overall. – Sourav Ghosh Sep 26 '17 at 14:14
  • I am sorry, but I can't delete the question. – Karina Kozarova Sep 26 '17 at 14:14
  • You could try unchecking the "accept answer". – Weather Vane Sep 26 '17 at 14:15
  • ...and then see if you can delete the question. – Weather Vane Sep 26 '17 at 14:17
  • Even without the checked answer, it is impossible because of all the answers this question got. – Karina Kozarova Sep 26 '17 at 14:17
  • 1
    @KarinaK You could attempt to modify and improve the question, for example by including the surrounding code where you found this line. Questions are closed and not deleted to give the poster a chance to improve them, if possible. Otherwise if nothing is changed and there are no up-voted answers, a closed question will eventually get deleted automatically by the scripts on the site. – Lundin Sep 26 '17 at 14:27

1 Answers1

-1
void* space_to_use = malloc(size); 
// malloc always return void pointer that means it can be typecast to any type.
// before using void pointer it is necessary to typecast it into proper type.
// for example:-
// if size is 8 byte.It will allocate 8 byte of memory.
/*
void* space_to_use = malloc(size); 
char * ptr = (char*)space_to_use;
*/
// These two line can be combine in one statement.

char * ptr = (char*)malloc(size*sizeeof(char));
// NOTE:sizeof(char) is to make sure platform independent.

// Same for int if we want to store some integer.
 int * ptr = (int*)malloc(size*sizeeof(int));
akshayk07
  • 2,092
  • 1
  • 20
  • 32
Rohit
  • 142
  • 8
  • 2
    Just for information: `sizeof char` is defined to always be `1` so there's never a need to muliply by it. However, the `CHAR_BIT` define can be different. – Zan Lynx Sep 26 '17 at 14:15
  • Also please see [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Bo Persson Sep 26 '17 at 14:23
  • Yes it will be always one byte but it will generic means for integer for integer * or structure varaibe or pointer.We need to add sizeof(int) or sizeof(int *) or sizeof(strcuct abcd) so if we mention sizeof(char) it will not harm anything. – Rohit Sep 27 '17 at 09:13