LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-27-2011, 07:09 AM   #16
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled

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?
 
Old 05-27-2011, 07:34 AM   #17
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by akshay_satish View Post
But the code definitely has a problem. Agree?
If the code is seg-faulting, then obviously there is a problem.
 
Old 06-20-2011, 04:14 AM   #18
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
hi @dwhitney67,

i got a query

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 04:18 AM.
 
Old 06-20-2011, 05:58 AM   #19
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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 06:00 AM.
 
Old 06-30-2011, 07:50 AM   #20
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
Smile

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 12:12 PM.
 
Old 06-30-2011, 08:10 AM   #21
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,533

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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 08:12 AM.
 
Old 06-30-2011, 08:39 AM   #22
akshay_satish
Member
 
Registered: May 2011
Posts: 63

Original Poster
Rep: Reputation: Disabled
thoughts...

Last edited by akshay_satish; 07-01-2011 at 10:56 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
segv debugging prakash_m80 Linux - Software 0 03-20-2009 04:10 AM
pls convert this windows code into linux code nagendrar Programming 6 06-03-2008 07:00 AM
g_thread_init SEGV The_Nerd Programming 1 06-20-2004 05:00 PM
pls pls pls help me ! i'm tired with httpd config on fedora apache 2.0.48 AngelOfTheDamn Fedora 0 01-24-2004 05:12 PM


All times are GMT -5. The time now is 08:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration