public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Re: malloc/free question..
       [not found] <4f63ca6a.434fb60a.3a21.5cf3SMTPIN_ADDED@mx.google.com>
@ 2012-03-16 23:22 ` niXman
  0 siblings, 0 replies; 3+ messages in thread
From: niXman @ 2012-03-16 23:22 UTC (permalink / raw)
  To: gcc-help

2012/3/17 I Rattan <ratta1i@cps.cmich.edu>:
>
> Under Linux with gcc version 4.4.6 (Debian 4.4.6-11)
>
> The following code:
>
> int main(int argv, char** args)
> {
>  int *arr;
>  int n;
>
>  n = 10000000;
>
>  arr = (int *)malloc(4*n);
>  free((void *)&arr);
>  exit(0);
> }
>
> generates warning:
>
>        err.c:14: warning: attempt to free a non-heap object 'arr'
>
> any ideas?
>
> -ishwar
>

int main(int argv, char** args)
{
 int *arr;
 int n;

 n = 10000000;

 arr = (int *)malloc(4*n);
 //free((void *)&arr);
 free(arr);
 exit(0);
}

http://liveworkspace.org/code/a6a92573a92d663fd7f863225863f0f7

-- 
Regards,
  niXman

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: malloc/free question..
       [not found] <4f63ca67.434bb60a.1711.6e81SMTPIN_ADDED@mx.google.com>
@ 2012-03-17  0:00 ` Ángel González
  0 siblings, 0 replies; 3+ messages in thread
From: Ángel González @ 2012-03-17  0:00 UTC (permalink / raw)
  To: I Rattan; +Cc: gcc-help

On 17/03/12 00:14, I Rattan wrote:
> Under Linux with gcc version 4.4.6 (Debian 4.4.6-11)
>
> The following code:
>
> int main(int argv, char** args)
> {
>   int *arr;
>   int n;
>
>   n = 10000000;
>
>   arr = (int *)malloc(4*n);
>   free((void *)&arr);
>   exit(0);
> }
>
> generates warning:
>
>     err.c:14: warning: attempt to free a non-heap object 'arr'
>
> any ideas?
>
> -ishwar

arr is already a pointer. &arr is the storage of the pointer in the stack.
Thus the warning is perfectly valid. Your code is wrong.

You don't need to cast the free() parameter or the malloc() return value
in C.
I suspect malloc() isn't prototyped, and gcc is assuming malloc()
returns int.
That can have catastrophic results on systems such as 64 bit Linux.
You should #include <stdlib.h>, which provide malloc() and free()
prototypes
(as well as exit).

^ permalink raw reply	[flat|nested] 3+ messages in thread

* malloc/free question..
@ 2012-03-16 23:18 I Rattan
  0 siblings, 0 replies; 3+ messages in thread
From: I Rattan @ 2012-03-16 23:18 UTC (permalink / raw)
  To: gcc-help


Under Linux with gcc version 4.4.6 (Debian 4.4.6-11)

The following code:

int main(int argv, char** args)
{
   int *arr;
   int n;

   n = 10000000;

   arr = (int *)malloc(4*n);
   free((void *)&arr);
   exit(0);
}

generates warning:

 	err.c:14: warning: attempt to free a non-heap object 'arr'

any ideas?

-ishwar

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-03-17  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <4f63ca6a.434fb60a.3a21.5cf3SMTPIN_ADDED@mx.google.com>
2012-03-16 23:22 ` malloc/free question niXman
     [not found] <4f63ca67.434bb60a.1711.6e81SMTPIN_ADDED@mx.google.com>
2012-03-17  0:00 ` Ángel González
2012-03-16 23:18 I Rattan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).