porting C code (unix) to a C++ prject (MSVS)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zman77
    New Member
    • Sep 2007
    • 17

    porting C code (unix) to a C++ prject (MSVS)

    Hi.
    I have a project (multiple header and source files) that was written in C, in unix. I made a new C++ project in MSVS 2005 (blank project), and am trying to simply copy the C code to my new C++ project. I thought the main problem I'd face would be finding the equivalent libraries for windows, however, I ran into another issue.
    I get the following error:

    error C2036: 'void *' : unknown size

    The line in question:
    Code:
    if(v->cmp(k, v->table + i * v->elem_size) == 0)
    The entire function:
    Code:
    void *vfind(VECTOR *v, void *k)
    {
    	UINT i;
    
    #if 0 /* bsearch is handing NULL pointers to v->cmp */
    	return (void*)bsearch(k, v->table, v->elem_count, v->elem_size, v->cmp);
    #endif
    
    	for(i = 0; i < v->elem_count; i++)
    	{
    		if(v->cmp(k, v->table + i * v->elem_size) == 0)
    		{
    			return v->table + i * v->elem_size;
    		}
    	}
    
    	return NULL;
    }
    There are other lines generating the same error, but I assume if I solve one, I can solve the rest. The original C code compiles just fine with Cygwin using gcc, and the only addition I made when porting it to my C++ project is #include <windows.h> for UINT. Oh, I also changed all uint declerations to UINT.

    How come the code compiles fine, but when I port it, it doesn't? What do I need to fix in order for the code to simply compile?
    Thank you for any assistance!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    what is the type of v->table? Can you show us where it is defined?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      A void* has an indeterminate size. Hence you can't use it in pointer arithmetic.

      Comment

      • zman77
        New Member
        • Sep 2007
        • 17

        #4
        Thank you for the replies.

        Here is where table is defined:
        Code:
        struct vector
        {
        	UINT    elem_count;      /* number of items in the vector */
        	UINT    size;            /* size of the vector */
        	UINT    elem_size;       /* element size */
        	int     (*cmp)(const void *, const void *);
        	void    *table;
        };
        If you can't do pointer arthimetic because void is of indeterminate size, how come the C code compiles with gcc? Is this the result of a difference in compilers, or C vs C++?

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by zman77
          If you can't do pointer arthimetic because void is of indeterminate size, how come the C code compiles with gcc? Is this the result of a difference in compilers, or C vs C++?
          It's a gcc compiler extension and therefore not portable (read this). Basically gcc performs pointer arithmatic on a void pointer by assuming that it points to data with a size of 1 byte (i.e. by treating it like a char pointer for the purposes of arithmetic).


          As with any extension on any platform it is not portable to other platforms.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Try compiling using the -pedantic switch. This disables non-standard language extensions. That is, it enforces ANSI/ISO C or C++. See of that starts producing the same sort of errors you see in Visual Studio.

            BTW, Visual Studio also has MIcrosoft language extensions enabled by default. You might want to go to your project properties to the C/C++ Language dialog and set Disable Language Extensions to YES.

            Comment

            • zman77
              New Member
              • Sep 2007
              • 17

              #7
              Disabling Microsoft Language Extensions worked (got rid of the error I described), but there are some other errors. I am assuming they are appearing for similar reasons.

              Trying to compile with -pedantic with gcc resulted in warnings given (warning: pointer of type `void *' used in arithmetic) on the same lines, but not errors.

              Thank you both for your help! Even though I still can't compile, at least I learned something, and things make more sense now.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Now it's making sense. gcc with -pedantic now can't work pointer arithmetic with a void* since it no longer can assume a size of the object pointed at.

                VC++ has the same problem.

                Now all you need do is get rid of that void* and you are good to go.

                Comment

                Working...