Question:
C programming: confusion with malloc and pointers?
Dfdsf
2013-02-26 12:58:27 UTC
int *nump
nump = (int *)malloc(sizeof(int));

so the above code dynamically allocates enough space to hold type int value and returns a pointer to nump? so does nump now hold the address of the first byte of the memory allocated by malloc?
Three answers:
Fred
2013-02-26 13:21:10 UTC
-------------------



int *numPtr; // declare a pointer to an integer.



numPtr = (int*)malloc(sizeof(int));



This code is a bit strange. For one the casting (int*) is unnecessary as the compiler will handle that for you. With that being said however, the code works as follows:



malloc accepts one argument, which is the number of bytes to allocate memory for (sizeof(int)).



In this case, you have only requested sizeof(int) bytes, or 4 most likely. Which means you have only enough memory to store 1 int.



Malloc then returns a pointer to newly allocated memory. so numPtr (nump) will now point to this new dynamically allocated memory. Keep in mind that mallocing memory does not place any value's in that spot. It only reserves the spot. If you dereference your pointer, you can then store an integer.



Now, malloc genreally would be used to create an array with the following code:



numPtr = malloc(sizeof(int) * 10) // This code will malloc space for 10 intengers. Which can be accessed by incrementing your pointer. The malloc here returns the address of the first spot. numPtr + 1 gives you the address of the second spot. (Exactly like array indexing).



Just like this:



numAry[10];



numAry[1] == *(numPtr + 1); //This statements are equal.
AnalProgrammer
2013-02-26 13:20:02 UTC
All you need to know is that the pointer nump points to some memory that can be used to store an int.

As to whether this is the first or the last byte is dependent on the operating system and does not concern you.



Have fun.
manza
2016-10-24 05:14:21 UTC
The coder of somefunction() needs to allocate a heap-staying power pointer variable that could be utilized to point to a personality or an array of characters. (unusual component to do, by technique of itself.) to target this, they use malloc(), it really is the way you allocate heap-staying power variables of any variety. malloc() desires to carry close how a lot area to allocate. seeing that "variable" is a personality pointer, the programmer used sizeof(char*) as a thanks to compute the type of bytes required for that variable. malloc() returns a pointer to the dispensed memory. in spite of the undeniable fact that, seeing that malloc() can't likely recognize what it really is returning (it in simple terms knows ways enormous, not what type), it returns all such memory guidelines as (void *). C expects you to inform it what type of pointer is being again. So the (char*) solid is needed. C calls for that a void* pointer (it really is what malloc() returns) be manage aligned for any type of variable. C also calls for that a void* pointer be sufficiently enormous (length smart) that recommendations that could want to different varieties may be switched over to it without lack of alignment or bits. C also calls for that char* and void* be extra or less equivalent, yet when "variable" pointed to at least something except char it really is plausible that the pointer varieties may want some form of conversion. (frequently not, in spite of the undeniable fact that it really is plausible.) So the solid is needed. On a 16-bit linear manage device, sizeof(char*) frequently is two. On a 32-bit linear manage device, sizeof(char*) frequently is 4. etc. this is continually discovered at assemble time, too. So there is not any computation time in touch. It in simple terms receives replaced with a consistent.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...