![]() |
|
#1
|
||||
|
||||
seg fault on ptr to ptr assignment!Hello,
I am faced with a weird issue which I do not know how to approach. I have a function which multiplies two 64bit ints using 32bit wide chunks. The algorithm is pretty simple and we just have to shift and add the partial products. And I have managed to write a function which does just that. But Im facing seg fault issues within the function which I assume is mostly due to referring illegal memory. BUt I do not seem to find out why exactly is that... The program is as shown below. I have put a comment above the first line which gives me a seg fault. The header file libepc.h contains the definitions for DWORD32 and QWORD64 which are nothing but unsigned long and unsigned long long on an x86 respectively. I have it because I plan to use some other routines from that header file in order to communicate with a machine without an operating system. CPP / C++ / C Code:
You do not need to know any assembly for that I guess __________________
Hope to hear from you guys! -------------------------------------------------- Best Regards, Aijaz Baig. |
|
#2
|
|||
|
|||
Re: seg fault on ptr to ptr assignment!Quote:
CPP / C++ / C Code:
You have other places where you store things using an uninitialized pointer. Also... I won't even try to understand the code or your approach. I mean, if I really studied it, I might ask why the heck you are using all of those pointers in the first place. If the compiler you are using supports 64-bit operations, then why not just store the 64-bit values with assignment statements (Maybe have variables named upper_upper, upper_lower, lower_upper, and lower_lower or some such things to hold the partial products.) Also, I note the following: You allocate an array of 16 unsigned chars: CPP / C++ / C Code:
Then, with this array as the argument in calling the function CPP / C++ / C Code:
There are two things wrong: 1. You are storing something beyond the end of the array. 2. I think you mean to store zero, but that is an invalid character constant. (Didn't your compiler give a warning? My GNU compiler complained with the message "large integer implicitly truncated to unsigned type".) If other parts of your program depend on having a zero in whatever position you store it, it probably won't find it. (My GNU compiler gives a value of 0x30 there, but the actual result is undefined.) If you want to store zero in that character position, you can use something like CPP / C++ / C Code:
CPP / C++ / C Code:
However, it's kind of silly to store a zero byte here anyhow. The result will be in the 16 chars of the array. Some of the bytes in the result may be zero. There is absolutely no reason to put a zero byte at the end, even if you had room for it. This leads up to my last observation: FInally: Your result will be some binary values stored in your char array. You certainly can't expect puts() to print the value of the result, since puts() prints chars corresponding to each element of the array. Quote:
First suggestion: Get it to work on your workstation. Then look at ways of getting it to fit into your target. Sometimes we use globals because of hardware considerations that limit the amount of stack space. (After all, in assembly-language programs everything is a global, even temporary variables used in different subroutines. You do what you have to do.) Also, declaring something a "register' variable should have absolutely no effect on any workstation compiler, so seg-faulting with or without "register" declarations is coincidental. Getting the target compiler or cross-compiler to use "register" variables is very implementation-dependent, and my recent experiences have shown time and time again that when I try to "help" the compiler make the "right" decision it doesn't help (and, sometimes actually hurts). Regards, Dave "Functionality first; optimization later." ---davekw7x |
|
#3
|
||||
|
||||
Re: seg fault on ptr to ptr assignment!hello,
Quote:
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
Then I have used an expression like the following in the code: CPP / C++ / C Code:
I wanted to add upper_mlr(32bits) to the upper 32bits of pp_lower1(64bits) and pp_upper0(64bits). I do this by casting their addresses as char* and then increase the addresses by 4 chars, so I get the upper 32 bits. I wonder if its ok to do that!..but it works for me though And again, as Im using gdb to try to find my way through the code, I see some interesting things. When I print tmp_32_lower using gdb, it gives me '0x027fc9f5' and when i use memmove to copy this to the next 4 bytes of 'result' using CPP / C++ / C Code:
I hope i haven't messed up my question too much...but I can frame it more clearly if you guys say so.. Keen to hear from you guys! __________________
Hope to hear from you guys! -------------------------------------------------- Best Regards, Aijaz Baig. Last edited by aijazbaig1 : 21-Apr-2008 at 05:06.
|
|
#4
|
|||
|
|||
Re: seg fault on ptr to ptr assignment!Quote:
Quote:
gdb is a tool. Not all tools are perfect. Since we can't look over your shoulder and see exactly what you see, it's pretty hard for me personally to see how to help with gdb and with the interpretation of its output. Sometimes using tools like gdb can help give you some insight. With other people's code, I sometimes use gdb when the code confuses me. Does your code give you what you expect to get? Did you try something like: CPP / C++ / C Code:
Code:
In other words, when gdb confuses me, I look at the code and make the program tell me exactly what it is seeing. By the way, using memmove the way that you did to copy lower and upper parts of the variables means that it only works with little-endian systems. If your target system is little-endian, then it will work the same way as on your little-endian workstation, but not all embedded system processors are little-endian. (Intel X-scale, for example, only works as a big-endian machine with their distribution code.) Just thought I would mention it. Also, you didn't ask, but I observe that there is a bug in your function that gives errors for certain large input values. For example If you change L1 and L2 both to be 0xffffffffffffffff (That's 64 bits of all ones), you get Code:
Whereas, the correct value is given by Code:
Regards, Dave |
|
#5
|
|||
|
|||
Re: seg fault on ptr to ptr assignment!Quote:
Oh, no! I made an error pasting that in there. I had the right value on my screen, but my hand slipped when copying to the post. I really, really hate it when that happens. Here's the correct value: Code:
That is: 15 'f' and an 'e', then 15 '0' and a '1' I regret the error. Regards, Dave Footnote: You don't even need a computer for multiplications like this: If I use '^' to indicate "to the power" Code:
Code:
|
|
#6
|
||||
|
||||
Re: seg fault on ptr to ptr assignment!Hi everybody,
After taking a break, im back at them labs in school. Well, luckily I regrouped and now after a through rethinking of the whole problem, I was finally able to get this assignment done in Assembly. On the contrary, the C version of this function gives the correct result for the operands 0xFFFFFFFFFFFFFFFF and 0xFFFFFFFFFFFFFFFF and I do see the 'E' in the correct place. Now suprisingly it isn't working for the other test cases needed to complete this lab. Looks like it is the way we display the result may be. The result has to be stored into an array of unsigned character. Hence we have to progress byte by byte and display the result. Before I show the code,I would like to say that for some reason, my lab partner wanted to work with 64bit values directly instead of dividing it into 32bit wide chunks and to me that looked unneccesarily complicated. Nonetheless, it works for the first test case and it doesn't work for the others as it gives drastically different results. Heres the C code: CPP / C++ / C Code:
The header file needed for this is here: CPP / C++ / C Code:
However in actuality Im supposed to use the ones for the embedded device and I have commented them out so they can still be seen. I hope to get some insights into why my program doesn't work for the other seemingly simpler inpputs but it seems to work for the corner case which dave also pointed out in his previous post. __________________
Hope to hear from you guys! -------------------------------------------------- Best Regards, Aijaz Baig. |
|
#7
|
|||
|
|||
Re: seg fault on ptr to ptr assignment!Quote:
Quote:
So, it's incorrect and you have to debug it. I mean, when you get seg faults it is sometimes hard proceed. But when the program runs to completion and you get output and it's wrong, then you can track it down. You have put a few print statements in place. Use them. First of all, I like to make the print statements actually print something in a meaningful format so that I can use the output to see where things went wrong. For example, take the test case in this post: CPP / C++ / C Code:
This much of the code gives Code:
Now one thing for sure: if it is done correctly, then none of the other cross products can affect the lower 32 bits of this, right? Now sneak a peak the correct answer given for this problem: the 32 least significant bits are 0x7C0481B5, which agrees with the 32 least significant bits of the printed value. So up to this part it's OK. Now fix the print statement in your main() function so that it actually prints the output array from the function, and you will see something like Code:
If you have any specific questions about what you don't understand about what you see, then ask, but I won't go any further in trying to unravel the stuff in your function; it's your problem, not mine. Just take it a step at a time. Regards, Dave |
|
#8
|
||||
|
||||
Re: seg fault on ptr to ptr assignment!Hello there,
thanks dave for giving me some solid examples. I was finally able to fix the code and what i was doing wrong was that I was not putting the correct values in the correct places i.e. the way i transferred stuff to 'result' was just so wrong... anyway, heres the correct way in which I have done the transfer and it seems to work now: CPP / C++ / C Code:
Well...this one works and im satisfied..thnks again __________________
Hope to hear from you guys! -------------------------------------------------- Best Regards, Aijaz Baig. |
Recent GIDBlog
First week of IA training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Assignment Operator and Pointer | Peter_APIIT | CPP / C++ Forum | 14 | 25-Sep-2007 05:36 |
| double concatenated dynamic list - different type assignment warning | dlp | C Programming Language | 11 | 01-Jun-2007 07:59 |
| Thread synchronization Assignment help!! | rossi143 | C Programming Language | 1 | 25-Mar-2007 20:10 |
| Am I reading this assignment correctly? | earachefl | CPP / C++ Forum | 2 | 09-May-2006 09:39 |
| Circular Linked Queue Copy Constructor and Assignment Operator Not Working? | wc3promet | CPP / C++ Forum | 0 | 17-Oct-2004 07:55 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The