| View previous topic :: View next topic |
| Author |
Message |
reefbarman
Joined: 08 Jan 2007 Posts: 87 Location: Australia
|
Posted: Thu Jan 11, 2007 4:47 pm Post subject: Weird Behaviour when using sprintf on an array of strings |
|
|
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 |
|
 |
hlide
Joined: 10 Sep 2006 Posts: 750
|
Posted: Thu Jan 11, 2007 5:14 pm Post subject: |
|
|
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 |
|
 |
reefbarman
Joined: 08 Jan 2007 Posts: 87 Location: Australia
|
Posted: Thu Jan 11, 2007 5:25 pm Post subject: |
|
|
| 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 |
|
 |
hlide
Joined: 10 Sep 2006 Posts: 750
|
Posted: Thu Jan 11, 2007 6:03 pm Post subject: |
|
|
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 |
|
 |
reefbarman
Joined: 08 Jan 2007 Posts: 87 Location: Australia
|
Posted: Thu Jan 11, 2007 6:09 pm Post subject: |
|
|
| thank you ill give it a go and let you know my results |
|
| Back to top |
|
 |
hlide
Joined: 10 Sep 2006 Posts: 750
|
|
| Back to top |
|
 |
reefbarman
Joined: 08 Jan 2007 Posts: 87 Location: Australia
|
Posted: Thu Jan 11, 2007 7:27 pm Post subject: |
|
|
| it worked a treat thankyou i was hung up on this for too long |
|
| Back to top |
|
 |
Cy-4AH
Joined: 31 Jan 2007 Posts: 44 Location: Belarus
|
Posted: Wed Jan 31, 2007 10:40 pm Post subject: |
|
|
| 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 |
|
 |
reefbarman
Joined: 08 Jan 2007 Posts: 87 Location: Australia
|
Posted: Wed Jan 31, 2007 11:01 pm Post subject: |
|
|
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 |
|
 |
Cy-4AH
Joined: 31 Jan 2007 Posts: 44 Location: Belarus
|
Posted: Wed Jan 31, 2007 11:34 pm Post subject: |
|
|
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 |
|
 |
reefbarman
Joined: 08 Jan 2007 Posts: 87 Location: Australia
|
Posted: Thu Feb 01, 2007 12:17 am Post subject: |
|
|
| 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 |
|
 |
|