473,323 Members | 1,589 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Will somebody help assist me about the pointer issue?

I have written a C program under unix, but some weird errors occurred:

================================================== ==
#include <stdio.h>

typedef unsigned long ULONG;
typedef unsigned short UWORD;
typedef unsigned char UBYTE;
typedef unsigned char BOOLEAN;

typedef struct
{
UBYTE i1;
UBYTE i2;
UWORD i3;
ULONG i4;

}TEST;

int main(void)
{
UBYTE temp[20] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };

UBYTE *temp1 = temp + 2;

UWORD *temp2 = (UWORD*)temp1;

UWORD * temp3 = (UWORD*)(temp+2);

ULONG * temp4 = (ULONG*)(temp+2);

TEST *temp5 = (TEST*)(temp+2);
printf("temp2 value: %xh\n", temp2[0]); // 203h
printf("temp3 value: %xh\n", temp3[0]); // 203h
printf("temp4 value: %xh\n", temp4[0]); // Bus error
printf("temp5 i1 value: %xh\n", temp5->i1); // 2h
printf("temp5 i2 value: %xh\n", temp5->i2); // 3h
printf("temp5 i3 value: %xh\n", temp5->i3); // 405h
printf("temp5 i4 value: %xh\n", temp5->i4); // Bus error

return 0;

}
================================================== ==

Thanks,
Tim

Jul 16 '07 #1
7 1399
On 16 Jul, 10:35, sxz...@gmail.com wrote:
I have written a C program under unix, but some weird errors occurred:

================================================== ==
#include <stdio.h>

typedef unsigned long ULONG;
typedef unsigned short UWORD;
typedef unsigned char UBYTE;
typedef unsigned char BOOLEAN;

typedef struct
{
UBYTE i1;
UBYTE i2;
UWORD i3;
ULONG i4;

}TEST;

int main(void)
{
UBYTE temp[20] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };

UBYTE *temp1 = temp + 2;

UWORD *temp2 = (UWORD*)temp1;

UWORD * temp3 = (UWORD*)(temp+2);

ULONG * temp4 = (ULONG*)(temp+2);

TEST *temp5 = (TEST*)(temp+2);

printf("temp2 value: %xh\n", temp2[0]); // 203h
printf("temp3 value: %xh\n", temp3[0]); // 203h
printf("temp4 value: %xh\n", temp4[0]); // Bus error
printf("temp5 i1 value: %xh\n", temp5->i1); // 2h
printf("temp5 i2 value: %xh\n", temp5->i2); // 3h
printf("temp5 i3 value: %xh\n", temp5->i3); // 405h
printf("temp5 i4 value: %xh\n", temp5->i4); // Bus error
Apart from lying to the printf command and trying to access data which
is not guaranteed to be appropriately aligned, I can't find any
errors...

Jul 16 '07 #2
sxz...@gmail.com wrote:
I have written a C program under unix, but some weird errors occurred:

================================================== ==
#include <stdio.h>

typedef unsigned long ULONG;
typedef unsigned short UWORD;
typedef unsigned char UBYTE;
typedef unsigned char BOOLEAN;

typedef struct
{
UBYTE i1;
UBYTE i2;
UWORD i3;
ULONG i4;

}TEST;

int main(void)
{
UBYTE temp[20] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };
UBYTE *temp1 = temp + 2;
UWORD *temp2 = (UWORD*)temp1;
UWORD * temp3 = (UWORD*)(temp+2);
ULONG * temp4 = (ULONG*)(temp+2);
TEST *temp5 = (TEST*)(temp+2);
printf("temp2 value: %xh\n", temp2[0]); // 203h
printf("temp3 value: %xh\n", temp3[0]); // 203h
printf("temp4 value: %xh\n", temp4[0]); // Bus error
printf("temp5 i1 value: %xh\n", temp5->i1); // 2h
printf("temp5 i2 value: %xh\n", temp5->i2); // 3h
printf("temp5 i3 value: %xh\n", temp5->i3); // 405h
printf("temp5 i4 value: %xh\n", temp5->i4); // Bus error

return 0;

}
================================================== ==
The Bus error is probably caused due to your non-aligned memory reads.
Also most of your printf format specifiers and their arguments don't
match. That's undefined behaviour.

Why do you need to do this type punning?

Jul 16 '07 #3
On Jul 16, 2:35 pm, sxz...@gmail.com wrote:
I have written a C program under unix, but some weird errors occurred:

================================================== ==
#include <stdio.h>

typedef unsigned long ULONG;
typedef unsigned short UWORD;
typedef unsigned char UBYTE;
typedef unsigned char BOOLEAN;

typedef struct
{
UBYTE i1;
UBYTE i2;
UWORD i3;
ULONG i4;

}TEST;

int main(void)
{
UBYTE temp[20] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };

UBYTE *temp1 = temp + 2;

UWORD *temp2 = (UWORD*)temp1;

UWORD * temp3 = (UWORD*)(temp+2);

ULONG * temp4 = (ULONG*)(temp+2);
This is wrong.
Address value, (temp + 2) may not be aligned to (sizeof ULONG) bytes.
For example if
(sizeof ULONG) is 8 bytes and if (temp + 2) is not aligned to 8 bytes,
the hardware may not allow
to read 8 bytes from that address and hence the bus error while
reading from that address.
Try printing (temp + 2) and you can verify that it is not aligned to
sizeof ULONG bytes.
>
TEST *temp5 = (TEST*)(temp+2);

printf("temp2 value: %xh\n", temp2[0]); // 203h
printf("temp3 value: %xh\n", temp3[0]); // 203h
printf("temp4 value: %xh\n", temp4[0]); // Bus error
printf("temp5 i1 value: %xh\n", temp5->i1); // 2h
printf("temp5 i2 value: %xh\n", temp5->i2); // 3h
printf("temp5 i3 value: %xh\n", temp5->i3); // 405h
printf("temp5 i4 value: %xh\n", temp5->i4); // Bus error
same reason as mentioned above.

Mohan
>
return 0;

}

================================================== ==

Thanks,
Tim

Jul 16 '07 #4
sx****@gmail.com wrote:
I have written a C program under unix, but some weird errors occurred:
There are no "weird" errors.
The only storage you allocated was of an unsigned char array.
You attempted to use the addresses of this array, aligned for chars, as
a pointer to unsigned shorts and unsigned longs. There is no reason to
think that char alignment is compatible with shorts and longs.
You further assumed that, not only temp[0] but also temp[2] were
correctly aligned for shorts and longs. Even if temp[0] were correctly
aligned for shorts and longs (which is a leap of faith on your part),
there is no reason to imagine temp[2] to be so.

In short, the only "weird error" is that you thought this to be
reasonable code.

>
================================================== ==
#include <stdio.h>

typedef unsigned long ULONG;
typedef unsigned short UWORD;
typedef unsigned char UBYTE;
typedef unsigned char BOOLEAN;

typedef struct
{
UBYTE i1;
UBYTE i2;
UWORD i3;
ULONG i4;

}TEST;

int main(void)
{
UBYTE temp[20] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };

UBYTE *temp1 = temp + 2;

UWORD *temp2 = (UWORD*)temp1;

UWORD * temp3 = (UWORD*)(temp+2);

ULONG * temp4 = (ULONG*)(temp+2);

TEST *temp5 = (TEST*)(temp+2);
printf("temp2 value: %xh\n", temp2[0]); // 203h
printf("temp3 value: %xh\n", temp3[0]); // 203h
printf("temp4 value: %xh\n", temp4[0]); // Bus error
printf("temp5 i1 value: %xh\n", temp5->i1); // 2h
printf("temp5 i2 value: %xh\n", temp5->i2); // 3h
printf("temp5 i3 value: %xh\n", temp5->i3); // 405h
printf("temp5 i4 value: %xh\n", temp5->i4); // Bus error

return 0;

}
================================================== ==

Thanks,
Tim
Jul 16 '07 #5
sx****@gmail.com writes:
I have written a C program under unix, but some weird errors occurred:
Really? *What* "weird errors"? You showed us your program, but you
didn't show us any error messages or output.

I tried your program myself. On some systems, it dies with a bus
error; on others, it executes without any apparent problems.
================================================== ==
#include <stdio.h>

typedef unsigned long ULONG;
What benefit does this typedef give you? Why not just declare thinks
as 'unsigned long'? (Note that "it saves typing" is not a good
reason; readability is far more important than ease of typing.
typedef unsigned short UWORD;
typedef unsigned char UBYTE;
typedef unsigned char BOOLEAN;
These typedefs aren't quite as bad, since they don't just repeat an
abbreviated version of the type name, but UWORD makes an unwarranted
assumption that the size of an unsigned short is one "word" (whatever
that means).

If I read a declaration that uses the name UWORD, I have to go back up
to the top of the program to see what UWORD means.

[snip]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 16 '07 #6
Mohan <aj****@gmail.comwrites:
On Jul 16, 2:35 pm, sxz...@gmail.com wrote:
>I have written a C program under unix, but some weird errors occurred:

================================================= ===
#include <stdio.h>

typedef unsigned long ULONG;
typedef unsigned short UWORD;
typedef unsigned char UBYTE;
typedef unsigned char BOOLEAN;

typedef struct
{
UBYTE i1;
UBYTE i2;
UWORD i3;
ULONG i4;

}TEST;

int main(void)
{
UBYTE temp[20] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19 };

UBYTE *temp1 = temp + 2;

UWORD *temp2 = (UWORD*)temp1;

UWORD * temp3 = (UWORD*)(temp+2);

ULONG * temp4 = (ULONG*)(temp+2);
This is wrong. Address value, (temp + 2) may not be aligned to
(sizeof ULONG) bytes. For example if (sizeof ULONG) is 8 bytes and
if (temp + 2) is not aligned to 8 bytes, the hardware may not allow
to read 8 bytes from that address and hence the bus error while
reading from that address. Try printing (temp + 2) and you can
verify that it is not aligned to sizeof ULONG bytes.
Yes, it's wrong, but you mis-state the reasons.

temp is an array of unsigned char. The entire array, not just its
individual elements, can be at an arbitrary misaligned address. Array
objects are often given stricter alignment than one byte, but it's not
guaranteed.

It's possible that, just by coincidence, temp+2 could be 8-byte
aligned.

The required alignment for any type is implementation-specific. The
only real requirement is that the size must be a multiple of the
required alignment (since arrays cannot have gaps between elements).

It's likely that *some* of the pointers in the OP's program will be
misaligned, but the language doesn't give us a clue about which ones.
And there are systems where all types have only a one-byte alignment
requirement, and the program will "work".

[snip]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 16 '07 #7
Keith Thompson <ks***@mib.orgwrites:
sx****@gmail.com writes:
>I have written a C program under unix, but some weird errors occurred:

Really? *What* "weird errors"? You showed us your program, but you
didn't show us any error messages or output.
[...]

Sorry, I didn't notice that you showed (a brief summary of) the output
as comments in the program:

printf("temp2 value: %xh\n", temp2[0]); // 203h
printf("temp3 value: %xh\n", temp3[0]); // 203h
printf("temp4 value: %xh\n", temp4[0]); // Bus error
printf("temp5 i1 value: %xh\n", temp5->i1); // 2h
printf("temp5 i2 value: %xh\n", temp5->i2); // 3h
printf("temp5 i3 value: %xh\n", temp5->i3); // 405h
printf("temp5 i4 value: %xh\n", temp5->i4); // Bus error

It's still better to show the exact output separately; it's easier to
see, and it gives us more confidence that you've shown us the actual
output.

And it's unlikely that you got two bus errors in the same program.
Apparently you modified the program and re-ran it. It would have been
good to mention that.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 16 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Bhalchandra Thatte | last post by:
I am allocating a block of memory using malloc. I want to use it to store a "header" structure followed by structs in my application. How to calculate the alignment without making any assumption...
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
8
by: Tim Rentsch | last post by:
Here's another question related to 'volatile'. Consider the following: int x; void foo(){ int y; y = (volatile int) x;
15
by: Daniel Billingsley | last post by:
Speaking of trying to read deeply nested if-else blocks... I often find it's not always easy to tell one indent level from another (granted I keep my tab settings low so I'm not halfway across...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
67
by: neilcancer | last post by:
i come from china,and i'm sorry that my english is very poor. now i'm studing data structure and i met some problem about c language. could you tell me what will happen after i use free()? i...
75
by: Steven T. Hatton | last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash, Mathematica, SML, or LISP. A college teacher recently posted to this newsgroup regarding her observation that there has...
8
by: Les Caudle | last post by:
Using this command line to run XSD.exe xsd /c /o:outputDir input.xsd /f on the following XSD <?xml version="1.0"?> <schema xmlns="http://www.w3.org/2001/XMLSchema"...
0
by: Frank Swarbrick | last post by:
We're trying to use SQL Assist in the DB2 Control Center for DB2/LUW 9.5 and we are getting the following error: "Routine "SYSIBM.SQLTABLES" (specific name "TABLES") has returned an error SQLSTATE...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.