Buddy Algorithm Implementation using Linked Lists in C
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Buddy Algorithm Implementation using Linked Lists in C
Hi all
I tried implementing a buddy algorithm. But I keep on getting segmentation faults. I am a novice so I would appreciate any help with the code.
thanks in advance
Here is the full code :
Its compiling fine but gives a segfault while execution
The input is of the form
U L
P S
Where the maximum memory size is 2 to the power of U, and the minimum block size is 2 to the power of L.
P is the name of the block to be allocated and is only of the range A to Z and S is the size of the memory to be allocated. If S is 0 then that letter should be deallocated.
#define LETTER_A 65
#define LETTER_Z 90
#define BASE 2
int minSize;
struct tree_str
{
int used; /* size taken in allocated size */
int size; /* size of size */
char alloc; /* name of allocated size */
struct tree_str * right; /* pointer to right child */
struct tree_str * left; /* pointer to left child */
};
please use [code][/code] to keep formatting. Also please provide a small instruction, how to use (invoke) this tool and how to reproduce the error. Also copy the full error message:
Quote:
Program received signal SIGSEGV, Segmentation fault.
0xff24f9e8 in memccpy () from /lib/libc.so.1
(gdb) where
#0 0xff24f9e8 in memccpy () from /lib/libc.so.1
#1 0xff2cce8c in fgets () from /lib/libc.so.1
#2 0x10c7c in main () at t.c:126
and you will see the variable in has not been initialized, so that causes the segfault.
I think you have misunderstood the point of the buddy algorithm, so your whole approach is wrong.
I haven't seen the assignment or other materials your instructor gave you, so I can't give you more specific guidance on the algorithm. But on the C programming ...
You seem to misunderstand the nature of parameter passing in C. Parameters are passed by value, not by reference. You seem to expect that modifying a parameters (small) inside a function will modify the caller's copy of that variable outside the function. It doesn't work that way.
I made the necessary corrections in my code pointed out by you guys. Thanks a tonne for the help. However I keep getting segfaults when I try to deallocate. Kindly help with that.
Thanks in advance.
PS:
The program takes as input the upper limit and the lower limit of the memory space in terms of power of 2. Therefore,
INPUT
10 4
20
10 is the upper limit -> 2^10=1024 is the biggest memory block
4 is the lower limit -> 2^4=16 is the smallest block size.
20 is the bytes to be allocated.
if I enter 0 for deallocation, i get segfaults and that is what I need help in fixing.
Last edited by fahad.anwar; 07-24-2012 at 05:10 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.