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 7 1429
On 16 Jul, 10:35, sxz...@gmail.co m 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...
sxz...@gmail.co m 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?
On Jul 16, 2:35 pm, sxz...@gmail.co m 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
sx****@gmail.co m 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 sx****@gmail.co m 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_Keit h) 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"
Mohan <aj****@gmail.c omwrites:
On Jul 16, 2:35 pm, sxz...@gmail.co m 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_Keit h) 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"
Keith Thompson <ks***@mib.orgw rites:
sx****@gmail.co m 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_Keit h) 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" This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
about the most restrictive type on my machine?
Thanks.
|
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 all-bits-zero, and does not therefore guarantee useful null
pointer values (see section 5 of this list) or floating-point zero values.
|
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;
|
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 the page by
the 3rd level), and I find myself doing things like this to help me keep it
straight:
class MyClass
{
public void SomeMethod()
|
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;
| |
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 mean once i
use free() on a pointer,what will the pointer points to ?
for example:
#include<stdio.h>
|
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 been a significant
decline in the number of students opting to take courses in C++. I just
want to let people know what I believe are the biggest obstacles to C++
language acquisition, and what aspects of the language make it less
appealing than...
|
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"
xmlns:dino="http://example.com/test" targetNamespace="http://example.com/test"
elementFormDefault="qualified">
|
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 with diagnostic text "SYSIBM:CLI:-727". SQLSTATE=38553"
If I try to call the SP directly I get the same error:
call sysibm.sqltables('','ACCTASGN','AUTO_NUMBERS','','');
SQL0443N Routine "SYSIBM.SQLTABLES" (specific name "TABLES") has...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |