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 

Weird Behaviour when using sprintf on an array of strings

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



Joined: 08 Jan 2007
Posts: 87
Location: Australia

PostPosted: Thu Jan 11, 2007 4:47 pm    Post subject: Weird Behaviour when using sprintf on an array of strings Reply with quote

hi everyone

im getting some very weird behaviour from sprintf() or at least thats what i think is doing it anyway heere is my code
Code:
typedef struct lines{
   char * data;
   int pos;
}Lines;

Lines line[14];

for (i = 0; i < 14; i++)
   {
      line[i].data = "";
      line[i].pos = writeText;
      writeText += 16;
   }

temp = line[linesUsed].data;
sprintf(line[linesUsed].data, "%s%c", temp, buffer);
chars++;
totalChars++;
if (chars > 100){linesUsed++; chars = 0;}


now what happens is that if i place a char into buffer and add it to line[linesUsed].data with sprintf that exact same char is replicated through all 14 strings, so if i was to print all 14 strings out yhey are exactly the same where as im trying to change each string seperately can anyone see whats going on???
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Thu Jan 11, 2007 5:14 pm    Post subject: Reply with quote

this is normal : you are in fault to code this way.
Code:

for (i = 0; i < 14; ++i) line[i].data = "";

then :
Code:

sprintf(line[linesUsed].data, "%s%c", temp, buffer);


is definitely WRONG !

"" is a char const *, that is a pointer on the same address whatever the index in your array of line; so, when you append a string to a line, it is as if you appended to all lines. But what follows is worse : you are trying to modify a constant string which is only 1 byte long ("" <=> '\0') in a read only data section, so by appending this way you are scratching over other data being defined after your "" string. You are lucky that your app is not crashing. PSP has no read-only protection access on section since it has no MMU for that, so it doesn't raise an exception when you attempt to append a string to your constant string.
Back to top
View user's profile Send private message
reefbarman



Joined: 08 Jan 2007
Posts: 87
Location: Australia

PostPosted: Thu Jan 11, 2007 5:25 pm    Post subject: Reply with quote

thanks for that i understand what you are saying, i have heard of this before just didnt strike me this time when i did it, is there another way to do what i want to do?? cause im having a bit of a mental block and cant see past my problem
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Thu Jan 11, 2007 6:03 pm    Post subject: Reply with quote

something like :

Code:

#define MAX_CHARS 32

typedef struct lines
{
  char data[MAX_CHARS];
  int pos;
} Lines;


just to get the length of characters :

Code:

int len = snprintf(NULL, 0, "%s%c", temp, buffer);


to append safely (string is truncated if overflowing) :
Code:

int len = snprintf(lines[linesUsed].data, MAX_CHARS, "%s%c", temp, buffer);
Back to top
View user's profile Send private message
reefbarman



Joined: 08 Jan 2007
Posts: 87
Location: Australia

PostPosted: Thu Jan 11, 2007 6:09 pm    Post subject: Reply with quote

thank you ill give it a go and let you know my results
Back to top
View user's profile Send private message
hlide



Joined: 10 Sep 2006
Posts: 750

PostPosted: Thu Jan 11, 2007 6:21 pm    Post subject: Reply with quote

you can find some examples of usage here : http://libslack.org/manpages/snprintf.3.html
Back to top
View user's profile Send private message
reefbarman



Joined: 08 Jan 2007
Posts: 87
Location: Australia

PostPosted: Thu Jan 11, 2007 7:27 pm    Post subject: Reply with quote

it worked a treat thankyou i was hung up on this for too long
Back to top
View user's profile Send private message
Cy-4AH



Joined: 31 Jan 2007
Posts: 44
Location: Belarus

PostPosted: Wed Jan 31, 2007 10:40 pm    Post subject: Reply with quote

reefbarman, you need basics of C, before starting programming. Because without it in future, you will get alot of problems with pointers and memory allocation.
Back to top
View user's profile Send private message
reefbarman



Joined: 08 Jan 2007
Posts: 87
Location: Australia

PostPosted: Wed Jan 31, 2007 11:01 pm    Post subject: Reply with quote

it nice to know someone cares,

but i think i can do without silly remarks

i have alot of experience with programming just happens that not too much of it is with c, im currently doing my masters degree in Software Engineering and have had about 4 years coding experience with c++, java, php, python and shell, so as i said im alright and my preferred language is c++, so jumping back a notch to the c language just meant i didnt know some of the specifics and functions it uses, but if you would like to check out some of my coding work check out my current project http://reefbarman.110mb.com/pSPC%20Beta2.1.rar

have fun
Back to top
View user's profile Send private message
Cy-4AH



Joined: 31 Jan 2007
Posts: 44
Location: Belarus

PostPosted: Wed Jan 31, 2007 11:34 pm    Post subject: Reply with quote

Oh, man, I wasn't doubt that you are programmer. I was just thinking that you programmed on Pascal or Java, maybe C# before and now started on C/C++. And just warned you, because alot of C-programmers have problems, because don't know language in details.
But you writed, that you experienced with C++, it is surprising me. How you could make such foolish mistake in that case? What you was using when worked wtih strings? CString or std::string?
Back to top
View user's profile Send private message
reefbarman



Joined: 08 Jan 2007
Posts: 87
Location: Australia

PostPosted: Thu Feb 01, 2007 12:17 am    Post subject: Reply with quote

in c++ i always used std::string i had no other need to use c style strings and functions that required them, i have used them before but in no way anything similar to what i am doing with them now which is basically construction an osk and typing functions which would be alot easier in c++ so i am just having to learn as you said the basics again, thanks for your concern
Back to top
View user's profile Send private message
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