473,769 Members | 2,140 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Simple allocation of two buffer on AMD64

- Code compiled by GCC:

int main(void)
{
char a[] = "123";
char b[] = "abc";

printf("%p %p\n", a, b);

return 0;
}

- Running it on AMD64 gives me:

0x7fff929c9410 0x7fff929c9400

- What is the reason for 16 bytes of distance between two address and
not just 8 bytes?

Nov 1 '07 #1
43 1893
ji*********@gma il.com wrote:
- Code compiled by GCC:

int main(void)
{
char a[] = "123";
char b[] = "abc";

printf("%p %p\n", a, b);

return 0;
}

- Running it on AMD64 gives me:

0x7fff929c9410 0x7fff929c9400

- What is the reason for 16 bytes of distance between two address and
not just 8 bytes?
Ask the gcc implementors. It was they who decided how
the compiler should allocate space, not the C language itself.
C does not require a sixteen-byte separation, but does not
forbid it.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Nov 1 '07 #2
ji*********@gma il.com wrote:
- Code compiled by GCC:

int main(void)
{
char a[] = "123";
char b[] = "abc";

printf("%p %p\n", a, b);
Cast 'a' and 'b' to void *.
>
return 0;
}

- Running it on AMD64 gives me:

0x7fff929c9410 0x7fff929c9400

- What is the reason for 16 bytes of distance between two address and
not just 8 bytes?
It's an implementation detail that Standard C says nothing about. It may
be due to alignment reasons.

Nov 1 '07 #3
It's an implementation detail that Standard C says nothing about. It may
be due to alignment reasons.
I know. Just curiosity about the election of 16 bytes over 8 (what I
expected).

Thanks anyway.

Nov 1 '07 #4
On Nov 1, 7:54 am, santosh <santosh....@gm ail.comwrote:
jimenezr...@gma il.com wrote:
char a[] = "123";
char b[] = "abc";
printf("%p %p\n", a, b);

Cast 'a' and 'b' to void *.
Correct me if I'm wrong, but isn't this technically a case where it
shouldn't matter, seeing as char * and void * have the exact same
representation?

Nov 1 '07 #5
Cast 'a' and 'b' to void *.

Same result.
Correct me if I'm wrong, but isn't this technically a case where it
shouldn't matter, seeing as char * and void * have the exact same
representation?
Yes, in this case is similar.

Nov 1 '07 #6
In article <11************ **********@o3g2 000hsb.googlegr oups.com>,
Justin Spahr-Summers <Ju************ *****@gmail.com wrote:
char a[] = "123";
char b[] = "abc";
printf("%p %p\n", a, b);
>Cast 'a' and 'b' to void *.
>Correct me if I'm wrong, but isn't this technically a case where it
shouldn't matter, seeing as char * and void * have the exact same
representation ?
We discussed this a few weeks ago. The idea seems to be that although
they have the same representation, they aren't necessarily passed to
variadic functions in the same way.

-- Richard

--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Nov 1 '07 #7
ji*********@gma il.com wrote:
>
- Code compiled by GCC:

int main(void) {
char a[] = "123";
char b[] = "abc";

printf("%p %p\n", a, b);
return 0;
}

- Running it on AMD64 gives me:

0x7fff929c9410 0x7fff929c9400

- What is the reason for 16 bytes of distance between two address
and not just 8 bytes?
Many systems access memory in 128 bit chunks. Aligning objects to
this value prevents having to do two memory accesses, when one
suffices. Check the gcc docs for means of avoiding this if
desired.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Nov 1 '07 #8
ji*********@gma il.com wrote:
- Code compiled by GCC:

int main(void)
{
char a[] = "123";
char b[] = "abc";

printf("%p %p\n", a, b);

return 0;
}

- Running it on AMD64 gives me:

0x7fff929c9410 0x7fff929c9400

- What is the reason for 16 bytes of distance between two address and
not just 8 bytes?
That is, of course, an issue between your hardware, your OS, and the
version of gcc you are using. There is no reason, as far as C is
concerned, for the addresses of two distinct objects to have any
particular relationship. But your post is extremely amusing. The fact
that you think it somehow appropriate for the difference to be eight
when sizeof a and sizeof b are each four is hilarious.

You really should include <stdio.hwhen using the variadic function printf.
Even with the special relationship between the form of (void *) and
(char *), you should cast pointers to (void *) when using the "%p"
specifier, since that is what it signals.
Nov 1 '07 #9
ji*********@gma il.com wrote:
- Code compiled by GCC:

int main(void)
{
char a[] = "123";
char b[] = "abc";

printf("%p %p\n", a, b);

return 0;
}

- Running it on AMD64 gives me:

0x7fff929c9410 0x7fff929c9400

- What is the reason for 16 bytes of distance between two address and
not just 8 bytes?
The stack MUST be aligned to an 8 byte boundary in
64 bits AMD/Intel. If that fails, each access provokes
a misaligned read, what makes performance take a
big hit.

Access to XMM registers in most cases MUST be aligned to a
16 byte boundary. If not, you can't load the 16 byte
XMM registers quickly.

Some compilers decide that performance is better than
wasting memory in the alignment, and align all variables
in the stack to a 16 byte boundary even when it is not
needed, as in your example.

Other compilers will say that performance by loading the XMM
registers is lost when the cache hits go down because the
program uses more memory and will NOT align to a 16 byte
boundary but only to a 8 byte boundary what is bad enough.

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Nov 1 '07 #10

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

Similar topics

8
1572
by: jason | last post by:
i'm a little new to VC++, so i'm curious how to appropriate perform the following task. there is a pointer which i wish you point to a buffer of frequently changing size. i'm wondering what is the proper way to initially allocate, and then grow that allocation? some sample code that might illustrate my intentions: DWORD CMyClass::MyEvent(void *ptr, char *stufftoappend)
51
8292
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct code? many thx!
8
5114
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
10
4424
by: AlabiChin | last post by:
Hello, I noticed that when I dynamically create an array of chars, the resulting size of the allocated memory block is larger by about 15 bytes than what I specified. Here is example code: char *createTextBuffer(char *buffer, int length) { buffer = new char;
5
4171
by: collection60 | last post by:
Hi people, I am writing a library that will implement a binary file format. Well... I'm thinking, as a design, it would be nice to have my library able to promise to not use more than a certain amount of RAM. I could have a "SetCacheSize(long NewSize);" function. So as part of that promise, when required memory exceeds that limit, the extra data is saved to disk and flushed from RAM.
66
3642
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if (function(socket, args) == -1) { perror("function"); exit(EXIT_FAILURE); } I feel that the ifs destroy the readability of my code. Would it be
0
1309
by: He Shiming | last post by:
Hi All, In my COM (component object model) server DLL I created, there are these methods that require buffer allocation. They look like this: class CComClass : public IClass { // just for demonstration public: HRESULT Method (long lParam1, long lParam2, unsigned char* pBufferOut, long * plSizeOut); };
10
2310
by: kid joe | last post by:
Hi all, I'm using a temporary buffer to transfer some data and would rather not allocate it on the heap. The problem is that the size of the buffer is only known upon entry into the function that utilizes it and I'd rather not waste space or dynamically allocate on the heap (since it doesn't need to persist). Now I'm planning on utilizing inline assembly to modify the stack pointer directly to allocate the space I need.
4
2197
by: Jung, William | last post by:
I have a function that convert a string to binary, where - string is the string needs to convert to binary. - binary is the BYTE array to hold the converted data StringtoBinary( LPCSTR string, BYTE *binary) Since BYTE is a pointer to BYTE, do I need to use new operator to allocate space / storage (dynmaic allocation)?
0
9579
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
10208
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...
0
10038
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 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...
0
9857
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 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...
1
7404
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5294
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
5444
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3952
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2812
bsmnconsultancy
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...

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.