C/C++ Programming Help Thread
This is a discussion on C/C++ Programming Help Thread within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; Zitat von CtrlAltDeleteDie Makefile: Code: TARGET = Select Square OBJS = main.o YOURLIBS= INCDIR = CFLAGS = -G4 -Wall -O2 ...
-
10-09-2006, 11:37 AM #1201Developer

- Registriert seit
- Mar 2006
- Beiträge
- 1.026
- Points
- 7.577
- Level
- 58
- Downloads
- 0
- Uploads
- 0
Your target can only be one word. Change it to:
Zitat von CtrlAltDeleteDie
Code:TARGET = SelectSquare

Check out my homebrew & C tutorials at http://insomniac.0x89.org/
Coder formerly known as Insomniac197
tshirtz: what is irshell ??
Atarian_: it's where people who work for the IRS go when they die
-
10-10-2006, 07:25 AM #1202QJ Gamer Blue
- Registriert seit
- Sep 2006
- Ort
- Germany
- Beiträge
- 216
- Points
- 4.511
- Level
- 42
- Downloads
- 0
- Uploads
- 0
Spoiler for old post:
Strings and variable length
Hi,
how can I create strings of variable length in C(non++)? I have a viariable that will be filled in a loop; I can't say when this loop will stop, I don't know how long the string will be. Do I have to use realloc(string, sizeof(string)+sizeof(add edString)) each time in the loop? Or is there a better way?Geändert von Lukeson (10-10-2006 um 07:26 AM Uhr) Grund: Automerged Doublepost
-
10-10-2006, 07:33 AM #1203QJ Gamer Green
- Registriert seit
- Jan 2006
- Beiträge
- 4.289
- Points
- 25.223
- Level
- 95
- Downloads
- 0
- Uploads
- 0
char string[variable]; ?
I can't remember, its been so long since I've programmed, and now I'm taking a Java class in school...QJ loads way 2 slow & the theme is ugly as hell, gone are the glory days
-
10-10-2006, 07:43 AM #1204QJ Gamer Blue
- Registriert seit
- Sep 2006
- Ort
- Germany
- Beiträge
- 216
- Points
- 4.511
- Level
- 42
- Downloads
- 0
- Uploads
- 0
And what if the string gets longer than <variable>?char string[variable];
-= Double Post =-
I'd actually even be happy if someone could confirm that the method I thought of (using realloc each time in the loop) actually is a method and not utter bull****Geändert von Lukeson (10-10-2006 um 07:43 AM Uhr) Grund: Automerged Doublepost
-
10-10-2006, 08:07 AM #1205Developer

- Registriert seit
- Mar 2006
- Beiträge
- 1.026
- Points
- 7.577
- Level
- 58
- Downloads
- 0
- Uploads
- 0
malloc & realloc.

Check out my homebrew & C tutorials at http://insomniac.0x89.org/
Coder formerly known as Insomniac197
tshirtz: what is irshell ??
Atarian_: it's where people who work for the IRS go when they die
-
10-10-2006, 08:10 AM #1206QJ Gamer Silver

- Registriert seit
- Jun 2006
- Ort
- UK
- Beiträge
- 2.326
- Points
- 10.263
- Level
- 67
- Downloads
- 0
- Uploads
- 0
You really want to avoid reallocating memory at runtime on a console. Just make the buffer a fixed size (eg 4096) and ensure that the message sent is no longer then that.
Your method of realloc would work but might be slow depending on the implementation of the function.
Another method would be to send the size of the message of first then the actual message.
-
10-10-2006, 10:14 AM #1207QJ Gamer Blue
- Registriert seit
- Sep 2006
- Ort
- Germany
- Beiträge
- 216
- Points
- 4.511
- Level
- 42
- Downloads
- 0
- Uploads
- 0
I'm retrieving a list of artists from winamp, always different and surely larger than 4096 at many PCsJust make the buffer a fixed size (eg 4096) and ensure that the message sent is no longer then that.
If it makes the console run slow I think I'll go with the other solution you mentioned. Can you actually send and recieve X bytes or is there a limit? Like i need to send a list of 411 Artist (~6165bytes) at once...Your method of realloc would work but might be slow depending on the implementation of the function.
-= Double Post =-
Converting numbers to char-arrays
I get a number from sizeof and need to save it in an array of chars (char length[8]). How do you do that?Geändert von Lukeson (10-10-2006 um 10:30 AM Uhr) Grund: Automerged Doublepost
-
10-10-2006, 10:44 AM #1208QJ Gamer Silver

- Registriert seit
- Jun 2006
- Ort
- UK
- Beiträge
- 2.326
- Points
- 10.263
- Level
- 67
- Downloads
- 0
- Uploads
- 0
Number to char arrays, use sprintf.
Yes there is a limit to how much data you can send in a packet on the network. I can't remember off hand what it is though.
-
10-10-2006, 10:45 AM #1209AKA Homer

- Registriert seit
- Jan 2006
- Ort
- Sweden
- Beiträge
- 1.779
- Points
- 12.596
- Level
- 73
- Downloads
- 0
- Uploads
- 0
You'd have to use the itoa function.
Zitat von Lukeson
Or sprintf if you want.
-
10-10-2006, 11:07 AM #1210QJ Gamer Blue
- Registriert seit
- Sep 2006
- Ort
- Germany
- Beiträge
- 216
- Points
- 4.511
- Level
- 42
- Downloads
- 0
- Uploads
- 0
Thanks guys
-= Double Post =-
Hmmm but how do I convert a char to an int? I tried
artist_list_length = (int)artist_list_length_c ;
but it fills artist_list_length with 16thousand-whatever-the-unsigned-int-maximum is...Geändert von Lukeson (10-10-2006 um 11:07 AM Uhr) Grund: Automerged Doublepost
-
10-10-2006, 11:12 AM #1211Developer

- Registriert seit
- Mar 2006
- Beiträge
- 1.026
- Points
- 7.577
- Level
- 58
- Downloads
- 0
- Uploads
- 0

Check out my homebrew & C tutorials at http://insomniac.0x89.org/
Coder formerly known as Insomniac197
tshirtz: what is irshell ??
Atarian_: it's where people who work for the IRS go when they die
-
10-10-2006, 11:12 AM #1212QJ Gamer Silver

- Registriert seit
- Jun 2006
- Ort
- UK
- Beiträge
- 2.326
- Points
- 10.263
- Level
- 67
- Downloads
- 0
- Uploads
- 0
atoi()
-
10-10-2006, 12:12 PM #1213QJ Gamer Blue
- Registriert seit
- Sep 2006
- Ort
- Germany
- Beiträge
- 216
- Points
- 4.511
- Level
- 42
- Downloads
- 0
- Uploads
- 0
Yay, everything works fine now, thanks again!
PS: This is the first time I'm doing something in C, so please be kind to me
-= Double Post =-
i have another noob-question: I got the following code
to get the artists from winamp. The part with malloc freezes my PSP. How else can I make buffer the right size?Code:void get_artists(int socket, char * recivedMessage) { buf[recieved_size] = recieve_message_of_variable_length(); recivedMessage = (char)malloc(recieved_size); } char buffer; get_artists(socket, buffer);Geändert von Lukeson (10-10-2006 um 12:12 PM Uhr) Grund: Automerged Doublepost
-
10-10-2006, 12:48 PM #1214Developer

- Registriert seit
- Feb 2006
- Ort
- Norway
- Beiträge
- 384
- Points
- 5.359
- Level
- 47
- Downloads
- 0
- Uploads
- 0
I've been trying to get the debug text to work with a prx for devhook.
I first tried using the normal one from pspdebug.h (pspDebugScreenPrintf)
but that gave me these errors
Spoiler for errors:
I also tried without defining which I thought maybe could possibly workbecouse I had -fno-builtin-printf which I have in CFLAGS. I didn't get any errors then but no text appeared..
Do I have to do something else to get it working in prx'es? Or is there some other simple text function that exists that works?
-
10-10-2006, 02:54 PM #1215words are stones in my <3

