passing pointer to structure to a function

This is a discussion on passing pointer to structure to a function within the C++ Programming forums, part of the General Programming Boards category; Hi Guys, I am a newbie to c++ programming and I need help with my program. Can anybody help me ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    passing pointer to structure to a function

    Hi Guys,
    I am a newbie to c++ programming and I need help with my program. Can anybody help me out? I am trying to pass pointer to a structure to create an object. The program compiles fine but during execution I get the following error:


    Debug Assertion Failed!

    Program: C:\PROGRAM FILES\.....
    File: dbgdel.cpp
    Line: 47

    Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)


    This is visual studio .net 2003 environment.

    Couple of things more: I am just starting out in C++ and any kind of feedback on the logic/design of my program is appreciated. I dont know if passing structures by reference is a good idea, please let me know how to do that. Also please ignore the mess of file input, it is for testing and unrelated (I hope ) to the error. I have an idea that my delete call to destructor is causing the error, but I dont know how and how to fix it. And thanks in advance for your input.

    -Sam

    I tried searching the forums before posting but could not find anything.

    Code:
    struct strArray {
    	int size;
    	int position;
    	int *byteArray;
    };
    
    
    class BinaryInputStream {
    
    	protected :	
    		strArray *source;
    		
    	private :
    		int i;
    
    	public:
    		BinaryInputStream(strArray *source) {
    			this->source = source;
    			//for (i = 0; i < source->size; i++) {
    			i = 0;
    			while (i < source->size) {
    				cout << "in default constructor " << source->byteArray[i] << "\n";
    				i++;
    			}
    
    		}
    		
    		~BinaryInputStream() {
    			delete [] source->byteArray;
    			delete source;
    		}
    }
    
    int main(int argc, char **argv) {
    
    	int i = 0, x=0;
    	strArray *source;
    	strArray src;
    	source = & src;
    
        if ((argc < 2)) {
    		cout << "\nMissing arguments\n\n";
    		cout << "USAGE : mre inputfilename\n\n";
            return 0;
    	} else {
    		cout << "Hello World\n The file size is :";		
    
    		ifstream inFile;
    		inFile.open(argv[1], ios::binary);
    		inFile.seekg(0, ios::end);
    		i = inFile.tellg();
    		cout <<i << "\n";
    
    		source->byteArray = new int[i];
    		if (source->byteArray == NULL) {
    			return 0;
    		}
    
    		while ((inFile.good()) && (x < i)) {
    			source->byteArray[x] = x + 1;
    			cout << "Byte " << x << ": " << source->byteArray[x] <<"\n";
    			x++;
    		}
    
    		inFile.close();
    
    		BinaryInputStream *ptr = new BinaryInputStream(source);
    		delete ptr;
    
    	}
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    1)First, I would suggest you use a little more creativity, and not name ANY variables with the same name:
    Code:
    BinaryInputStream(strArray *source) {
    		this->source = source;
    How about this:
    Code:
    BinaryInputStream(strArray* ptr) {
    			this->source = ptr;
    As it is, it makes it difficult just to talk about your code.

    2)You only delete memory that was allocated with new.
    Code:
    strArray *source;
    strArray src;
    source = & src;
    The variable source is a pointer that was not created using new, so you wouldn't delete it. This is essentially what you are doing:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	int* source;
    	int num = 10;
    	source = &num;
    
    	delete source;
    
    	return 0;
    }
    Try running that program and see what happens.

    In addition, you only use "delete []" when you created an array using "new". In otherwords, if you use "new[]", then you use "delete []". If you only used "new", then you only use "delete". Here is an example:
    Code:
    #include <iostream>
    using namespace std;
    
    struct Apple
    {
    	int size;
    	int color;
    };
    
    int main()
    {
    	Apple* ptr1 = new Apple;
    	delete ptr1;
    
    	Apple* ptr2 = new Apple[10];  //used new with '[]' somewhere to the right of new
    	delete [] ptr2; //..then use delete with '[]'
    
    	return 0;
    }
    Last edited by 7stud; 04-15-2005 at 07:20 PM.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Hi 7stud!

    Thanks for your comments. Regarding 1) I completely agree and am thankful for your advice, will follow it. 2) I found that out and fixed it. But 3) was something I had read and learnt but I forgotten about it !! And thanks again!! You guys are awesome!!!

    -sam

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Passing pointer into function bug
    By CodeMonkey in forum C++ Programming
    Replies: 4
    Last Post: 04-27-2005, 12:13 AM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 02:09 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21