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

Home Posts Topics Members FAQ

What is const char * const *p;

Hello,

I have few questions. They are:

1. Is "const char * const *p;" a valid construct?
2. How do I align a given structure, say, at 32-byte boundary?
3. Then, how do I assert that a given object is aligned, say,
at 32-byte boundary?
Regards,

--
Vijay Kumar R. Zanvar
Home Page - http://geocities.com/vijoeyz/
Nov 14 '05 #1
13 19349
Vijay Kumar R. Zanvar wrote:


I have few questions.

1. Is "const char* const *p;" a valid construct?
Yes. Both p and the char to which p ponts are const.
2. How do I align a given structure, say, at 32-byte boundary?
char* q = (char*)malloc(6 4);
char* p = (q + 31)&(~31);

p is aligned on a quad word boundary.

3. Then, how do I assert that a given object is aligned, say,
at 32-byte boundary?


assert(p == ((p + 31)&(~31)));
Nov 14 '05 #2
vi*****@gmail.c om (Vijay Kumar R. Zanvar) wrote:
1. Is "const char * const *p;" a valid construct?
Yes.
2. How do I align a given structure, say, at 32-byte boundary?
3. Then, how do I assert that a given object is aligned, say,
at 32-byte boundary?


You don't, and you can be reasonably but not perfectly sure,
respectively.

That is, if you happen to _know_ that a certain C type must be aligned
on 32-byte boundaries, then you also know that both all actual objects
of that type are indeed so aligned, and that so is the memory you get
from malloc() (because that must be correctly aligned for any type,
including your 32-byte one).
The problem is finding a type like that, because nothing in the Standard
demands anything about the alignment of any type except that several
kinds of types must have the same alignment as closely related types
(e.g., signed and unsigned int must be identically aligned; void * has
the same requirements as char *; etc.) and that malloc() returns an
all-suitable pointer. There is nothing that either demands that a
certain type is aligned to certain boundaries, or that allows you to
find out which alignment requirements a type has.
Oh, one exception: because array members are contiguous, all types can
have no stricter alignment requirements than to sizeof(type). But since
they could also be aligned more loosely (say, to half of sizeof(type)),
this doesn't actually help you any if you really need an alignment of a
particular strictness. In particular, any object of type struct align {
char align[32]; } is not guaranteed to be aligned on a 32-byte boundary,
or indeed any boundary at all.

As for asserting the alignment of an existing object, you can try to
check that ((intptr_t)&obj ect)%32 (or if you use C89, ((unsigned
long)&object)%3 2) is zero. This is not strictly required to work, since
conversion from pointers to integers is implementation-defined, but it
would not be unreasonable to assume that on all modern implementations ,
this does in fact work as expected - if only because the implementor
would have to go out of his way to make it fail. So expect it to fail
quite spectacularly on the DS9K <g>, but not on any common system.

Richard
Nov 14 '05 #3
"E. Robert Tisdale" <E.************ **@jpl.nasa.gov > wrote in message
news:co******** **@nntp1.jpl.na sa.gov...
Vijay Kumar R. Zanvar wrote a bit hastily:


I have few questions.

1. Is "const char* const *p;" a valid construct?


Yes. Both p and the char to which p ponts are const.


No! p points to a constant pointer to constant chars. You can change p, but
you cannot change the pointer it points to, not the characters this pointer
points to.
2. How do I align a given structure, say, at 32-byte boundary?


char* q = (char*)malloc(6 4);
char* p = (q + 31)&(~31);

p is aligned on a quad word boundary.


quad word ? what is the size of a word ? are you mixing bits and bytes ?
The above code will not compile on a decently configured compiler because it
does implicit conversions between integers and pointers. you can use this
instead:

unsigned char *q = malloc(32 + 31);
void *p = (void*)(((unsig ned long)q + 31) & ~31);

But it not portable code either. On a C99 conformant environment, you may use
intptr_t instead of unsigned long to allow for pointers with a different size
than long, but the arithmetic trick played is not guaranteed to have the
required effect on pointer alignment on all architectures.
3. Then, how do I assert that a given object is aligned, say,
at 32-byte boundary?


assert(p == ((p + 31)&(~31)));


Same remark : implicit conversions between pointer and integer.
With the restrictions expressed above, use this:

assert(((unsign ed long)p & 31) == 0);

--
Chqrlie.
Nov 14 '05 #4
On Mon, 29 Nov 2004 21:54:38 -0800, E. Robert Tisdale
<E.************ **@jpl.nasa.gov > wrote:
Vijay Kumar R. Zanvar wrote:
2. How do I align a given structure, say, at 32-byte boundary?


char* q = (char*)malloc(6 4);
char* p = (q + 31)&(~31);

p is aligned on a quad word boundary.


That is undefined behaviour in standard C:

6.5.10 Bitwise AND operator

Constraints
2 Each of the operands shall have integer type

How do you know that the (int) value (~31) is wide enough to bitwise and
with a pointer to give meaningful results?

In C99 you could write:

#include <stdint.h>

...

void *q = malloc(64);
intptr_t pq = ((intptr_t)q + 31) & (~31);
char *p = (char*)pq;

However, although the standard guarantees that an intptr_t is a "signed
integer type with the property that any valid pointer to void can be
converted to this type, then converted back to pointer to void, and the
result will compare equal to the original pointer", it does not
guarantee that the result after doing arbitrary arithmetic operations on
the type will still convert back to a valid pointer. For instance, a
pointer to character could be defined on a hypothetical machine as:
+-+-+-+-+- ... -+-+
| b | word-ptr |
+-+-+-+-+- ... -+-+

Where 'word-ptr' is the pointer to a 32 bit word and 'b' an 8-bit byte
reference within the word. Pointer arithmetic on T* would manipulate it
appropriately (if sizeof(T) were a multiple of 4 bytes then it would be
simple, otherwise the compiler would have to shift bits around), but
casting to an intptr_t and anding with ~31 would produce a very wrong
result.

Unlikely, certainly, but nothing in the standard forbids it.
3. Then, how do I assert that a given object is aligned, say,
at 32-byte boundary?


assert(p == ((p + 31)&(~31)));


The same applies.

In my own code I have assumed that malloc() is standard-compliant and
returns an area of memory which is "suitably aligned so that it may be
assigned to a pointer to any type of object and then used to access such
an object or an array of such objects in the space allocated" (section
7.20.3), and then have made sure that if I subdivide it I do so in sizes
also aligned to the worst case.

Note that since arrays of objects are contiguous, any object need only
be aligned to its own size (or rather the size of the biggest builtin
type in the object if it is an aggregate) as a worst case. Unless you
are doing some system-dependant thing which needs a special alignment
(for instance using hardware-accessed buffers).

Chris C
Nov 14 '05 #5
In <sl************ ******@ccserver .keris.net> Chris Croughton <ch***@keristor .net> writes:
On Mon, 29 Nov 2004 21:54:38 -0800, E. Robert Tisdale
<E.************ **@jpl.nasa.gov > wrote:
Vijay Kumar R. Zanvar wrote:
2. How do I align a given structure, say, at 32-byte boundary?


char* q = (char*)malloc(6 4);
char* p = (q + 31)&(~31);

p is aligned on a quad word boundary.


That is undefined behaviour in standard C:

6.5.10 Bitwise AND operator

Constraints
2 Each of the operands shall have integer type


A constraint violation is not undefined behaviour. Basically, it means
that this is not standard C code and the compiler must diagnose it,
while undefined behaviour need not be diagnosed.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #6
"E. Robert Tisdale" wrote:
Vijay Kumar R. Zanvar wrote:
.... snip ...
3. Then, how do I assert that a given object is aligned, say,
at 32-byte boundary?


assert(p == ((p + 31)&(~31)));


Undefined behaviour. This is an operation that can only be
performed outside the C language, and is non-portable.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #7
In <42************ *************@p osting.google.c om> vi*****@gmail.c om (Vijay Kumar R. Zanvar) writes:
1. Is "const char * const *p;" a valid construct?
Yes, of course.
2. How do I align a given structure, say, at 32-byte boundary?
You cannot do that portably in C.
3. Then, how do I assert that a given object is aligned, say,
at 32-byte boundary?


Ditto.

