-3

So the question is, how does the original developer prevent a situation where user/new developer is trying to access a free'd memory element.

int *num=(int *)malloc(n*sizeof(int));
int i;
for(i=0;i<n;i++)
{
 scanf("%d",&num[i]);        
}    

for(i=0;i<n-1;i++){
temp = some_function(x);        
} 

free(num);

for(i=0;i<n;i++)
{
  printf("\nnum[%d]= %d\n",i,num[i]);        
}

P.S.: The above code works and actually prints out data in array. Which is not our intention. [EDIT] Sorry if I was not clear enough. Someone suggested to ask this as a separate question, I thought why not. Here's the original post

Community
  • 1
  • 1
  • 1
    _The above code works and actually prints out data in array._.. welcome to UB. – Sourav Ghosh Jun 17 '16 at 08:05
  • 1
    [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Jun 17 '16 at 08:06
  • The code works on one particular platform, probably in a debug build. Running this one a different system (or a release build) may crash. it depends on the memory allocator. Some allocators will zero (or set all bytes to something else) after free, so you would get different results then. – Neil Jun 17 '16 at 08:07
  • _Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question._ – Sourav Ghosh Jun 17 '16 at 08:08
  • 1
    Thumb injury is prevented by not hitting your thumb with a hammer. Undefined behavior is prevented by not writing bad code. – Art Jun 17 '16 at 08:14
  • @SebastianRedl thanks for your input, I totally agree with you, its not the developers responsibility to care about user/new developers unethical style of coding. Im just trying to find out ways to be safe :) – CodeInfinity Jun 17 '16 at 08:15
  • One of the easiest fixes in the snippet above is to Always (*always*) validate the return of `scanf` to know whether or not your program is processing garbage from that point on. – David C. Rankin Jun 17 '16 at 08:16
  • @SouravGhosh sorry for the confusion, original post was here. [http://stackoverflow.com/questions/37851778/free-wont-delete-memory-allocated-to-the-pointer-int-array-using-free-twice] – CodeInfinity Jun 17 '16 at 08:35

2 Answers2

4

The fact that your code "works" is a manifestation of the undefined behaviour that you're experiencing when reading memory that you no longer own.

You could consider setting num to NULL after the first free call. Then writing num[i] will almost certainly crash the program. It can also occasionally be useful: a free called with NULL passed as the pointer is a no-op.

There's little else you can do unfortunately.

But, in general, setting freed pointers to NULL leads to sloppy programming so I tend to avoid it.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

The first thing you can do is set num=NULL.

More generally you can add a malloc hook to write garbage to the memory before freeing the memory (probably debug only). This will force a crash if the memory contains pointers or print garbage in this case when accessing freed memory.

Also since you are talking C++, make use of smart pointers.

Edit: fixed up the crash issue.

doron
  • 27,972
  • 12
  • 65
  • 103