Re: kernel memory issue using malloc()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Well the kernel manages the memory and malloc makes a kernel call
to give you or not the amount of memory requested... so it is a kernel
issue and therefore my question fits totally in here.

Moreover, I found this on wikipedia:


     "Allocation failure

|malloc| is not guaranteed to succeed — if there is no memory available, or if the program has exceeded the amount of memory it is allowed to reference, |malloc| will return a |NULL| pointer. "


And I am not in these 2 cases so the kernel should be able to allocate 256 bytes while I have nearly
800 Mega bytes of free memory.
The thing I cannot figure out is why it is working with 512 (which is bigger than 256....obviously
it can allocate more memory but not less???) and not with 256.

There is something though I forgot to mention. The program is allocating 256 bytes for about 2500 times but the segmentation fault appears about the 2000th allocation and I know that later on there will be a segmentation fault because later on I need 376 bytes (after the 2050th allocation)
and I know that at that time there will be a segmentation fault.

So my question is: why is this segmentation fault appears after the 200th and not after the 2000th allocation? Does it have to do with some kind of locking features and memory of the kernel? Can it be a scheduling issue?

Thank you guys again for your answers.

MB



Nish Aravamudan wrote:
On 2/10/07, Mounir Bakkali <mounir.bakkali@xxxxxxxxx> wrote:
Hi everything,

Here is the issue:
I am trying to allocate some memory for my data structure
and after a msecs, I get a "segmentation fault" right after
"allocating mem"....
but if I change the size of the malloc from 256 to 512, it works just fine.

My guess is that somehow, internal there is a segmentation fault but not
in that peace of code but in another peace of code and that I get a
segmentation
fault only when I call malloc only when I try to allocate the memory....

I have checked the memory and it looks fine (lots of mem still available
at execution time
and right before the segmentation fault);

Please, can you confirm? Do you know why is it that doing so??
Is there a way to prevent such behaviour?


My kernel version is: 2.6.19.2
CPU: Pentium M (mobile centrino).
RAM: 1GB
Compiler: gcc 4.1.2-2
And here is the peace of code:

// the following code is called about 2500 times.
printf("allocating mem\n");
my = (char*) malloc(256);
printf("GOOD!\n");

What does this have to do with the kernel?

Thanks,
Nish



--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux