Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Ahhh I think I am goint to contradict.. I did not happen to hit the fault on an ia-64. i think it is because of the memory layout that we somehow bail out without the fault. But the code definitely has a problem. Agree?
I have something like this in a smaple code;
unsigned int *a = 0;
if ((a == 0) || (*a == 0)) {
....
...
..
.
}
Can this cause a dump? The *a == 0 surely does core, but I think compiler traverse's L -> R. So, it evaluates only a == 0 and succeeds. We somehow bail out of the faulty code. Can this behavior be speculated, like is there any instance when it can evaluate R->L?
Wanted to confirm.
Last edited by akshay_satish; 06-20-2011 at 05:18 AM.
AFAIK, and from years of experience, the expression is always evaluated from L -> R.
Thus your code, albeit awkward, will be ok. Personally, rather than confuse yourself or others months/years from now, it would be better to write your code such as:
Code:
int* a = 0; // or NULL
if (a != 0 && *a == 0)
{
...
}
The usage of parenthesis around each conditional is optional, but some like it for readability.
Last edited by dwhitney67; 06-20-2011 at 07:00 AM.
I have a question regarding segfault. Dwhitney67 if you can pitch in , pleasure
int HashTable<T>::search(
const char *key,
const T &item,
unsigned int *index,
unsigned int &n)
{
*index = hash(key, n);
if (*(m_freeList[m_Table[*index].ptr]) == item)
{
return HASH_SUCCESS;
}
else //collision, keep searching
{
*index = hash(key, ++n);
continue;
}
}
....
....
}
When calling
hash(key, n); we use the djb2 algorithm to get the hash value.
I have a segfault (which seems to happen really very very rare) around this line;
*(m_freeList[m_theTable[*index].ptr]; though it doesn't say directly, but after unwinding and seeing the load instruction and this one I pasted below seemes to have cored.
*(m_freeList[m_Table[*index].ptr]
A large value getting assigned to *index can also segfault?
How can a key in a hash table become NULL?
Last edited by akshay_satish; 07-01-2011 at 01:12 PM.
From the very first line of your function, I could easily deduce that you are averse to defensive programming.
Code:
*index = hash(key, n);
In the line above, you assume that index is a valid pointer; that's not a very good idea.
The other issue I see is where you access m_Table, using possibly an index that is out of range. I do not know how you have m_Table declared, thus I cannot tell if it is an array or a vector. If the latter, then you should check the index versus the size of the "table", or handle an exception if you opt to use the at() method.
As for the following statement, you are once again making assumptions without any error checking; I would suggest that you program a little more defensively rather than make wild assumptions.
Code:
if (*(m_freeList[m_Table[*index].ptr]) == item)
Above you are accessing two arrays, never once checking to ensure the indexes are proper, and then de-referencing a pointer, which may be NULL.
P.S. Why is index passed as a pointer to the function; pass it by reference. Passing pointers is seldom done in C++.
P.S. #2 Note how I have shown the code above... I have used CODE tags; please do the same in the future when posting code, for it makes it easier to read, and it preserves the formatting of the code.
Last edited by dwhitney67; 06-30-2011 at 09:12 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.