GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Feb-2007, 11:18
acosgaya acosgaya is offline
New Member
 
Join Date: Jun 2006
Posts: 13
acosgaya is on a distinguished road

pointer/memory issue


Hi, I have an issue with the following piece of code, in it I read a node from a binary file, and return it.

CPP / C++ / C Code:
Node* ArrayRTree::readNode(ifstream &in, int N_SIZE, int pos){
  Node* p = new Node(); 
  MYTYPE t; 

	for (int i = 0; i < m_maxDim; i++)
		p->m_pDims.push_back(t);		
	
	in.seekg(pos*N_SIZE);
	in.read(reinterpret_cast<char *>(&p->m_pDims[0]), sizeof(MYTYPE) * m_maxDim);
	in.read(reinterpret_cast<char *>(&p->m_child), sizeof(p->m_child));
	in.read(reinterpret_cast<char *>(&p->m_children), sizeof(p->m_children));		
	
	return p;
}
....

//I get the resulting value later in my code as:
 Node* p = new Node;   
 p = readNode(in, N_SIZE, 0); 

this works fine, but I will call this operation many times, so I think doing this loop
CPP / C++ / C Code:
	for (int i = 0; i < m_maxDim; i++)
		p->m_pDims.push_back(t);	
that many times, in the overall, costs me a lot of time, and also I am not sure if the "Node *p" inside the function ReadNode gets deleted after I return its contents, or maybe I am creating a new one every time I call the function, and the previous one is still in memory, causing waste of it. is this really happening?

So I was thinking, maybe instead of returning the node, better to pass it to the function (do the for loop outside), and it just gets rewritten every time I use it. The problem is that I tried it, but it didn't work, and as I am storing those read values somewhere else, it just becomes a mess, and only keeps the last value that was read.

this was my idea, that didn't work:
CPP / C++ / C Code:
void ArrayRTree::readNode(Node *p, ifstream &in, int N_SIZE, int pos){

	in.seekg(pos*N_SIZE);
	in.read(reinterpret_cast<char *>(&p->m_pDims[0]), sizeof(MYTYPE) * m_maxDim);
	in.read(reinterpret_cast<char *>(&p->m_child), sizeof(p->m_child));
	in.read(reinterpret_cast<char *>(&p->m_children), sizeof(p->m_children));			
			
}

...
// later on
 Node* p = new Node;   
 readNode(p, in, N_SIZE, 0); 

 // but p doesn't seem to get the right value

I guess it has to do with some pointer management, but not sure how to fix it.

I'd appreciate any insight and help with it.

Thanks
  #2  
Old 09-Feb-2007, 12:50
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: pointer/memory issue


Since the class for which m_pDims is an object has a push_back() function and overloaded [], maybe it has resize(). (Like a std::vector, for example). If it does, then maybe the loop could be replaced by

CPP / C++ / C Code:
    p->m_pDims.resize(m_maxDim);

(I am assuming that the vector has size = 0 when this function is invoked, otherwise the resize argument would be p->m_pDims.size() + m_maxDim, since your loop appends that m_maxDim elements to whatever the present vector is.)

(I'm assuming that m_maxDim is not constant for a given object of this class; otherwise you would have fixed the size in the constructor, right?)

(I'm also assuming that you really need a vector, since if you aren't actually using m_pDims as a vector ---that is, you are always accessing it with [] notation rather than pushing and popping and you always know its size--- then you would have made it an array rather than a vector, right?)

Regards,

Dave
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to issue dos command from c program jaininaveen C Programming Language 2 03-Mar-2006 11:02
Computer resets - weird memory slot issue SpikeyUK Computer Hardware Forum 7 22-Jul-2005 19:15
PHP/MySQL coding issue cmarti MySQL / PHP Forum 3 26-Jul-2004 09:01
PHP/Apache memory usage issue bacchus Apache Web Server Forum 0 18-Aug-2003 13:57
Possible issue with HTML BBCode? JdS GIDForums™ 2 16-Aug-2003 12:14

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:40.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.