0

I am currently learning about dynamic memory allocation in C and am having difficulty with a particular concept. For using malloc for a particular integer, I need to put a previously declared integer within that space.

The following below is what I am putting in as my code.

void example(int ab)
{
int* intPtr=(int*)malloc(sizeof(int));
*intPtr = &ab;
}

I am not trying to run a program or anything. I just want to see if I have the right idea about some basic memory allocation.

Noob Coder
  • 21
  • 3
  • Its not necessary to typecast the `malloc()` as it's done implicitly by compiler. Read [Do I cast the result of malloc?](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). This `int* intPtr = malloc(sizeof(int));` is fine. – Achal May 28 '19 at 09:31
  • `ab` already has a space on stack, In C memory to local variables is gets allocated on stack a static time in terms of offset from Sack pointer. You can read about Storage Classes in C for static memory allocation . Also you cannot change address of any variable in c in run-time. – Vagish May 28 '19 at 09:34
  • Vagish that is true I have learned about the stack and how local variables are allocated. So if I was hypothetically declaring a new variable in this context would i do: *intPtr=int x? – Noob Coder May 28 '19 at 09:47

1 Answers1

0

There are few issues, firstly its not necessary to typecast the result of malloc() as it's done implicitly by compiler. Read Do I cast the result of malloc?. This

int* intPtr = malloc(sizeof(int)); /* or even malloc(sizeof(*intPtr)); is better */

is fine.

Secondly, this

*intPtr = &ab; /* *intPtr means value at allocated dynamic address, you should assign the value not address of ab*/

is wrong as *intPtr is the value at dynamic address, compiler should have warned you like

main.c:7:9: warning: incompatible pointer to integer conversion assigning to 'int' from 'int *'; remove & [-Wint-conversion]

If you had compiled with below flags like

gcc -Wall -Wextra -Werror -Wpedantic test.c

It should be

*intPtr = ab;

Also you are not freeing the dynamically allocated memory, its leads to memory leakage. Free the dynamically allocated memory by calling free() once usage is done.

Side note, if your intention is to do like this

intPtr = &ab; /* Before this statement intPtr was pointing dynamic memory(heap), but now that gets overwritten with stack allocated space */

then you are overwriting dynamic memory with stack created memory i.e address of local variable, in that case you are loosing dynamic memory as nothing is pointing to previously allocated dynamic address.

Achal
  • 11,821
  • 2
  • 15
  • 37
  • Thanks that clears up some misconceptions I was having. I also was type casting because I read some prior examples that did type casting when using malloc. I will read the resource you posted above. – Noob Coder May 28 '19 at 09:49
  • @NoobCoder: Prior to the 1989 standard, `malloc` returned `char *`, so a cast *was* required if you wanted to assign the result to a different pointer type. The ‘89 standard changed `malloc` to return `void *`, which can be assigned to other pointer types without the cast. This is not the case in C++, which does not allow direct assignment between `void *` and other pointer types. – John Bode May 28 '19 at 11:47