473,396 Members | 1,748 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,396 software developers and data experts.

Memory and pointers

Consider:

// points to a valid, alloc:ed, aligned memory location
char* ptr_aligned = xxx;

// same but unaligned obviously
char* ptr_unaligned = xxx + 1;

// write short val to aligned_ptr
*(short*)ptr_aligned = val;

// write short val to unaligned_ptr
*(short*)ptr_unaligned = val;

printf("Ok.");
On most platforms this prints Ok. On sparc-solaris this spits out
SIGSEGV and coredumps.

Question: exactly what does the std say about this? UB? Then how does
one implement ie optimized memcpy without knowing alignment
requirements of target hardware?
Thanks,

- NK

Jul 23 '05 #1
3 1478
narechk wrote:

Question: exactly what does the std say about this? UB? Then how does
one implement ie optimized memcpy without knowing alignment
requirements of target hardware?


memcpy is part of the standard library. The library writer knows the
alignment requirements.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2
"narechk" <na*****@gmail.com> wrote in news:1119359913.087806.40840
@g44g2000cwa.googlegroups.com:
Consider:

// points to a valid, alloc:ed, aligned memory location
char* ptr_aligned = xxx;

// same but unaligned obviously
char* ptr_unaligned = xxx + 1;
Nope, not obviously unaligned. May be running on a processor where 1
byte alignment is required.
// write short val to aligned_ptr
*(short*)ptr_aligned = val;

// write short val to unaligned_ptr
*(short*)ptr_unaligned = val;

printf("Ok.");
On most platforms this prints Ok. On sparc-solaris this spits out
SIGSEGV and coredumps.

Question: exactly what does the std say about this? UB? Then how does
one implement ie optimized memcpy without knowing alignment
requirements of target hardware?


Undefined behaviour. Happens that on the sparc, the CPU will trap an
unaligned memory access (which blows up your program). It's just not
required.

As for "optimizing memcpy".... I didn't think there's much you could do
to optimize memcpy. It copies a sequence of bytes from one memory
location to another. This implementation could range from effectively a
loop which copies byte-by-byte (which is probably the only "portable"
implementation), to some sort of implementation where you load some
registers with a start, end, and length value and make 1 CPU instruction
to perform the copy. But of course that would mean that you knew that
your target CPU supported that kind of instruction...
Jul 23 '05 #3


Andre Kostur wrote:
"narechk" <na*****@gmail.com> wrote in news:1119359913.087806.40840
@g44g2000cwa.googlegroups.com:
Consider:

// points to a valid, alloc:ed, aligned memory location
char* ptr_aligned = xxx;

// same but unaligned obviously
char* ptr_unaligned = xxx + 1;
Nope, not obviously unaligned. May be running on a processor where 1
byte alignment is required.


Right. Even/uneven/aligned/unaligned... I was kind of hoping my meaning
was getting through.
// write short val to aligned_ptr
*(short*)ptr_aligned = val;

// write short val to unaligned_ptr
*(short*)ptr_unaligned = val;

printf("Ok.");
On most platforms this prints Ok. On sparc-solaris this spits out
SIGSEGV and coredumps.

Question: exactly what does the std say about this? UB? Then how does
one implement ie optimized memcpy without knowing alignment
requirements of target hardware?
Undefined behaviour. Happens that on the sparc, the CPU will trap an
unaligned memory access (which blows up your program). It's just not
required.


Ok. Yes, there was no doubt as to why it coredumped, and the code was
modified to use byte memory accesses, but I was quite surprised to have
run into this. I've seen quite a few lines of code in the past which do
some really hairy pointer-memory-pointer stuff. In my case this is a
pool-like memory manager.

I take it this basically means that accessing memory in arbitrary
manner leads to UB (or is it UD?), at least as far as the standard is
concerned? Oh the joy of portable programming!


As for "optimizing memcpy".... I didn't think there's much you could do
to optimize memcpy. It copies a sequence of bytes from one memory
location to another. This implementation could range from effectively a
loop which copies byte-by-byte (which is probably the only "portable"
implementation), to some sort of implementation where you load some
registers with a start, end, and length value and make 1 CPU instruction
to perform the copy. But of course that would mean that you knew that
your target CPU supported that kind of instruction...


I'll rather eat my shorts than attempt to optimize memcpy and the likes
in ISO C++.
Many thanks,

- NK

Jul 23 '05 #4

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

Similar topics

4
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online...
11
by: Michael B. Allen | last post by:
Coming from C and Java on *nix I'm a little out of my element messing around with CList and MSVC++ but I think my issues are largely syntactic. I have an ADT that I use called a 'varray' that can...
32
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes...
2
by: mosfets | last post by:
Hi, I'm having a little trouble figuring out the difference in terms of memory allocation between: class person_info; class A { private:
11
by: Eric A. Johnson | last post by:
Hi, The book I am studying from, which comes from 1994, makes mention of far memory (i.e., that memory beyond 64K). I am under the belief, however, that far memory no longer needs any special...
8
by: nkrisraj | last post by:
Hi, I have a following structure: typedef struct { RateData rdr; int RateID; char RateBalance; } RateInfo;
62
by: ivan.leben | last post by:
How can I really delete a preloaded image from memory/disk cache? Let's say I preload an image by creating an Image object and setting its src attribute to desired URL: var img = new Image();...
10
by: goose | last post by:
Hello all I've written a wrapper for malloc and friends. Its available from http://www.lelanthran.com/downloads/os_mem/index.php The reason for doing writing this so that newbies can...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
6
by: CANCER.0707 | last post by:
The problem statement is as follows Create a library that creates pools of memory of different sizes.For e.g. one pool of 32 byte size, another pool of 64 byte size and so on.Create an array of...
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
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,...
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
tracyyun
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...
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,...

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.