Thread: Converting c to c++, help!

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    Converting c to c++, help!

    Hi,

    I am trying to convert a complex c program to c++ to make it easier to maintain. I am trying to complie the c code using g++ and I get the following error:

    error: invlid conversion from 'void *' to 'char *'

    I am freeing up some memory in the c program. The code that causes the error is:
    Code:
    seq=ckfree((void *)seq);
    This calls the following function:
    Code:
    void *ckfree(void *ptr)
    {
    	if (ptr == NULL)
    		warning("Bad call to ckfree\n");
    	else {
    	 	free(ptr);
    		ptr = NULL;
    	}
    	return ptr;
    }
    The variable seq is a char*.
    Does anyone know what could be causing the error? Is it something that is not allowed in c++, that is allowed in c? Also does anyone has links to information on things to look out for when converting c to c++?

    If anyone can help it would be much appreciated.
    Mark

  2. #2
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    seq=(char *)ckfree((void *)seq);

    could this one help?

    blow me ... ...

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    Seems to work

    That gets rid of that particular error I am having. But I am getting them all over the place. The function ckfree that I have in my first post is used to free up the memory I am using. It just prints an error if the ptr is null.
    The thing is that the code works in c, but does not work in c++. Does anyone know why this is? Why do I have to convert back to a char *? And will this still free up the memory? A void * is just any type of pointer right?

    Thanks
    Mark

  4. #4
    Never Exist Hermitsky's Avatar
    Join Date
    Jul 2004
    Posts
    149
    eee.....in c++, you have to convert it first, that's the rule.

    and for sure, the memory will be freed.
    Last edited by Hermitsky; 11-17-2005 at 06:29 AM.

    blow me ... ...

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    void* pVoid = 0;
    char* pChar = 0;
    
    pChar =  pVoid; //error
    
    pChar = static_cast<char*>(pVoid); //ok

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    Thanks

    I will give that a try.

  7. #7
    Super Moderator Harbinger's Avatar
    Join Date
    Nov 2004
    Posts
    74
    > I am trying to convert a complex c program to c++ to make it easier to maintain.
    rewrite it then.

    Unless you count a 'cast-fest' which results from your quick hack as being more maintainable.

    It will only ever look like C converted to C++ unless you sit down and actually spend some time thinking about how best to implement it as a true C++ program.

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    38

    Rewrite

    It will end up being effectively a rewrite. I want to do it in steps though, so I would like to have the code compiling as I am going along with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. Tips for converting C prog. to C++?
    By eccles in forum C++ Programming
    Replies: 7
    Last Post: 01-15-2005, 07:38 AM
  3. Converting Sign Magnitude Integer Binary Files
    By Arthur Dent in forum C Programming
    Replies: 7
    Last Post: 09-13-2004, 10:07 AM
  4. Converting from Screen to World Coordinates
    By DavidP in forum Game Programming
    Replies: 9
    Last Post: 05-11-2004, 12:51 PM
  5. converting string to float
    By twhubs in forum C Programming
    Replies: 1
    Last Post: 09-16-2001, 09:02 AM