473,405 Members | 2,160 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,405 software developers and data experts.

Pointer Converts to Direct Memory

We discussed about the union keyword on the previous threads 2-3 days
earlier. I want to add. Two byte data inside struct and one word data are
inside union. You modify word data. Then two byte data are modified
automatically because two byte data and one word data are sharing the same
memory address. Let's move on and forget union because we claim that union
keyword is not the language of C/C++ Compiler.
Here is a pointer variable code below.

enum
{
Low = 0,
High = 1
};

U_BYTE B[4] = { 0xFF, 0x20, 0x02, 0x22 };
PU_WORD W = (PU_WORD)&B;
PU_DWORD DW = (PU_DWORD)&B;

B[Low] += 0x0A;
B[High] += 0x01;
W[Low] += 0x0100;
*DW = 0x41424344; // *DW and DW[0] are same.
DW[0] = 0x51525354;

I decide to define four elements in an array of B variable. I define W
as the pointer to B variable. This allows to modify individual two byte
data. Then W variable is modified automatically from B variable. W
variable is modified causing two byte data to be modified automatically. DW
variable does the same to modify 4 byte data. Sometimes, I want to modify
individual byte data and word data, but I depend on word data often. I load
two byte data from big array in memory and they are stored in B[L] and B[H].
I use addition to put two byte data together into word data so I don't need
to use addition on both byte data. It depends according to my decision and
design of my source code.
You may be surprised to see direct variable and pointer variable. You
set debug mode. You go step by step looking at each variable in watch
window. You go into disassemble window like x86 machine and non-x86
machine. You can see MOV instruction on direct variable. Also, you can see
LEA instruction and MOV instruction on pointer variable.
You switch from debug mode to release mode. You look at disassemble
window again. Do you see disappearing LEA instruction? W variable is
converted to direct variable from pointer variable. Do you know why? The
C/C++ Compiler uses optimization and it decides that two variables share the
same memory address so it does not need LEA instruction. It reduces time
and improve performance.
I choose word data instead of dword data, but you claim that word data
is slow than dword data. What happen if word data has 0xFFFF? It is added
by 0x0004. The result shows 0x0001-0003. I do not want data on 31th
through 16th bits. 32th through 16th bits can always be ignored. Word data
can be used. It is pain to use "AND" on dword data if you want to mask 16
bit data or 8 bit data.
Sometimes, C/C++ Compiler ignores "AND" like dword dw &= 0xFFFF and it
uses MOV instruction to store 16 bit data or 8 bit data into memory address
directly.
Please let me know what you think. It can be interesting discussion. I
may do not need to post more so I can continue writing my C++ source code
according to my design so two variables can share the same memory address.

--

Yours Truly,
Bryan Parkoff
Dec 14 '07 #1
4 1797
Bryan Parkoff wrote:
U_BYTE B[4] = { 0xFF, 0x20, 0x02, 0x22 };
PU_WORD W = (PU_WORD)&B;
PU_DWORD DW = (PU_DWORD)&B;

B[Low] += 0x0A;
B[High] += 0x01;
W[Low] += 0x0100;
*DW = 0x41424344; // *DW and DW[0] are same.
DW[0] = 0x51525354;
This is not portable code. The contents of B, when read and modified
using W and DW, will depend on the endianess of the system. In a
low-endian system (such as an Intel PC) the result will be different
than with a high-endian system (such as an UltraSparc).
Dec 14 '07 #2
James Kanze wrote:
And *W will probably core dump from time to time, because B
won't be correctly aligned.
Is there any compiler in any architecture which creates char arrays at
non-aligned memory locations (by double-word or even larger alignment)?

The major problem with his code is that it's not portable between
architectures of different endianess.
Dec 14 '07 #3
REH
On Dec 14, 8:31 am, Juha Nieminen <nos...@thanks.invalidwrote:
Is there any compiler in any architecture which creates char arrays at
non-aligned memory locations (by double-word or even larger alignment)?
Yes, I work with several compilers will not align character arrays.
Unfortunately, I also work with people like the OP that are constantly
getting bit by this.

REH
Dec 14 '07 #4
Juha Nieminen wrote:
James Kanze wrote:
And *W will probably core dump from time to time, because B
won't be correctly aligned.
Is there any compiler in any architecture which creates char arrays at
non-aligned memory locations (by double-word or even larger alignment)?
Are there any that don't? Neither Sun CC nor g++ guarantee that
char arrays are aligned for more than char, and I'd be surprised
that any other compiler does as well.
The major problem with his code is that it's not portable between
architectures of different endianess.
Amongst other things. There are, in fact, several major
problems. The biggest one is that we don't know what the code
is trying to do, so we can't really analyse it for more
problems. The fact remains that it core dumps on a Sun Sparc,
at least if his types correspond the what I think they do.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 14 '07 #5

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

Similar topics

16
by: junky_fellow | last post by:
According to Section A6.6 Pointers and Integers (k & R) " A pointer to one type may be converted to a pointer to another type. The resulting pointer may cause addressing exceptions if the...
23
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when...
8
by: nsharma78 | last post by:
Hi, I have a code as follows: class A { public: void print(){cout << "Magic" << endl;} };
1
by: Tomás | last post by:
Some programmers treat arrays just like pointers (and some even think that they're exactly equivalent). I'm going to demonstrate the differences. Firstly, let's assume that we're working on a...
11
by: Brian Gladman | last post by:
A lot of low level cryptographic code and some hardware cryptographic accelerators either fail completely or perform very poorly if their input, output and/or key storage areas in memory are not...
15
by: khan | last post by:
Hi, I read that pointer representation can non-zero bit pattern, machine specific.Compiler when comes accross value '0' in pointer context, converts it to machine specific null pointer...
5
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
4
by: ctx2002 | last post by:
hi guys: I am reading Sqlite code at moment, my c language skill not good enough , hope some one here can help me. In function listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg);...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.