- Registriert seit
- Jul 2005
- Ort
- Spokane
- Beiträge
- 5.008
- Points
- 35.274
- Level
- 100
- My Mood
-
- Downloads
- 1
- Uploads
- 0
waterbottle - Sorry, i havent any experience with PRX printing routines... Or anything PRX that matter...
Lukeson - 'malloc' doesnt just stop working and makes it crash. Either you havent free'd the current memory in recieveMessage, you're trying to allocate memory to something whose memory already has been allocated or all else fails, something you're calling before is causing it to crash -- meaning maybe a call to malloc before yours corrupts the stack, or something of the sort... All i know for sure is malloc doesnt just stop working for no reason...
Best of luck with your problem, wish i could help more...
...at what speed must I live.. to be able to see you again?...
Projects
You can support my Open World 3D RPG for PSP by voting for it here
-
10-10-2006, 03:13 PM #1216likes kittens....awww....
- Registriert seit
- Sep 2006
- Ort
- Detroit
- Beiträge
- 628
- Points
- 6.975
- Level
- 55
- Downloads
- 0
- Uploads
- 0
Can vars have double quotes in them?
For example:
Code:short inches = "Inches";
-
10-10-2006, 04:06 PM #1217words are stones in my <3

- Registriert seit
- Jul 2005
- Ort
- Spokane
- Beiträge
- 5.008
- Points
- 35.274
- Level
- 100
- My Mood
-
- Downloads
- 1
- Uploads
- 0
psp
- character arrays, yes. Others, not so sure but you CAN add the ASCII value together and have hte same effect I suppose, but after each char you need an + operator...
int string = 'h'+'e'+'l'+'l'+'o'; // add ASCII values together, not so useful
char string[] = "hello"; // auto sizes array
char string[6] = "hello"; // make room for '\0' char
...at what speed must I live.. to be able to see you again?...
Projects
You can support my Open World 3D RPG for PSP by voting for it here
-
10-11-2006, 04:24 AM #1218QJ Gamer Blue
- Registriert seit
- Sep 2006
- Ort
- Germany
- Beiträge
- 216
- Points
- 4.511
- Level
- 42
- Downloads
- 0
- Uploads
- 0
The correct version should be:
Zitat von Me
However this version freezes after printing "recievedMessage-size: 15" and doesn't print "get_artists returned: message".Code:void get_artists(int socket, char * recivedMessage) { recieved_size = recieve_message_size(); buf[recieved_size] = recieve_message_of_variable_length(); recivedMessage = (char*)malloc(recieved_size); printf("recievedMessage-size: %d\n", sizeof(recivedMessage)); return; } char * buffer; get_artists(socket, buffer); printf("get_artists returned: %s", buffer);
Does anyone have an explanation for this?
-= Double Post =-
Or, if not an explanation, probably even a better solution for getting a string of variable length from a functionGeändert von Lukeson (10-11-2006 um 04:36 AM Uhr) Grund: Automerged Doublepost
-
10-11-2006, 04:29 AM #1219
you assign the return value from receive_message_of_variab le_length() to the array, not the variable recieved_sizeCode:void get_artists(int socket, char * recivedMessage) { buf[recieved_size] = recieve_message_of_variable_length(); }
Since recieved_size is not assign a value, it contains random value. You're allocation a random number of memory.Code:recivedMessage = (char*)malloc(recieved_size); }
Memory allocated, but you didn't assign any value to it. You're trying to print some random value.Code:printf("recievedMessage-size: %d\n", sizeof(recivedMessage)); return; }
Two more things:
1. sizeof(recivedMessage), it returns the size of a char*, becoz recivedMessage is a char*. It won't return the message size.
2. If you don't use the pointer correctly, it's always result in a crash.
-
10-11-2006, 04:50 AM #1220Heroes never die

