0

Today I was trying to code something and i found my self at some point that i need to allocate a block of memory needed for a pointer. The program was OK and checked with valgrind was also OK.

Some how after I started to review the whole code i realized that what i did, was that the memory was allocated for int a which is not a pointer.

I'm a person which learn C just for fun, I read some books and hundreds of Tutorials, but i never saw any mention about something like this so I really need an Explanation here.

I tried to make a small program to explain the context:

#include<stdio.h>
#include<stdlib.h>

void createMe(int *a);
void freeMe(int *b);

int main(void){
    int a;
    createMe(&a);
}

void createMe(int *ptr){
    ptr = malloc(256);
    *ptr = 10;
    printf("A = %d\n",*ptr);
    freeMe(ptr);
}

void freeMe(int *ptr){
    free(ptr);
}

Here i have int a and i passed its address in createMe(&a);

The create function takes a pointer as argument so i had to use &a. Now comes the strange part for me:

inside createMe(); i call malloc on that pointer ptr which is the argument of the function. Where exactly it is that memory block added/used in the pointer which is the argument of create() function or it sent to a inside main?

As far as I read/learn until now:

1) a doesn't get that memory block because a is an int and not an int*.

2) does the function argument ptr get that memory block`?, if so, how comes that i never read something like that.

I used Linux mint 17.3 with GCC-4.8.5 and GCC-5.2.0

Here is the result of Valgrind:

==6793== Memcheck, a memory error detector
==6793== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==6793== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==6793== Command: ./program
==6793== 
A = 10
==6793== 
==6793== HEAP SUMMARY:
==6793==     in use at exit: 0 bytes in 0 blocks
==6793==   total heap usage: 1 allocs, 1 frees, 256 bytes allocated
==6793== 
==6793== All heap blocks were freed -- no leaks are possible
==6793== 
==6793== For counts of detected and suppressed errors, rerun with: -v
==6793== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

As you can see the memory allocation takes place and free takes place too.

And the GCC Flags are:

-Wall -Wextra -Werror -Wstrict-prototypes -Wconversion -Wmissing-prototypes -Wold-style-definition -std=c11 -O0 -g
2
  • 4
    Hint: a is not needed here at all. Try passing NULL. It'll work just as well. Commented Jan 4, 2016 at 12:30
  • @cad Thank you for your response. Me I was asking if is possible to create a block of memory for a function argument. I never read something like that. :) Commented Jan 4, 2016 at 12:34

2 Answers 2

2

In case of your code,

void createMe(int *ptr){
    ptr = malloc(256);
    *ptr = 10;
    printf("A = %d\n",*ptr);
    freeMe(ptr);
}

all the time, ptr is local to your function createMe(). Whatever you do to ptr (notice, not *ptr) itself, is not going to make any effect to a, anyway.

What you're doing here is

  1. Getting a pointer type variable (as a function argument), fine.
  2. Allocating memory dynamically, fine.

    2.1. Not checking for malloc() success, bad.

  3. Using the allocated memory properly, fine.

  4. Releasing the memory, fine.

You don't have any issues in your function, it's ok.

Just to elaborate, you're using ptr just as another local variable in createMe() function. The first malloc() allocates new memory block to ptr, anyway.

Sign up to request clarification or add additional context in comments.

5 Comments

This means that you can add a memory block for a pointer which is argument of a function? I never read about that.
Like i said, was just created a small program, I always check a pointer for NULL. The Question was clearly i hope :)
Based of your Answer I can call that function on any variable which is int and it will not harm something. Did i understood right?
@Michi Yes, you're right, as long as you keep this function definition unchanged. :)
Yes, it is. I have to wait 5 minutes to accept it. Thank you.
1

In C, arguments are passed by value. ptr in createMe points to new location after

ptr = malloc(256); 

Now ptr is pointing to the beginning of a new memory chunk.

2 Comments

Yes, this is exactly the same thing I was thinking too, that ptr points to a new location, but i was confused, when i saw, that through that pointer i still can update the value of a inside main.
@Michi; No. Once ptr is updated, it has no information about location of a.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.