The best you can do in C is converting pointers to (unsigned) integers,
adjust the integers so they become multiples of the desired alignment and
then convert them back to pointers. Alignment checking is performed the
same way.

Here's some sample C89 code that may or may not work (it works on all
mainstream implementations ):

struct struct32 { ... } *p;
char buff[sizeof(struct struct32) + 31];
unsigned long addr = (unsigned long)buff, tmp;

p = (struct struct32 *) (addr + ((tmp = addr % 32) ? 32 - tmp : 0));
if ((unsigned long)p % 32 == 0) /* p is aligned */ ;

When the desired alignment is a power of 2, the % operator can be replaced
by bitwise operations, but the code becomes less readable.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #8
In <41************ ***@yahoo.com> CBFalconer <cb********@yah oo.com> writes:
"E. Robert Tisdale" wrote:
Vijay Kumar R. Zanvar wrote:
... snip ...
3. Then, how do I assert that a given object is aligned, say,
at 32-byte boundary?


assert(p == ((p + 31)&(~31)));


Undefined behaviour.


Can I have the chapter and verse? I always thought it was a constraint
violation:

6.5.10 Bitwise AND operator

Syntax

1 AND-expression:
equality-expression
AND-expression & equality-expression

Constraints

2 Each of the operands shall have integer type.
This is an operation that can only be performed outside the C language,
Wrong again: it can be performed without violating the C language
specification, by converting the pointer to a suitable integer type.
and is non-portable.


This is correct, in theory, at least.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Nov 14 '05 #9
Dan Pop wrote:
CBFalconer <cb********@yah oo.com> writes:
"E. Robert Tisdale" wrote:
Vijay Kumar R. Zanvar wrote:

... snip ...

3. Then, how do I assert that a given object is aligned, say,
at 32-byte boundary?

assert(p == ((p + 31)&(~31)));


Undefined behaviour.


Can I have the chapter and verse? I always thought it was a
constraint violation:

6.5.10 Bitwise AND operator

Syntax

1 AND-expression:
equality-expression
AND-expression & equality-expression

Constraints

2 Each of the operands shall have integer type.
This is an operation that can only be performed outside the C language,


Wrong again: it can be performed without violating the C language
specification, by converting the pointer to a suitable integer type.
and is non-portable.


This is correct, in theory, at least.


Thanks for the corrections. However the theme of my reply "Tisdale
is in serious error again" still holds, together with "don't do
that". Even if the conversions work, there is no guarantee that
any such results indicate anything whatsoever about alignment.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!

Nov 14 '05 #10

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

Similar topics

1
3908
by: electric sheep | last post by:
Hi, can somebody explain the following syntax to me. This is straight from a gnu info file: int main(void) { /* Hashed form of "GNU libc manual". */ const char *const pass = "$1$/iSaq7rB$EoUw5jJPPvAPECNaaWzMK/"; I think the idea at play here is whether the pointer is constant or not ?
2
1574
by: Pinnacle | last post by:
Can anyone explain what is "const char* const*" mean??
2
6481
by: s88 | last post by:
Hi all: I saw the code likes... 7 #include <stdio.h> 8 int main(void){ 9 const char *const green = "\033[0;40;32m"; 10 const char *const normal = "\033[0m"; 11 printf("%sHello World%s\n", green, normal); 12 return 0; 13 }
24
2320
by: kevin.hall | last post by:
Is char** (or char*) implicitly convertible to 'const char * const *'? I couldn't find anything about it in the standard. MSVS 8.0 allows this. I'm curious if I'll run into trouble with other compilers like GCC though. Many thanks! - Kevin
2
2022
by: itsolution | last post by:
Hi Guys, When a function has following arg type, update_data(const char *const * update_list,...) exactly what object type update_list can take? For example, I can call update_aaa() using following variale as an arg:
3
2376
by: Ferdinend | last post by:
In the below mentioned code, my intension is only to use the values which are received as a arguments. But unfortunately, I am able to change the “cStringVal”. How to avoid this? void setValue(const int * const intVal, const char * const cStringVal) { int length=0; //*intVal = 50; //Error Can't change the value
0
8402
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
8315
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
8829
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
8508
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
8608
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
6172
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
5633
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
4164
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...
2
1627
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.