- Registriert seit
- Aug 2006
- Ort
- ...........
- Beiträge
- 1.323
- Points
- 8.645
- Level
- 62
- Downloads
- 0
- Uploads
- 0
correct;)
-
10-11-2006, 05:36 AM #1221QJ Gamer Blue
- Registriert seit
- Sep 2006
- Ort
- Germany
- Beiträge
- 216
- Points
- 4.511
- Level
- 42
- Downloads
- 0
- Uploads
- 0
Ahh, sorry, what I wrote up there is simplified code just to explain the problem, here's the real deal:
In another function that is called in main():Code:void get_artists(int socket, char * recivedMessage) { char *command; int command_len; char artist_list_length_c[8]; int artist_list_length; long rc; command = "get_artists"; command_len = strlen(command); if (send(socket, command, command_len, 0) != command_len) { printf("send() sent a different length of bytes than expected!\n"); // needs real exceptionhandling return; } printf("\n\nsending command get_artists...\n"); if((rc = recv(socket, artist_list_length_c, 8,0)) < 0) { printf("Error while recieving the artist-list-length; error-code: %s\n",strerror(errno)); return; } if(rc==0) { printf("Client has closed the connection before sending the artist-list-length\n"); return; } printf("Recieving artist-list-length\n"); artist_list_length_c[rc]='\0'; artist_list_length = atoi(artist_list_length_c); printf("Artistlist length: %d\n", artist_list_length); artist_list_length++; char buf[artist_list_length]; recivedMessage = (char*)malloc(artist_list_length); printf("recieving artist-list...\n"); rc = recv(socket,buf,artist_list_length,0); if(rc==0) { printf("Server has closed the connnection before sending the artist-list\n"); return; } if(rc==SOCKET_ERROR) { printf("Error: recv, error-code: %s\n",strerror(errno)); return; } buf[rc]='\0'; strcpy(recivedMessage, buf); printf("recivedMessage length: %d\n", strlen(recivedMessage)); printf("buf: %s\n", buf); return; }
Sorry for the confusionCode:char* artist_list; get_artists(sock, artist_list); printf("\n\nget_artists returned: %s\n", artist_list);
-= Double Post =-
Hoever it works using malloc after the initialization and the reallocating it... .Geändert von Lukeson (10-11-2006 um 05:36 AM Uhr) Grund: Automerged Doublepost
-
10-11-2006, 06:19 AM #1222Heroes never die

- Registriert seit
- Aug 2006
- Ort
- ...........
- Beiträge
- 1.323
- Points
- 8.645
- Level
- 62
- Downloads
- 0
- Uploads
- 0
i have some problem when you make array's and let the user put in the value
how can you "+" them
for exemple
note two :int repeat()
{
int totaal , scores[5];
totaal=?????????????????
cout<<"geef de scores van de toetsen\n";
cin>>scores[1]>>scores[2]>>scores[3]>>scores[4]>>scores[5];
cout<<"de gemmidelde score voor deze test is "<<totaal /(sizeof(scores)/sizeof(int));
how cane you let the user put in more numbers for example
the user wants to put in 20 numbers expect 5
int repeat()
{
int totaal , scores[?????];
totaal=??????????;
cout<<"geef de scores van de toetsen\n";
cin>>??????????;
cout<<"de gemmidelde score voor deze test is "<<totaal /(sizeof(scores)/sizeof(int));
i use cout , but dont wory this is something for psp;)
-
10-11-2006, 07:30 AM #1223QJ Gamer Blue
- Registriert seit
- Sep 2006
- Ort
- Germany
- Beiträge
- 216
- Points
- 4.511
- Level
- 42
- Downloads
- 0
- Uploads
- 0
Errm, what do you mean?how can you "+" them
totaal = scores[1] + scores[2] + scores[3] + scores[4] + scores[5];
?
Example:how cane you let the user put in more numbers
Code:int n=0, max=5, z,i; int *scores=NULL; scores = (int *)calloc(max, sizeof(int)); printf("enter scores; end with 0\n"); while(1) { printf("Enter Score #%d: ", n+1); scanf("%d", &z); if(z==0) break; if(n >= max) { max += max; scores = (int *)realloc(scores,max*sizeof(int)); } scores[n++] = z; } printf("The following scores have been entered ->\n\n"); for(i = 0; i < n; i++) printf("%d ", scores[i]); printf("\n"); free(scores);
-
10-11-2006, 07:53 AM #1224Heroes never die

- Registriert seit
- Aug 2006
- Ort
- ...........
- Beiträge
- 1.323
- Points
- 8.645
- Level
- 62
- Downloads
- 0
- Uploads
- 0
lol , ofcourse i know that:ROFL:
Zitat von Lukeson
but 1) it's a bi work
2) you answered that in question 2 , thnxxx
-
10-11-2006, 08:02 AM #1225QJ Gamer Blue
- Registriert seit
- Sep 2006
- Ort
- Germany
- Beiträge
- 216
- Points
- 4.511
- Level
- 42
- Downloads
- 0
- Uploads
- 0
Now I've got a question:
if you open a file of let's say 200byte and you write 50bytes with fprintf, will it just replace the first 50 bytes an leave the rest as it it (I guess so) or will it replace the file with a new one of 50 bytes? And if you write 300bytes into that file, will there be problems, or will he just create a file of 300bytes then?
Thanks, Luke
PS: What the hell does [WiP] actually mean?
PPS: How do I remove a file? remove(filename)?Geändert von Lukeson (10-11-2006 um 08:28 AM Uhr)
-
10-11-2006, 11:07 AM #1226AKA Homer

- Registriert seit
- Jan 2006
- Ort
- Sweden
- Beiträge
- 1.779
- Points
- 12.596
- Level
- 73
- Downloads
- 0
- Uploads
- 0
That depends whether you choose to truncate the file when you open it. I would suggest reading some about the fopen function.
Zitat von Lukeson
WiP = Work in Progress ;)
Not sure if the remove function actually works, but you can use the sdk function sceIoRemove(filepath).
-
10-11-2006, 11:17 AM #1227QJ Gamer Blue
- Registriert seit
- Sep 2006
- Ort
- Germany
- Beiträge
- 216
- Points
- 4.511
- Level
- 42
- Downloads
- 0
- Uploads
- 0
An answer! Thank you!
PS: How do you know so much about the SDK? The only documentation I found sucks and the samples don't include AFAIK no fileIO examples...
-
10-11-2006, 11:29 AM #1228AKA Homer

- Registriert seit
- Jan 2006
- Ort
- Sweden
- Beiträge
- 1.779
- Points
- 12.596
- Level
- 73
- Downloads
- 0
- Uploads
- 0
The includes (.h) in /usr/local/pspdev/psp/sdk/include have some documentation, like right before the functions it says what it does etc.
-
10-11-2006, 12:18 PM #1229QJ Gamer Blue
- Registriert seit
- Sep 2006
- Ort
- Germany
- Beiträge
- 216
- Points
- 4.511
- Level
- 42
- Downloads
- 0
- Uploads
- 0
i c...
-
10-11-2006, 12:27 PM #1230Developer

- Registriert seit
- Sep 2005
- Ort
- Sweden
- Beiträge
- 941
- Points
- 10.075
- Level
- 67
- Downloads
- 0
- Uploads
- 0
All the documentation are stored in usr/local/pspdev/psp/sdk/doc/html. Then just open main.html. I get the feeling that must stuff is documented there.


LinkBack URL
About LinkBacks
Mit Zitat antworten

Hello everyone I am new here and I am glad to be part of this amazing community and I think there...
New to forum