LinuxQuestions.org
Review your favorite Linux distribution.
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 03-25-2010, 06:11 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 531

Rep: Reputation: 42
converting c code to c++ causes an type error


Hi all,
hope someone can help me out with this. I'm a C / assembler programmer so am not use to C++, which I need to use. Basically I'm using my own versions of memcpy, but for long and int rather than char. It's for fast graphics. So I have a char array which I copy to another array. But I copy as either longs or ints, much faster. for example
Code:
void * fastmemcpysprite (void *destaddr, void const *srcaddr, size_t len)
{

  register int *dest = destaddr;
  register int const *src = srcaddr;

  len /= 4;
  while (len-- > 0)  *dest++ = *src++;
  return destaddr;
}
but under C++ the int *dest *src says they should be void const void.
I tried changing my voids but of course this the causes errors as I'm passing an array of chars. I've Google but the couple of c++ examples seem to do it in exactly the same way I do :S

Can anyone point me in the right direction.
 
Old 03-25-2010, 06:25 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by knobby67 View Post
but under C++ the int *dest *src says they should be void const void.
In C++, you can't (shouldn't be able to) cast from void* implicitly - you just need to make it explicit:

Code:
  register int *dest = (int*) destaddr;
  register int const *src = (int const *)srcaddr;
John G

P.S. Or, even better, use "static_cast<int*>(destaddr)" instead of "(int*)destaddr", which'll just make the cast more visible and easier to search for.

Last edited by JohnGraham; 03-25-2010 at 06:35 AM.
 
Old 03-25-2010, 07:02 AM   #3
knobby67
Member
 
Registered: Mar 2006
Posts: 531

Original Poster
Rep: Reputation: 42
Thanks that solves that problem, but then a whole host of others open up.
I might be gone some time
 
Old 03-25-2010, 08:02 AM   #4
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,057

Rep: Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104Reputation: 1104
Quote:
Originally Posted by JohnGraham View Post
even better, use "static_cast<int*>(destaddr)" instead of "(int*)destaddr", which'll just make the cast more visible and easier to search for.
I think reinterpret_cast<int*>(destaddr) is a better fit for the meaning that cast.

A static_cast implies there may be some change to the pointer value that the compiler should know how to do. For example, you may static_cast to a pointer to a derived class. The programmer is responsible for being sure that the pointer you cast from really is a base class subobject of the specified derived class. But the compiler is responsible for adjusting any offsets in case that base class subobject is not the physically first component of the derived class.

By contrast, a reinterpret_cast strictly means the exact same pointer that pointed to one of those now points top one of these.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to converting the c++ code to assembly Nejad Programming 17 12-29-2008 12:50 PM
kernel: 10.x.x.x sent an invalid ICMP type 11, code 0 error to a broadcast: x.x.x.x Felipe Linux - Networking 0 02-25-2008 02:20 AM
Need Help Converting C# Code to C++ timrs Programming 1 04-03-2007 07:40 PM
invalid icmp type 3 code 3 error to a broadcast nkeever Linux - Newbie 3 06-12-2006 03:15 PM
converting vb6 code to python mrobertson Programming 2 06-02-2005 01:27 PM


All times are GMT -5. The time now is 11:36 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration