tonyjk
tonyjk

Reputation: 35

Pointer and memory allocation

I am trying to understand the behavior of memory allocation in c. I have written this code: I allocated 1 space of memory of the char pointer, however I am trying to add data to it outside its allocated memory and it is giving me good result. But what is the inconvenience of not allocating the right amount of memory?

int main() {
char *c = (char*)malloc(1*sizeof(char));
c[0]='1';
c[1] ='2';
c[2]='4';
c[3]='3';
c[4]='6';
c[5]='\0';
printf("%s",c);
free(c);
return 0; }

Another question,

for example I have a function that returns a char * and inside this function I am allocating a memory :

char * mallocbyme()
   {
      char *f = (char*) malloc(4*sizeof(char));
      return f;
   }
char *d = (char*) malloc(1*sizeof(char));
d= mallocbyme();

My question is what will happen with the first allocated memory assigned to d?

Thank you

Upvotes: 0

Views: 95

Answers (4)

Caleb
Caleb

Reputation: 125017

But what is the inconvenience of not allocating the right amount of memory?

The inconvenience comes later, when you write data to memory that you haven't properly allocated and cause problems for other sections of your program, or cause your program to crash. C allows you to do all sorts of things that are dangerous, and writing data to places that you shouldn't creates bugs that can be very difficult to track down.

My question is what will happen with the first allocated memory assigned to d?

Because you haven't freed the memory that d points to before assigning a new value to d, the first block of memory will continue to exist, but you won't be able to access it because you no longer have it's address. This is typically called a memory leak. In the case you've shown, you're only leaking 4 characters' worth of data, so it's not a huge problem, but if that same section of code is executed repeatedly you could end up losing access to a lot of memory. (Don't worry, though -- the entire memory space will be reclaimed once your program exits.) So, if you have the only pointer to a block of memory that a you've allocated, free that block before you reassign the variable.

Upvotes: 1

anonymousBeaver
anonymousBeaver

Reputation: 48

C does not have bounds checking, so depending on your compiler, you may not see an error. Remember, when accessing elements of an array in C, you're really dereferencing the address resulting from pointer arithmetic. For example, I can access the ith element of some array a as follows:

*(a+i) // Equivalent to a[i]

So you see that the reason why you may still be able to read/write to an index beyond the space allocated is because you're still dereferencing a valid read/write address, however, you have no idea what the program is actually using that piece of memory for and can induce several errors depending on the use.

Upvotes: 0

chux
chux

Reputation: 154198

am trying to understand the behavior of memory allocation in c.
I am trying to add data to it outside its allocated memory

This is undefined behavior (UB). Anything may happen. To understand the behavior of memory allocation in C, use code with defined behavior.

Start with

// char *c = (char*)malloc(1*sizeof(char));
char *c = malloc(sizeof c[0] * 6);
if (c) {
  ...

Upvotes: 1

cryptxum
cryptxum

Reputation: 333

It is undefined behaviour to write outside the memory given to you by malloc, calloc, etc. The 'inconvenience' is that your program may or may not work each time you run it, as the operating system won't always check if you are reading/writing inside of the bounds of your memory chunk. In reality what probably happened is that the OS allocated you more memory than you asked so you have a bit of leeway before you get a segfault. But by no means does this mean that you should write out of the bounds of what you allocated.

In your second block of code, because you assigned to d the return value of mallocbyme without freeing the original malloc, you now have a memory leak.

Upvotes: 1

Related Questions