473,657 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strange crash - cast short* to int*

Hi,

the program below workes w/o problems on a GP2X and on the PC, but my
PocketPC (using GCC 3.3.3) crashes.
Very dissapointing, since I expect some speed boost from it.
Thnak you for your help.
int dx=..; // number of pixels width to draw
// one pixel is one unsigned short

// Get some pointer to the screen to draw a rect
unsigned short* pScreen = getColorBuffer( x,y);

int num_q = dx>>2; // number of 4 pixel pairs
int num_r = (dx & 0x00000003); // remaining pixels -loop unrolling

// prepair a 2 pixel pair "long"
unsigned long colcol = col | ((unsigned long)col << 16);
// get a long pointer to that screen (draw 2 "shorts" at once)
unsigned long* pScLong = (unsigned long*)pScreen;
... loop for each line
for(register int rx=0; rx<num_q; ++rx)
{
#ifdef THIS_CRASHES
*pScLong++ = colcol;
*pScLong++ = colcol;
pScreen+=4;
#else
*pScreen++ = col;
*pScreen++ = col;
*pScreen++ = col;
*pScreen++ = col;
#endif
}
... remaining pixels and next lines...
--
------------------------------------
Gernot Frisch
http://www.glbasic.com

Oct 15 '08 #1
6 1990
Sam
Gernot Frisch writes:
Hi,

the program below workes w/o problems on a GP2X and on the PC, but my
PocketPC (using GCC 3.3.3) crashes.
Very dissapointing, since I expect some speed boost from it.
Thnak you for your help.
int dx=..; // number of pixels width to draw
// one pixel is one unsigned short

// Get some pointer to the screen to draw a rect
unsigned short* pScreen = getColorBuffer( x,y);
There is no "getColorBuffer ()" function in the standard C++ library. Try
asking for help on a newsgroup for your hardware platform.
int num_q = dx>>2; // number of 4 pixel pairs
int num_r = (dx & 0x00000003); // remaining pixels -loop unrolling

// prepair a 2 pixel pair "long"
unsigned long colcol = col | ((unsigned long)col << 16);
// get a long pointer to that screen (draw 2 "shorts" at once)
unsigned long* pScLong = (unsigned long*)pScreen;
... loop for each line
for(register int rx=0; rx<num_q; ++rx)
{
#ifdef THIS_CRASHES
*pScLong++ = colcol;
*pScLong++ = colcol;
pScreen+=4;
#else
*pScreen++ = col;
*pScreen++ = col;
*pScreen++ = col;
*pScreen++ = col;
#endif
}
You did not specify your platform-specific word sizes. Assuming
sizeof(short)=2 , and sizeof(long)=4, the above math does not add up. Again,
has nothing to do with C++, the language. You can get a more definitive
answer on a more appropriate newsgroup.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkj 1z5EACgkQx9p3GY HlUOLt4QCfdvIqG KyvW1XX9bxtR6kO/msh
jNAAn0a3FDlNUNR UzYohePfpak9LGk fN
=4Fb0
-----END PGP SIGNATURE-----

Oct 15 '08 #2
In article <6l************ @mid.individual .net>, "Gernot Frisch"
<me@privacy.net wrote:
the program below workes w/o problems on a GP2X and on the PC, but my
PocketPC (using GCC 3.3.3) crashes.
Very dissapointing, since I expect some speed boost from it.
[...]
// get a long pointer to that screen (draw 2 "shorts" at once)
unsigned long* pScLong = (unsigned long*)pScreen;
... loop for each line
for(register int rx=0; rx<num_q; ++rx)
{
#ifdef THIS_CRASHES
*pScLong++ = colcol;
*pScLong++ = colcol;
pScreen+=4;
#else
*pScreen++ = col;
*pScreen++ = col;
*pScreen++ = col;
*pScreen++ = col;
#endif
}
... remaining pixels and next lines...
On some platforms, long has a stricter alignment requirement than short,
and violating that causes a crash. In your case, it's likely that long
must be aligned on a 4-byte boundary. One fairly portable solution is to
do an extra short if the address isn't yet 4-byte aligned, then do the
bulk as longs.
Oct 15 '08 #3
On Oct 15, 4:10*am, Sam <s...@email-scan.comwrote:
Gernot Frisch writes:
Hi,
the program below workes w/o problems on a GP2X and on the PC, but my
PocketPC (using GCC 3.3.3) crashes.
Very dissapointing, since I expect some speed boost from it.
Thnak you for your help.
*int dx=..; // number of pixels width to draw
*// one pixel is one unsigned short
*// Get some pointer to the screen to draw a rect
*unsigned short* pScreen = getColorBuffer( x,y);

There is no "getColorBuffer ()" function in the standard C++ library. Try
asking for help on a newsgroup for your hardware platform.
How about dx, x, y, etc. Should the poster go elsewhere because they
are not in the standard as well? Do you have problems with people
using foo()? Is that standard?
You did not specify your platform-specific word sizes.
Why is that needed for the question? If the poster new that the
platform-specific word sizes had anything with it, he wouldn't be
surprised.
Assuming
sizeof(short)=2 , and sizeof(long)=4, the above math does not add up. Again,
has nothing to do with C++, the language.
Do you know much about the C++ language? Are you claiming that sizeof
applied to types or their comparisons are not on-topic here? Your
releasing steam is off-topic here.
You can get a more definitive
answer on a more appropriate newsgroup.
This is an appropriate newsgroup. Why seek more?

Ali
Oct 15 '08 #4
ac******@gmail. com wrote:
On Oct 15, 4:10*am, Sam <s...@email-scan.comwrote:
>Gernot Frisch writes:
Hi,
the program below workes w/o problems on a GP2X and on the PC, but my
PocketPC (using GCC 3.3.3) crashes.
Very dissapointing, since I expect some speed boost from it.
Thnak you for your help.
int dx=..; // number of pixels width to draw
// one pixel is one unsigned short
// Get some pointer to the screen to draw a rect
unsigned short* pScreen = getColorBuffer( x,y);

There is no "getColorBuffer ()" function in the standard C++ library. Try
asking for help on a newsgroup for your hardware platform.

How about dx, x, y, etc. Should the poster go elsewhere because they
are not in the standard as well? Do you have problems with people
using foo()? Is that standard?
>You did not specify your platform-specific word sizes.

Why is that needed for the question? If the poster new that the
platform-specific word sizes had anything with it, he wouldn't be
surprised.
>Assuming
sizeof(short)= 2, and sizeof(long)=4, the above math does not add up.
Again, has nothing to do with C++, the language.

Do you know much about the C++ language? Are you claiming that sizeof
applied to types or their comparisons are not on-topic here?
I did not read that in that part you quoted.
Your releasing steam is off-topic here.
Hm, the posting from Sam does not sound very aggressive, as opposed to
yours.

>You can get a more definitive
answer on a more appropriate newsgroup.

This is an appropriate newsgroup. Why seek more?
Because the topical answer to the OP is that the cast

unsigned long* pScLong = (unsigned long*)pScreen;

and the following usage of pScLong is undefined behavior. That answer is
clearly not helpfull but all that the standard has to say about his
program. Therefore, looking for answers elsewhere is a good idea.
Best

Kai-Uwe Bux
Oct 15 '08 #5
On Oct 15, 11:14 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
acehr...@gmail. com wrote:
On Oct 15, 4:10 am, Sam <s...@email-scan.comwrote:
Gernot Frisch writes:
the program below workes w/o problems on a GP2X and on
the PC, but my PocketPC (using GCC 3.3.3) crashes. Very
dissapointing, since I expect some speed boost from it.
Thnak you for your help.
int dx=..; // number of pixels width to draw
// one pixel is one unsigned short
// Get some pointer to the screen to draw a rect
unsigned short* pScreen = getColorBuffer( x,y);
There is no "getColorBuffer ()" function in the standard C++
library. Try asking for help on a newsgroup for your
hardware platform.
How about dx, x, y, etc. Should the poster go elsewhere
because they are not in the standard as well? Do you have
problems with people using foo()? Is that standard?
You did not specify your platform-specific word sizes.
Why is that needed for the question? If the poster new that
the platform-specific word sizes had anything with it, he
wouldn't be surprised.
Assuming sizeof(short)=2 , and sizeof(long)=4, the above
math does not add up. Again, has nothing to do with C++,
the language.
Do you know much about the C++ language? Are you claiming
that sizeof applied to types or their comparisons are not
on-topic here?
I did not read that in that part you quoted.
Your releasing steam is off-topic here.
Hm, the posting from Sam does not sound very aggressive, as
opposed to yours.
His posting was presumptuous, and just noise, and of no help to
anyone.
You can get a more definitive answer on a more appropriate
newsgroup.
This is an appropriate newsgroup. Why seek more?
Because the topical answer to the OP is that the cast
unsigned long* pScLong = (unsigned long*)pScreen;
and the following usage of pScLong is undefined behavior. That
answer is clearly not helpful
Isn't it? It's certainly a necessary starting point: the code
was illegal, and if it worked on some platforms, it was just by
accident.

One might also point out why it was decided that this should be
undefined behavior. General considerations about alignment,
etc., are also on topic here.

Presumably, of course, the problem is due to the fact that on
some platforms, long requires more strict alignment than short,
and that his original short* didn't meet those requirements. On
an Intel processor, this will slow things up considerably, but
the code will stil run; on most processors, alignement errors
will result in a hardware trap, which the system maps into a
core dump or something similar.
but all that the standard has to say about his program.
Therefore, looking for answers elsewhere is a good idea.
No. His problem is pure C++, and I don't know where else he
should look. The fact that he gets the original pointer from a
function which is not in the standard is completely irrelevant
(but the names do help us understand the context, and why he is
trying to do this).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Oct 16 '08 #6
James Kanze wrote:
On Oct 15, 11:14 pm, Kai-Uwe Bux <jkherci...@gmx .netwrote:
>acehr...@gmail .com wrote:
On Oct 15, 4:10 am, Sam <s...@email-scan.comwrote:
Gernot Frisch writes:
the program below workes w/o problems on a GP2X and on
the PC, but my PocketPC (using GCC 3.3.3) crashes. Very
dissapointing, since I expect some speed boost from it.
Thnak you for your help.
int dx=..; // number of pixels width to draw
// one pixel is one unsigned short
// Get some pointer to the screen to draw a rect
unsigned short* pScreen = getColorBuffer( x,y);
>There is no "getColorBuffer ()" function in the standard C++
library. Try asking for help on a newsgroup for your
hardware platform.
How about dx, x, y, etc. Should the poster go elsewhere
because they are not in the standard as well? Do you have
problems with people using foo()? Is that standard?
>You did not specify your platform-specific word sizes.
Why is that needed for the question? If the poster new that
the platform-specific word sizes had anything with it, he
wouldn't be surprised.
>Assuming sizeof(short)=2 , and sizeof(long)=4, the above
math does not add up. Again, has nothing to do with C++,
the language.
Do you know much about the C++ language? Are you claiming
that sizeof applied to types or their comparisons are not
on-topic here?
>I did not read that in that part you quoted.
Your releasing steam is off-topic here.
>Hm, the posting from Sam does not sound very aggressive, as
opposed to yours.

His posting was presumptuous, and just noise, and of no help to
anyone.
>You can get a more definitive answer on a more appropriate
newsgroup.
This is an appropriate newsgroup. Why seek more?
>Because the topical answer to the OP is that the cast
> unsigned long* pScLong = (unsigned long*)pScreen;
>and the following usage of pScLong is undefined behavior. That
answer is clearly not helpful

Isn't it? It's certainly a necessary starting point: the code
was illegal, and if it worked on some platforms, it was just by
accident.
The "accident" is probably what is important.

One might also point out why it was decided that this should be
undefined behavior. General considerations about alignment,
etc., are also on topic here.
That was is Sam's posting.

Presumably, of course, the problem is due to the fact that on
some platforms, long requires more strict alignment than short,
and that his original short* didn't meet those requirements. On
an Intel processor, this will slow things up considerably, but
the code will stil run; on most processors, alignement errors
will result in a hardware trap, which the system maps into a
core dump or something similar.
>but all that the standard has to say about his program.
Therefore, looking for answers elsewhere is a good idea.

No. His problem is pure C++,
I beg to differ. His problem, the way I understand it, is to write code that
works on the target platform. If that involves undefined behavior, so be
it. Why the particular code he proposes breaks is a matter of how the
platform defines the behavior left undefined by the standard. Also, what
possible fixes there are (to improve the speed) is platform specific.
and I don't know where else he should look.
Neither do I. But that does not imply that there is no better place.
The fact that he gets the original pointer from a
function which is not in the standard is completely irrelevant
(but the names do help us understand the context, and why he is
trying to do this).
True. But what the OP is trying to do with that pointer gives us a clue that
alignment is probably at the heart of the matter. What alignment
requirements he actually has, is platform specific.
Best

Kai-Uwe Bux
Oct 16 '08 #7

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

Similar topics

2
1649
by: William Payne | last post by:
Hello! Consider the following code: #include <fstream> #include <iostream> int main() { char name = {"testing"}; unsigned short number = 4711;
15
4869
by: Michael Baehr | last post by:
I recently upgraded my Arch Linux system to GCC 3.4, and found out that a previously accepted behavior (cast-as-lvalue) is now marked as deprecated, and will cease to function in GCC 3.5. This has caused several builds to break, most notably elfutils. Presumably this behavior is not part of the C standard and it is thus being excised like many other nonstandard GCC extensions. Regardless, my question is not about the specifics or...
6
1657
by: JS | last post by:
On this page under "Malloc": http://www.le.ac.uk/cc/tutorials/c/ccccstrc.html I found this: ptr = (*int) malloc(sizeof(int)*N)
10
2578
by: bear | last post by:
hi all, I have a program whose speed is so strange to me. It is maily used to calculate a output image so from four images s0,s1,s2,s3 where so=(s0-s2)^2+ (s1-s3)^2. I compile it with gcc (no optimization). the codec between /***********/ is the initialization code. What supprise me a lot is the code with initialization(io==1) is much faster than without initialization(io!=1). The initialization code should takes some time and it should...
0
1558
by: BobTheHacker | last post by:
I have a c# class library that I call to centralize some code amongst projects. It returns an arraylist back to the calling function that contains class data. Anyway when I call it I can receive it as an object and the data looks fine. However when I do the type casting I get the above error. Any ideas. Below is the code. At the bottom you can see the comment on the cast that blows. I have even tried it without using the Object*. I just...
31
2864
by: gamehack | last post by:
Hi all, I've been testing out a small function and surprisingly it does not work okay. Here's the full code listing: #include "stdlib.h" #include "stdio.h" char* escaped_byte_cstr_ref(char byte); int main (int argc, const char * argv)
6
527
by: Jack | last post by:
Is it possible to cast a 4-byte data type (e.g. an unsigned int) to a 4-byte struct? Sometimes the HIWORD and LOWORD of a 4-byte value contain information independent of each other. Is there a direct way to perform this cast? If not, is there way in C++ to do it? For example, I would like my function test() to work the way it is coded below. It produces a compiler error though: #include <pshpack2.h// 16-bit padding
23
2124
by: gribouille | last post by:
Hi, via fgets() i create a array containing a text file. fp = fopen(argv, "r"); while ((c = fgetc(fp)) != EOF) { line = c; when i want to print it
23
2252
by: mike3 | last post by:
Hi. I seem to have made some progress on finding that bug in my program. I deactivated everything in the bignum package that was used except for the returning of BigFloat objects. I even crippled all the constructors. So now all the operations and constructors that were used do is just return BigFloats but no memory is actually accessed at any point, nor is any allocated. However, when I reenable those parts of the constructor that...
0
8399
marktang
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...
0
8312
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
Oralloy
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...
1
8504
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,...
0
7337
agi2029
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...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4159
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...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.