LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-01-2013, 06:47 AM   #1
dayalan_cse
Member
 
Registered: Oct 2006
Posts: 132

Rep: Reputation: 15
const void* question


memcmp (const void *a1, const void *a2, size_t size)

the above function - memcmp receiving a1 as pointer to void which is 'const' this means content of the a1 can not be changed.

Question:

const void *a1;

We can not dereference a1 this must be compiler error because its pointing to an address which is void then why memcmp should receive a1 as "const void*" instead of "void *".
 
Old 04-01-2013, 07:13 AM   #2
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
You cannot deference a void pointer, the results are undefined (compiler may complain or program will seg fault) and so it is not done.
 
Old 04-01-2013, 07:36 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by dayalan_cse View Post
We can not dereference a1 this must be compiler error because its pointing to an address which is void then why memcmp should receive a1 as "const void*" instead of "void *".
A reasonable implementation of memcmp must effectively cast the pointer to const char* before using it.

A reasonable caller of memcmp might have cast the pointer from const XXX* for some type XXX before passing the pointer to memcmp.

If memcmp took the pointer as void* then the caller would be casting from const XXX* to void*, which would appear to be a bug.
 
Old 04-01-2013, 09:49 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
What is your real question? Eg:
Q: What 'const' means in 'const void *a1'?
A: memcmp won't change the memory pointed by a1
Q: What 'void *' means?
A: Generic pointer.
 
Old 04-01-2013, 12:48 PM   #5
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by dayalan_cse View Post
why memcmp should receive a1 as "const void*" instead of "void *".
Because it does not modify contents of the memory pointed by a1.
 
Old 04-01-2013, 09:37 PM   #6
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by mina86 View Post
Because it does not modify contents of the memory pointed by a1.
That isn't what it means at all.

It means that the POINTER cannot be modified, not what it points to.
 
Old 04-01-2013, 10:34 PM   #7
dayalan_cse
Member
 
Registered: Oct 2006
Posts: 132

Original Poster
Rep: Reputation: 15
const void* question

Quote:
Originally Posted by johnsfine View Post
A reasonable implementation of memcmp must effectively cast the pointer to const char* before using it.

A reasonable caller of memcmp might have cast the pointer from const XXX* for some type XXX before passing the pointer to memcmp.

If memcmp took the pointer as void* then the caller would be casting from const XXX* to void*, which would appear to be a bug.


Yes, const void *a1 - can be cast to another pointer internal implementation of memcmp function.

My question was, why memcmp or for that reason any C function which receives void *arg as argument if i add 'const' qualifier - what is the use.
 
Old 04-01-2013, 11:16 PM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
The use of const is to indicate a value that is not to be changed by that function.

I believe it is only a documentation aid. It makes sure that the programmer knows the parameters are not modified.
 
Old 04-01-2013, 11:28 PM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> > const void *a

> It means that the POINTER cannot be modified, not what it points to.

Wrong. Actually, it means a pointer pointing to constant data. Please read the forgotten manual, or perform tests.
 
Old 04-02-2013, 02:01 AM   #10
Dr_P_Ross
Member
 
Registered: Nov 2003
Location: Edinburgh, UK
Distribution: Arch
Posts: 43

Rep: Reputation: 18
>> My question was, why memcmp or for that reason any
>> C function which receives void *arg as argument if
>> i add 'const' qualifier - what is the use.

> I believe it is only a documentation aid. It makes
> sure that the programmer knows the parameters are
> not modified.

It is not 'only' a documentation aid. As already mentioned, const void *a1 means
a1 is a pointer to constant data. The compiler can use this information to do useful things such as being clever about optimising the code. And if you provided your own definition of memcmp which did modify a1, then the compiler could tell you that you had made a mistake. It's much better to be told about such mistakes at compile time than to have to try to find them when the program misbehaves at run time.

For comparison, observe that memcpy is declared as memcp(void *dest, const void *src, size_t n) because the copy process does modify the contents pointed at by dest pointer.
 
Old 04-02-2013, 02:39 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
what was not mentioned, actually memcmp does not take care about the meaning of the content. it has [got] two memory addresses, and compares the content byte by byte (compares size number of bytes). So you can say, the type of the data is irrelevant, and void * means exactly that: a pointer to something, whatever, without knowing/handling its type, structure. const means the function will not change the content of that data.
 
Old 04-02-2013, 03:00 AM   #12
psionl0
Member
 
Registered: Jan 2011
Distribution: slackware_64 14.1
Posts: 722
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
Quote:
Originally Posted by NevemTeve View Post
> > const void *a

> It means that the POINTER cannot be modified, not what it points to.

Wrong. Actually, it means a pointer pointing to constant data. Please read the forgotten manual, or perform tests.
From reading http://www.lix.polytechnique.fr/ I deduce the following:
Code:
  void * const Var;           /* The pointer is constant, 
                               * the data its self can change. */
  const void * Var;           /* The data is constant,
                               * the pointer can change       */
  const void * const Var;
         OR
  void const * const Var;     /* the pointer and data are both constant */
As for the keyword void, that would indicate that the function makes no assumption about the type of data the pointer is referencing. The function would make an appropriate internal cast to allow byte by byte comparisons. (For speed purposes it might actually do a 32-bit word by 32-bit word comparison then compare the 4 bytes in the word that doesn't match).
 
1 members found this post helpful.
Old 04-02-2013, 06:06 AM   #13
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
That's interesting, I didn't know the order mattered, but then I never use these.
 
Old 04-02-2013, 06:13 AM   #14
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
thanks for correcting me.

I was wrong.
 
1 members found this post helpful.
Old 04-02-2013, 07:06 AM   #15
geox
Member
 
Registered: Jan 2012
Posts: 42

Rep: Reputation: 2
I always use the following hint from Wikipedia on how C/C++ handle the const keyword:

Quote:
To render the syntax for pointers more comprehensible, a rule of thumb is to read the declaration from right to left. Thus, everything to the left of the star can be identified as the pointee type and everything to the right of the star are the pointer properties. (For instance, in our example above, int const * can be read as a mutable pointer that refers to a non-mutable integer, and int * const can be read as a non-mutable pointer that refers to a mutable integer.)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
(void)time(&cur_time); Why did the author put (void) here? e3399 Programming 3 03-28-2011 11:45 AM
Convert const wchar_t* to const char* to use in fprintf() coders123 Programming 1 01-17-2011 03:47 PM
const char * question HarryBoy Programming 6 02-02-2010 07:20 AM
void main(void) linuxanswer Programming 4 10-26-2003 12:37 AM
void foo(void) and void foo() lackluster Programming 9 02-15-2003 10:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration