CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2009
    Posts
    12

    Question Need help with Void*

    The malfunctioning code snippet:
    Code:
    	ifstream uFile;
    	struct stat results;
    	char filePath[fileName.size()]; 
    	chstr(fileName,*filePath);
    	
    	uFile.open(filePath, ios::in | ios::binary);
    	
        if ( !uFile.is_open() )
        {
        	return false;
        }
        
        stat(filePath, &results);
        
        if (results.st_size <= 0)
        {
        	return false;
        }
        
        archFiles[fileAmt] = malloc(results.st_size+1);
        uFile.read(((char*)archFiles[fileAmt]),results.st_size);
    I don't have a clue whats wrong with it!

    Anyway, more in depth the last two lines are the problem.
    Everytime it gets to uFile.read the program crashes.

    A note:
    archfiles is defined as such:
    Code:
    void * archFiles[];
    I'm certain the problem lies in archFiles[fileAmt] but I haven't a clue whats wrong with it. Even an attempt to print out the value of the pointer (after the malloc ofc) results in a crash at the print statement!

    Edit: More info, I'm using windows and my compiler is g++
    Last edited by wd40bomber7; May 23rd, 2009 at 02:25 PM.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Need help with Void*

    Code:
    	ifstream uFile;
    	struct stat results;
    	char filePath[fileName.size()]; 
    //...
    void * archFiles[];
    The code in red is illegal C++. You cannot define array sizes using variables, and you cannot define arrays as empty.

    Use std::string to define string variables, and std::vector to define adjustable arrays.
    my compiler is g++
    That explains it. Turn off the extensions, and you will see that g++ will give you compiler errors (-Wall -pedantic). Otherwise, consult your g++ manual as to how to what those extensions do, as they are not legal C++.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 23rd, 2009 at 03:47 PM.

  3. #3
    Join Date
    May 2009
    Posts
    12

    Re: Need help with Void*

    Ok then, thanks for that. Do you have any suggestions on how to facilitate dynamic arrays?

    I don't have a g++ manual. I acquired it through minGW. Also I'm using a programming environment called Wind River Work Bench. As far as I can tell there's no way to turn off any specific parameters from within the environment so I'll take your word for it.

    Ok I changed
    Code:
    char filePath[fileName.size()];
    to
    Code:
    char *filePath = (char*)malloc(fileName.size());
    //...
    free((void*)filePath);
    Oh you said c++ doesn't allow for a variable to define an array size. What about in the following case?
    Code:
    int *a;
    int n;
    n = 25;
    a = new int[n];
    If that code works, my only problem would be expanding the array. The only solution to that, that I can think of is making a new array of the expanded size, copying all the data from the previous array to the new array then deleting the previous array.
    Or perhaps I could make a linked list, though this is somewhat more difficult to deal with and has 4-8 bytes of necessary overhead per array entry.

    Hmm after having thought about I'll probably go with a linked list of structs. The 8 bytes of overhead (per array element) is minor when being compared with the size of each array element.
    Last edited by wd40bomber7; May 23rd, 2009 at 04:14 PM.

  4. #4
    Join Date
    Apr 2009
    Location
    Netherlands
    Posts
    91

    Re: Need help with Void*

    Quote Originally Posted by wd40bomber7 View Post
    Ok then, thanks for that. Do you have any suggestions on how to facilitate dynamic arrays?
    use std::vector.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Need help with Void*

    To add, your code looks more like 'C' than C++. Usage of void* is not needed at all in the code you did show. Also, malloc() in a supposedly trivial C++ code is dubious, at best.

    Another thing as that you never mentioned where "fileAmt" comes from. It just looks like you want to read something into a buffer, and the size of the buffer is determined by some runtime value.
    Code:
    #include <string>
    #include <vector>
    #include <fstream>
    //... Other includes
    typedef std::vector<std::vector<char> > ArrayofCharArray;
    
    using namespace std;
    ArrayofCharArray archFiles;
    
    void someFunction()
    {
       ifstream uFile;
       stat results;
       string filePath;
       //...
      uFile.open(filePath, ios::in | ios::binary);
    	
       if ( !uFile.is_open() )
       {
        	return false;
        }
        
        stat(filePath.c_str(), &results);
        
        if (results.st_size <= 0)
        {
        	return false;
        }
        
        // Add an array of char to the vector
        archFiles.push_back(vector<char>());
    
        // Get the new array as a pointer for convenience
        std::vector<char>* buffer = &archFiles.back();
    
        // Resize it
        buffer->resize(results.st_size+1));
    
        // Use it in function
        uFile.read(&buffer->front(), results.st_size);
    }
    You now have an "array" of char arrays in archFiles. There is no call to malloc(), and the code is standard C++.

    To get the data archFiles[0] is the buffer that contains the first line. archFiles[0].size() is the number of characters in the first line. archFiles[1] is the second line, etc.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Need help with Void*

    Quote Originally Posted by wd40bomber7 View Post
    Ok then, thanks for that. Do you have any suggestions on how to facilitate dynamic arrays?
    I stated to use std::vector. That is the dynamic array in C++ (see my example).
    As far as I can tell there's no way to turn off any specific parameters from within the environment so I'll take your word for it.
    You must be able to do that somehow. The g++ compiler relies on proper setting of command-line parameters, both by the IDE and by users. If not, then the IDE is worthless.
    Ok I changed
    Code:
    char filePath[fileName.size()];
    to
    Code:
    char *filePath = (char*)malloc(fileName.size());
    //...
    free((void*)filePath);
    That is still 'C'. I stated to use std::string, which is the C++ way of defining strings.
    Oh you said c++ doesn't allow for a variable to define an array size. What about in the following case?
    Code:
    int *a;
    int n;
    n = 25;
    a = new int[n];
    'a' is not an array. It is a pointer to an integer. Two different things altogether.
    If that code works, my only problem would be expanding the array. The only solution to that, that I can think of is making a new array of the expanded size, copying all the data from the previous array to the new array then deleting the previous array.
    Get a good C++ (not 'C') book. The std::vector class should (must) be explained there, as all of this is done automatically for you.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; May 23rd, 2009 at 04:20 PM.

  7. #7
    Join Date
    Aug 2007
    Posts
    858

    Re: Need help with Void*

    Also I'm using a programming environment called Wind River Work Bench. As far as I can tell there's no way to turn off any specific parameters from within the environment so I'll take your word for it.
    If it doesn't even allow you to change compiler settings you just need to trash it and go with something else (Code::Blocks and Visual Studio are the popular choices).

  8. #8
    Join Date
    May 2009
    Posts
    12

    Re: Need help with Void*

    I find it very important to note
    uFile.open(filePath, ios::in | ios::binary);
    can not accept a string as its first parameter.

    As for the rest of the code I am looking it over now. I'm glad your taking the time to enlighten me.

    Ah.. I didn't know about string.c_str() /cry that could have saved me a few years.
    Last edited by wd40bomber7; May 23rd, 2009 at 04:27 PM.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Need help with Void*

    Quote Originally Posted by wd40bomber7
    I don't have a g++ manual.
    That can be rectified by consulting the appropriate documentation at the GCC website: GCC online documentation.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured