Basic C Memory Allocation [duplicate] - c

This question already has answers here:
How do I modify a pointer that has been passed into a function in C?
(7 answers)
Closed 1 year ago.
#include <stdio.h>
#include <stdlib.h>
void allocateMem(int *a)
{
a = (int*)malloc(5 * sizeof(int));
}
int main()
{
int *ptr;
allocateMem(ptr);
ptr[0] = 5454;
ptr[1] = 54;
printf("Hi %d\n", ptr[1]);
free(ptr);
return 0;
}
I didn't get any output and error with the code. But if I allocate memory in main function, it actually works.

C function arguments are passed by value. This means that when you pass ptr to allocateMem and then modify it within allocateMem, you're not changing anything about ptr.
You are creating a memory leak, since you can't free the memory you've dynamically allocated as you haven't preserved a pointer to it.
Any argument you want a function to modify external to the function you're calling, you need to pass a pointer to, so if you want to modify a pointer, you need to pass a pointer to a pointer, as #babon has demonstrated with the code in their answer.

Here's the fix:
#include <stdio.h>
#include <stdlib.h>
void allocateMem(int **a)
{
*a = malloc(5 * sizeof(int));
}
int main()
{
int *ptr;
allocateMem(&ptr);
ptr[0] = 5454;
ptr[1] = 54;
printf("Hi %d\n", ptr[1]);
free(ptr);
return 0;
}
To write to a variable in another function, you need to pass a pointer to it. Since the intention here is to write to a pointer, you need to pass the address of the pointer - &ptr.
As an address was passed, the allocateMem() function dereferences a once to hop into the memory location which is to be updated and lays down the address returned by malloc(). When this function returns, main() finds that ptr is pointing to a valid address and writes data inside the allocated memory.

You can return the pointer to the allocated memory (and check the return value), like this:
#include <stdio.h>
#include <stdlib.h>
int *allocateMem(void)
{
return (int*)malloc(5 * sizeof(int));
}
int main()
{
int *ptr;
if( NULL != (ptr = allocateMem()))
{
ptr[0] = 5454;
ptr[1] = 54;
printf("Hi %d\n", ptr[1]);
free(ptr);
}
return 0;
}
The constant number 5 in allocateMem should not be hardcoded, or give it a valuable name.

Related

How does malloc work within wrapper function? [duplicate]

This question already has answers here:
How do I modify a pointer that has been passed into a function in C?
(7 answers)
Closed 2 years ago.
I have a question dedicated to:
void* malloc (size_t size);
In the regular example that can be found on millions of sites over the internet it's shown that the right way to use malloc is the following:
int main()
{
int* num;
num = malloc(sizeof(int));
*num = 10;
printf("Value = %d\n", *num);
free(num);
return 0;
}
But If I want to allocate memory within a function and use it in main like below, then the only option is to implement the function the following way:
void func_alloc(int** elem, int num_value)
{
*elem = malloc(sizeof(int));
**elem = num_value;
}
int main()
{
int* num;
func_alloc(&num, 10);
free(num);
return 0;
}
I assumed by mistake, that such code as below would work:
void func_alloc(int* elem, int num_value)
{
elem = malloc(sizeof(int));
*elem = num_value;
}
int main()
{
int* num;
func_alloc(num, 10);
free(num);
return 0;
}
Could you please explain or maybe give a link to resource with explanation why does it work only this way?
I really cannot understand why do I need double pointer as an input parameter and why in the other case it comes to "segmentation fault"...
Thank in advance ;)
I assumed by mistake, that such code as below will work.
In C, the arguments are passed by value, when you pass a pointer as an argument of a function, you are passing the value of the pointer, basically a copy of it, not the pointer itself, malloc will change the value of that pointer, but since what you passed was a copy, that is what's changed, not the original pointer, that one remains unchanged.
In the second code snippet, the working code, *elem = malloc(sizeof(int)); broadly means make this pointer elem point to this valid memory address given to me by malloc(assuming it succeeds), the value of the pointer to the pointer elem which you passed as an argument remains unchanged, it being a copy doesn't matter because it's not changed, it's still the same address that was passed as argument, the address of the pointer num which is now pointing to the memory location given by malloc.
**elem = num_value means store num_value in the address stored in the pointer where elem is pointing to, which is where num is pointing to, which is the new memory block previously given by malloc.
That being said, it's not the only option, you can use a local pointer, return it and assign it to another local pointer in the caller side, this is still a copy, but it's a copy of the changed pointer:
int *func_alloc(int num_value)
{
int *elem = malloc(sizeof *elem); //more idiomatic
if(elem == NULL){ // check for allocation errors
perror("malloc" );
exit(EXIT_FAILURE);
}
*elem = num_value;
return elem;
}
int main()
{
int* num = func_alloc(10);
free(num);
return EXIT_SUCCESS;
}
Footnote:
In the third code snippet, freeing num, given that it is uninitialized is a bad idea, I assume you know as much, nonetheless I thought I'd mention it. This may be the reason for the segfault you experienced, whatever garbage value num has will be assumed to be valid memory address, and free will try to deallocate it, doing this will invoke undefined behavior. If it was NULL, it's a different story, it's well defined behavior (execept in some very old standars). Initializing variables when they are declared is, in most cases, a good idea.
A commented explanation :
void func_alloc(int* elem, int num_value)
{
/* elem points to address gave by malloc, let's say 0x12345678 */
elem = malloc(sizeof(int));
/* at address 0x12345678 you have now your num_value */
*elem = num_value;
/* end of the function. Changes made to parameters passed by value are lost */
}
int main()
{
int* num;
/* num is a pointer to an address you could not have write access to, you actually don't know */
func_alloc(num, 10);
/* As C arguments are passed by value, changes made into the function are lost */
/* You try to free num which is still a pointer to an address you potentially have no access to => SEGFAULT */
free(num);
return 0;
}
EDIT:
Not shown in this example, but it is good practice to always check that pointer returned by malloc is not NULL, otherwise you should exit without trying to assign a value to the pointer.
If you have:
#include <stdio.h>
void foo(int x)
{
x = 9;
}
int main(void)
{
int a = 1;
foo(a);
printf("%d\n", a);
}
you probably don't expect the value of a in main() to change just because foo() assigned to x, right? It doesn't change, because parameters are assigned by value. The variables x in foo(), and a in main() are two different variables.
The same applies in your code. elem in func_alloc() is a different variable from num in main(), and assigning to the former doesn't change the value of the latter. The fact that these two are of type int *, and not e.g. just int, makes no difference in this.
That said, you can also return the pointer you got from malloc(), e.g.
int *alloc_int(int value)
{
int *p = malloc(sizeof(int));
*p = value;
return p;
}
(not that it seems to make much sense for a mere int.)

