Re: Jemalloc SEGV for 1MB chunk

From: Rink Springer <rink_at_FreeBSD.org>
Date: Wed, 28 Jan 2009 13:13:49 +0100
Hi,

On Wed, Jan 28, 2009 at 05:31:43PM +0530, Channa wrote:
> size_t size = 1048576 ;
> 
>         buf = malloc(size);
>   for (i = 0; i <= 1048575; i++)
>     buf[i] = 'a';
> buf[size]='\0';
> printf("The length of buff is : %d\n",strlen(buf));
> free(buf);
> return 0;
> }
> 
> I NULL terminated the string
> buf[size] = '\0'  <== The last character is NULL
> 
> But still i get a SEGV at strlen.
> 
> Could you please tell me if my changes above are correct?

They are not - because size = 1048576, you will get a buffer of 1048576
bytes, that is, buf[0] ... buf[1048575] = buf[0] .. buf[size - 1]. Thus,
you need to do:

	buf[size - 1] = '\0';

And it'll work.
Regards,

-- 
Rink P.W. Springer                                - http://rink.nu
"Chance favours the prepared mind"
- Penn
Received on Wed Jan 28 2009 - 11:12:33 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:39:41 UTC