forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

passing dynamic character arrays

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
iamjeff



Joined: 20 Jul 2006
Posts: 3

PostPosted: Sun Aug 06, 2006 12:34 pm    Post subject: passing dynamic character arrays Reply with quote

hi, im new here... this might be a simple question, but i don't know how to fix it.

i read data in from a file and store it in a buffer
now, im trying to extract data from that buffer and store it in another variable. here is my code:

Code:
class Element
{

public:
   int GetNodeSize() {return nodesize;}
   char * GetNodeElement() {return node;}
   
   void SetNodeSize(int size) {
      nodesize = size;
      node = new char [nodesize];
   }
      
   void SetNodeElement(char *temp) {
      node = temp;
   }
   
private:
   int nodesize;
   char *node;

};


Code:
void parseBuffer(char *buffer, long lSize) {
   int x, y, loop;
   char *tempnode;
   for (x = 0; x <= lSize; x++)
      if (buffer[x] == '<') {
         y=x;
         while (buffer[y] != '>')
            y++;
         XML[0].SetNodeSize((y-x)-1);
         tempnode = new char [XML[0].GetNodeSize()];
         for (loop = 0; loop < XML[0].GetNodeSize(); loop++)
            tempnode[loop] = buffer[(x+1)+loop];
         XML[0].SetNodeElement(tempnode);
         delete [] tempnode;
         break;
      }
   printf("%s\n", XML[0].GetNodeElement());
}


XML[0].GetNodeSize() returns the correct value.
however, the printf statement returns garbage.

if i change the printf statement to print tempnode instead (and comment out "delete tempode"), it comes close... it prints out the string i want, plus a few trailing garbge characters.

i figure it has something to do with the way i pass it to SetNodeElement (or maybe the assignment), but ive been trying all sorts of things and cant figure it out. what am i doing wrong?
Back to top
View user's profile Send private message AIM Address
Tinnus



Joined: 29 Jul 2006
Posts: 67

PostPosted: Sun Aug 06, 2006 1:00 pm    Post subject: Reply with quote

Code:
void SetNodeElement(char *temp) {
      node = temp;
   }

That copies the pointer, not the string data. You should use something like strcpy or a for loop.

Also, for the printf not to print garbage, you should add a NULL character( '\0' ) at the end of your string when reading from the file.
_________________
Let's see what the PSP reserves... well, I'd say anything is better than Palm OS.
Back to top
View user's profile Send private message
iamjeff



Joined: 20 Jul 2006
Posts: 3

PostPosted: Sun Aug 06, 2006 1:21 pm    Post subject: Reply with quote

works perfectly, much thanks!
Back to top
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group