LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-29-2006, 03:48 PM   #1
EmtYskY
LQ Newbie
 
Registered: Apr 2005
Posts: 12

Rep: Reputation: 0
Type Casting problem C


Hi

I am to about to make a program reads partition table. I put MBR sector copy on a unsigned char *buffer of 512 bytes of size. I wanted to validate the last fields of MBR which is the Checksum has 2 bytes. First I create a pointer *signature= malloc(2 * sizeof(unsigned char)) and then point it to the 510 position on MBR buffer but It hasn't worked correctly I had only the 1st byte of the checksum.

Any Suggestion please ?

Thanks
 
Old 12-29-2006, 04:22 PM   #2
dombrowsky
Member
 
Registered: Dec 2005
Location: New York
Distribution: Debian/GNU
Posts: 235

Rep: Reputation: 31
perhaps my C syntax is a bit rusty, but I believe this is wrong:

*ptr = malloc(size);

In fact, the following code will compile with a warning, but instantly coredumps on my machine:
Code:
int main(void){
  char *ptr = (char*)0;
  printf("%p\n",(void*)ptr);
  *ptr = malloc(16);
  printf("%p\n",(void*)ptr);
  ptr = malloc(16);
  printf("%p\n",(void*)ptr);
}
Comment out the bad line ->
Code:
int main(void){
  char *ptr = (char*)0;
  printf("%p\n",(void*)ptr);
  /* *ptr = malloc(16); */
  printf("%p\n",(void*)ptr);
  ptr = malloc(16);
  printf("%p\n",(void*)ptr);
}
results:
Code:
0x0
0x0
0x660578
 
Old 12-29-2006, 05:11 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by EmtYskY
First I create a pointer *signature= malloc(2 * sizeof(unsigned char)) and then point it to the 510 position on MBR buffer [..]
If set the pointer signature to some place in some other buffer, then the address of the new malloc()ed memory is lost. This is a memory leak. You can just point signature to buffer+510, or read directly from buffer[510] and buffer[511] to get the checksum.


Quote:
Originally Posted by EmtYskY
[..] but It hasn't worked correctly I had only the 1st byte of the checksum.
You probably need to cast buffer[510] to the type of the checksum. As this is 2 bytes, you need to make sure not to read past the end of the buffer.

Probably a safer way is to transfer the bytes to an certain type using bitwise shifting. For example, if you need to read the checksum in a unsigned int, you could do something like:
Code:
unsigned int read_chksum;
read_checksum = (buffer[511] << 8) | buffer[510];
/* switch [511] and [510] depending on endianess of the on-disk checksum*/
Hope this helps.
 
Old 12-29-2006, 06:01 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
In order to gaurantee a 16 bit type, you could also use uint16_t, although that is C99 only.
 
  


Reply

Tags
error, syntax



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
A Couple of C++ questions, casting -0- Programming 7 06-27-2005 12:14 PM
How to do type casting in Sparc Assembly Language foxele Programming 1 10-09-2004 04:40 PM
casting cynthia Programming 6 10-05-2004 03:58 AM
c++, casting, templates kev82 Programming 0 08-24-2004 05:09 PM
C++ Casting bretthoward Programming 1 05-11-2004 01:11 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 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
Open Source Consulting | Domain Registration