Passing double pointer as function argument

I simply want to assign a pointer to another pointer via the function (same memory address). My code is below:
#include <stdio.h>
void d(int** a)
{
int* val_ptr = malloc(1);
*val_ptr = 5;
printf("%d\n", *val_ptr);
a = &val_ptr;
}
int main()
{
int* a = NULL;
d(&a);
printf("%d\n", *a);
return 0;
}
Output from Link
5
Segmentation fault
Your code has three problems:
Here int* val_ptr = malloc(1);, you allocate 1 byte rather than allocating space for an int. Use the following to fix it:
int* val_ptr = malloc(1 * sizeof(int));
This a = &val_ptr; is not what you want. It changes the local pointer and makes it point to the address of val_ptr. This will not affect the pointer that you've defined in main.
Fix it using
*a = val_ptr;
This way, the pointer in main will also reflect the change and will point to the malloced memory
You should free the allocated memory after its use. Add
free(a);
after the printf in main to free it.

Initialization function (i.e. "constructor") for struct, why are variables not being initialized properly?

I have an unnamed structure called `FooStruct', and I wrote a function for it that initializes all its variables and also takes care of dynamic memory allocation.
I tried running this code, but it is not producing the results that I am expecting. For some reason, the variables are initialized correctly when inside the function, but then they change again once the block is exited.
A project that I'm working on requires that I use unnamed structs only. Why is this happening?
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int fooPrimitive;
} FooStruct;
void FooStructInit(FooStruct * ptr, int num) {
ptr = (FooStruct*)malloc(sizeof(FooStruct));
ptr -> fooPrimitive = num;
// Expected output: 5
// Actual output: 5
printf("ptr -> fooPrimitive: %d\n", (ptr -> fooPrimitive));
}
int main() {
FooStruct * ptr;
FooStructInit(ptr, 5);
// Expected output: 5
// Actual output: some random number
printf("FooStruct -> fooPrimitive: %d\n", (ptr -> fooPrimitive));
}
C only has pass by value semantics, so when you pass a pointer to a function, the original value of the pointer is not changed, only the local copy is changed in the function.
The way to get around this is to pass a pointer to a pointer, so that you can change the value at the original memory location.
void FooStructInit(FooStruct **ptr, int num) {
*ptr = malloc(sizeof(FooStruct));
...
}
int main(void) {
FooStruct * ptr;
FooStructInit(&ptr, 5); /* get the address of the pointer */
...
}
When you pass ptr to FooStructInit, it is copied and this copy is assigned the return value of malloc then. This is called pass-by-value. Instead, pass a pointer to ptr to actually write to ptr and not just the passed argument:
void FooStructInit(FooStruct** ptr, int num) {
*ptr = malloc(sizeof(FooStruct));
(*ptr)->fooPrimitive = num;
// Expected output: 5
// Actual output: 5
printf("(*ptr)->fooPrimitive: %d\n", ((*ptr)->fooPrimitive));
}
int main() {
FooStruct* ptr;
FooStructInit(&ptr, 5);
// Expected output: 5
// Actual output: 5
printf("FooStruct->fooPrimitive: %d\n", (ptr->fooPrimitive));
}
Notes:
call free after malloc
don't cast the result of malloc

Passing arrays in c

I have a question regarding passing arrays in c to a function.
When I run this program it gives me a segmentation fault
int main()
{
char **ptr= NULL;
test(ptr);
printf("%s", ptr[0]);
return 0;
}
void test(char **ptr)
{
ptr = (char **) malloc(sizeof(char *));
ptr[0] = (char *) malloc(sizeof(char)*5);
strcpy(ptr[0], "abc");
return;
}
but this worked just fine
int main()
{
char **ptr= NULL;
test(&ptr);
printf("%s", ptr[0]);
return 0;
}
void test(char ***ptr)
{
*ptr = (char **) malloc(sizeof(char *));
*ptr[0] = (char *) malloc(sizeof(char)*5);
strcpy(*ptr[0], "abc");
return;
}
Could someone explain why?
You're passing the parameter ptr by value; the formal parameter ptr in test is a different object from the actual parameter ptr in main, so changing the value of test::ptr is not reflected in main::ptr.
Thus, you need to pass a pointer to ptr into test, and test needs to dereference that pointer to write to the correct object.
For any type T, if you want to modify the value of the function argument, you need to do the following:
void foo( T *param )
{
*param = ...;
}
void bar( )
{
T obj;
foo( &obj );
}
In this particular instance, T is char **.
In C, when you pass an array to a function, the array "decays" to a pointer. The function receives a pointer to the first element of the array. It does not receive the array itself. Any changes made to the pointer assignment in the called function are not reflected in the calling function.
In your first example, test() receives the array of strings, and inside the function, it changes what that pointer points to. So the local copy of ptr, which was NULL, gets allocated memory inside the function, as does ptr[0]. However, those changes are local to test(). They are not reflected in the calling function. When test() finishes executing and returns, the value of ptr in the calling function is still NULL. And you have a memory leak because there is no way to access the memory that was allocated in test().
In order for the changes to be reflected in the calling function, you have to pass a pointer to the array of strings: hence the &ptr in the call and the three-level pointer in the definition of test(). Another, simpler approach would be:
int main()
{
char **ptr= NULL;
ptr = test();
printf("%s", ptr[0]);
return 0;
}
char** test(void)
{
char **ptr = (char **) malloc(sizeof(char *));
ptr[0] = (char *) malloc(sizeof(char) * 5);
strcpy(ptr[0], "abc");
return ptr;
}
One clarification: I said "Any changes made to the pointer assignment in the called function are not reflected in the calling function." This is not the same as saying "Any changes to array elements are not reflected in the calling function." Consider this:
int main (void) {
int array [] = { 0, 1, 2, 3, 4 };
test1 (array);
printf ("%d\n", *array);
test2 (array);
printf ("%d\n", *array);
return 0;
}
void test1 (int* array) {
int new_array[] = { 3, 4, 5, 6, 7 };
array = new_array;
return;
}
void test2 (int* array) {
array [0] = 5; // or *array = 5
array [1] = 6;
array [2] = 7;
array [3] = 8;
array [4] = 9;
return;
}
Output:
0
5
Here in test1() the change is to where the array pointer itself is pointing, which won't be reflected in the caller. So in the calling function, *test is still 0. In test2(), the changes are to the array elements, which are reflected in the caller. Hence the difference in output: *test is now 5. All this uses static rather than dynamic memory allocation, but the principle is the same.
It's because you expect your function not to return a value, but to modify a variable you have already created in another scope.
You expect, from your function, to pass ptr and end up with the following structure:
ptr -> b -> []
where b is a pointer to the array [].
In the first example you are not modifying the outer ptr inside the function, but a copy of it. In that case, to get the desired structure you'd need your function to return a char** so that you could return the allocated pointer. And you wouldn't need to pass the original ptr.
In the second example, on the other hand, you are passing a pointer to the outer ptr, so the function will be modifying the same memory space that the outer ptr uses.
I hope this can help a little.
Consider below code for understandling
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void test(char **ptr);
int main()
{
char *ptr= NULL;
test(&ptr);
printf("%s\n", ptr);
return 0;
}
void test(char **ptr)
{
*ptr = malloc(sizeof(char *));
strcpy(*ptr, "abc");
return;
}
Whatever doing in function definition has to be reflect in main function. So if you want to make changes in ptr variable you have to pass the address of the ptr variable.
ptr is single pointer(char ptr) variable, we need to pass address of the pointer(&ptr) variable.
In function definition we are receiving address of pointer variable, so argument has to be the type of double pointer.
This concept is similar to call by reference.
Similar things happening in your code also.
You need to pass address of double pointer to make changes in pointer variable.
Address of double pointer has to taken by triple pointer in function definition.
To expand on the answer from #rcrmn.
It is a good program design to have data get passed "up" the calling structure. In this case main() needs to free the storage it received from test.
Notice that this code avoids all of the double pointers in the original question and still meets the requirements. This is the essence of good programming, write clear, simple, code that gets the job done.
#include <stdio.h>
char *test();
int main()
{
char *ptr = test();
printf("%s", ptr);
free(ptr);
return 0;
}
char *test()
{
char *ptr = (char *) malloc(sizeof(char) * 5);
strncpy(ptr, "abc", 3);
return ptr;
}

Functions and pointer to pointers

I have been trying to create a pointer variable in the called function and somehow pass the value pointed by it to the main function. I wrote a few sample programs but it i still seem to be missing something out. and the challenge was to achieve this using pointers to pointers to pointers. Here is the code that I wrote and tested. I think am missing something obvious, can you guys point it. Thanks.
int one(int ***ptr)
{
static int *p,**pp,b=10;
p=&b;
pp=&p;
ptr=&pp;
printf("b:%d\tp:%d\tpp:%d\n",b,*p,**pp);
printf("Sub Ptr:%d\n",***ptr);
printf("Address of ***ptr:%d\n",&ptr);
return 32;
}
int main(int argc, char *argv[])
{
static int ***ptr;
int a=200,*b,**c;
b=&a;
c=&b;
ptr=&c;
printf("Main Ptr:%d\n",&ptr);
a=one(ptr);
printf("Main Ptr:%d\n",&ptr);
printf("Main Ptr:%d\n",***ptr);
system("PAUSE");
return 0;
}
I get an output of 32 which is the return value of the function. Is it possible to get the value of 10 which is pointed in the called function.
I also tried global declaration but din work either. I would like to maintain the local declaration and see if its possible...
I think you misunderstood about pointers. The problem of your code is that you didn't realize that pointer is also "pass by value", the pointer in the function is another variable on the stack, instead of the one you declare in the main function.
I would use a more simple example, but the idea is the same.
void changePointerValue (int * ptr)
{
int newValue = 10;
ptr = &newValue;
}
int main ()
{
int x = 20;
int * ptr = &x;
changePointerValue (ptr);
printf ("After Change: %d\n", *ptr);
}
What value do you think it will output in main function? Is that be 10?
But no, in fact, it will output 20.
Why? Let's look what our code does. In the line of chagePointerValue(ptr), the computer copy the value of ptr in the main function to a new varaible on stack, let's call it ptr' and pass it to the changePointerValue function.
So in fact the ptr in the changePointerValue function is ptr', not the one you declare in the main function. The second line of changePointerValue, you assigned a new memory address to ptr', and after that, ptr' is discarded because the function is returned. The ptr in the main function remains the same value, which is the memory address pointed to x.
If you want the output to be 10, you need deference ptr in the changePointerValue, and the assignement will means 'Change the value where the ptr is pointed at'.
void changePointerValue (int * ptr)
{
int newValue = 10;
*ptr = newValue; // Now you are change the content of the memroy cell where ptr is pointed at.
}
int main ()
{
int x = 20;
int * ptr = &a;
printf ("Before Change:%d", x); // 20
printf ("Before Change:%d", *ptr); // 20
changePointerValue (ptr);
printf ("After Change: %d\n", *ptr); // 10
printf ("After Change: %d\n", x); //10
}
Edit:
So, if you want it print 10 in the main function, the correct way to do this is deference ptr in the function. But this will also change the value of variable a in the main function.
int one(int ***ptr)
{
***ptr=10; // Dereference ptr to where it points to. (Varaible a in the main function)
return 32;
}
int main(int argc, char *argv[])
{
static int ***ptr;
int a=200,*b,**c;
int result;
b=&a;
c=&b;
ptr=&c;
printf("Main Ptr:%d\n",&ptr); // The memory address of variable ptr.
result=one(ptr);
printf("Main Ptr:%d\n",&ptr); // the memory address of variable ptr.
printf("Main Ptr:%d\n",***ptr); // 10
printf("Main a:%d\n", a); // 10
printf("Main result:%d\n", result); //32
system("PAUSE");
return 0;
}
You're setting ptr to the address-of-the-address-of-the-address-of a local variable (b), which goes out of scope at the end of the function. This is undefined behaviour. You either need to assign to ***ptr, or malloc something on the heap.
Also, you're passing ptr by value to the function. So any local modifications to it won't be reflected in main. You need to pass by pointer, i.e. you'll need an int **** as an argument.
I really hope this is nothing more than a learning exercise, because any more than two levels of pointers usually indicates that the design really needs to be re-evaluated!

Resources