From owner-freebsd-hackers@FreeBSD.ORG Thu Apr 12 19:37:11 2012 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id 237AF106564A for ; Thu, 12 Apr 2012 19:37:11 +0000 (UTC) (envelope-from jilles@stack.nl) Received: from mx1.stack.nl (relay02.stack.nl [IPv6:2001:610:1108:5010::104]) by mx1.freebsd.org (Postfix) with ESMTP id A67A28FC0A for ; Thu, 12 Apr 2012 19:37:10 +0000 (UTC) Received: from snail.stack.nl (snail.stack.nl [IPv6:2001:610:1108:5010::131]) by mx1.stack.nl (Postfix) with ESMTP id BDEF73624BB; Thu, 12 Apr 2012 21:37:09 +0200 (CEST) Received: by snail.stack.nl (Postfix, from userid 1677) id A6C942847A; Thu, 12 Apr 2012 21:37:09 +0200 (CEST) Date: Thu, 12 Apr 2012 21:37:09 +0200 From: Jilles Tjoelker To: Maninya M Message-ID: <20120412193709.GA7521@stack.nl> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Cc: freebsd-hackers@freebsd.org Subject: Re: mmap segmentation fault X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 12 Apr 2012 19:37:11 -0000 On Fri, Apr 13, 2012 at 12:25:32AM +0530, Maninya M wrote: > I want to allocate memory at a specified address location 'a' of size 'b'. > I wrote code below to do it, but I'm getting a seg fault. How can I solve > this? > How can I get the allocated memory at the required address? > int main() > { > unsigned int *addr,*newaddr; > unsigned long a=134516736,a1; > unsigned long b=3895296; > unsigned long flags =6; > a1=(a&0xffff0000); > printf("%x\n",(void *)a); > newaddr=(unsigned int *)mmap((void *)a,b,6,MAP_ANONYMOUS|MAP_FIXED,-1,0); > if(newaddr==MAP_FAILED) > printf("mmap failed"); > else > printf("sucess %x",newaddr); > return 0; > } > Output is > 8049000 > Segmentation fault If this is i386, you're mapping onto the executable itself. If you really want to map something there, you will have to move your code somewhere else or manipulate your executable to contain a suitable memory area at the required address. Try, for example, procstat -v $$ -- Jilles Tjoelker