Weird Behaviour when using sprintf on an array of strings

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
reefbarman
Posts: 87
Joined: Mon Jan 08, 2007 12:16 pm
Location: Australia

Weird Behaviour when using sprintf on an array of strings

Post by reefbarman »

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: Select all

typedef struct lines{
	char * data;
	int pos;
}Lines;

Lines line[14];

for &#40;i = 0; i < 14; i++&#41;
	&#123;
		line&#91;i&#93;.data = "";
		line&#91;i&#93;.pos = writeText;
		writeText += 16;
	&#125;

temp = line&#91;linesUsed&#93;.data;
sprintf&#40;line&#91;linesUsed&#93;.data, "%s%c", temp, buffer&#41;;
chars++;
totalChars++;
if &#40;chars > 100&#41;&#123;linesUsed++; chars = 0;&#125;
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???
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

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

Code: Select all

for &#40;i = 0; i < 14; ++i&#41; line&#91;i&#93;.data = "";
then :

Code: Select all

sprintf&#40;line&#91;linesUsed&#93;.data, "%s%c", temp, buffer&#41;;
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.
reefbarman
Posts: 87
Joined: Mon Jan 08, 2007 12:16 pm
Location: Australia

Post by reefbarman »

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
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

something like :

Code: Select all

#define MAX_CHARS 32

typedef struct lines
&#123;
  char data&#91;MAX_CHARS&#93;;
  int pos;
&#125; Lines;
just to get the length of characters :

Code: Select all

int len = snprintf&#40;NULL, 0, "%s%c", temp, buffer&#41;;
to append safely (string is truncated if overflowing) :

Code: Select all

int len = snprintf&#40;lines&#91;linesUsed&#93;.data, MAX_CHARS, "%s%c", temp, buffer&#41;;
reefbarman
Posts: 87
Joined: Mon Jan 08, 2007 12:16 pm
Location: Australia

Post by reefbarman »

thank you ill give it a go and let you know my results
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

you can find some examples of usage here : http://libslack.org/manpages/snprintf.3.html
reefbarman
Posts: 87
Joined: Mon Jan 08, 2007 12:16 pm
Location: Australia

Post by reefbarman »

it worked a treat thankyou i was hung up on this for too long
Cy-4AH
Posts: 44
Joined: Wed Jan 31, 2007 9:58 pm
Location: Belarus

Post by Cy-4AH »

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.
reefbarman
Posts: 87
Joined: Mon Jan 08, 2007 12:16 pm
Location: Australia

Post by reefbarman »

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
Cy-4AH
Posts: 44
Joined: Wed Jan 31, 2007 9:58 pm
Location: Belarus

Post by Cy-4AH »

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?
reefbarman
Posts: 87
Joined: Mon Jan 08, 2007 12:16 pm
Location: Australia

Post by reefbarman »

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
Post Reply