I usually use references to pass anything that is not a trivial type. And that's how it should be done.
I don't see how vectors and strings would be the only special cases.
I need to use Pi in a C++ program so i though the easiest thing would be to use a double and it should keep 16 decimals if im not wrong?
Just wanted to make sure its better to use a double thn a float for this right?
[QUOTE=Richy19;24986432]I need to use Pi in a C++ program so i though the easiest thing would be to use a double and it should keep 16 decimals if im not wrong?
Just wanted to make sure its better to use a double thn a float for this right?[/QUOTE]
There's no good answer to this question. It depends on what kind of precision you need.
And the number of decimals isn't static, it depends on what number you're representing.
EDIT: But yes doubles are more precise, but nullsquared / insert 3d guy here could probably tell you about cases where doubles are slower and should be avoided in X part of a graphics rendering engine. Or maybe I'm wrong and it really doesn't matter anymore.
I'm trying to send messages over TCP using sockets. The server is written in C, and if the message is over 1024 bytes it only sends the first 1024 bytes, and then it needs to call send() again to send the rest of the message.
I can handle that, but how can the client determine if a message is split into pieces? Technically, the message doesn't have to split at 1024, it can split at 512 if it really wants to, so I can't just say that if recv() receives 1024 bytes, that I need to read again to get the rest of the message. It's possible to get two blocks of 100 bytes.
[QUOTE=gparent;24986545]There's no good answer to this question. It depends on what kind of precision you need.
And the number of decimals isn't static, it depends on what number you're representing.
EDIT: But yes doubles are more precise, but nullsquared / insert 3d guy here could probably tell you about cases where doubles are slower and should be avoided in X part of a graphics rendering engine. Or maybe I'm wrong and it really doesn't matter anymore.[/QUOTE]
Oh this wont be used in rendering of any sort just simply converting angles to radians and vice versa
[QUOTE=PvtCupcakes;24986769]I'm trying to send messages over TCP using sockets. The server is written in C, and if the message is over 1024 bytes it only sends the first 1024 bytes, and then it needs to call send() again to send the rest of the message.
I can handle that, but how can the client determine if a message is split into pieces? Technically, the message doesn't have to split at 1024, it can split at 512 if it really wants to, so I can't just say that if recv() receives 1024 bytes, that I need to read again to get the rest of the message. It's possible to get two blocks of 100 bytes.[/QUOTE]
TCP connections are best thought of as streams. The calls to send and recv are just writes and reads, and the amount of calls doesn't affect the other end in any way. Hence there is no concept of a message unless you implement it in your protocol.
[QUOTE=jA_cOp;24987109]TCP connections are best thought of as streams. The calls to send and recv are just writes and reads, and the amount of calls doesn't affect the other end in any way. Hence there is no concept of a message unless you implement it in your protocol.[/QUOTE]
Is there an easy or simple way to do a protocol like that? Since theres no way of knowing how many bytes are going to be sent before calling send I can't really send something to the client saying "the next 3 messages belong together"
What would you guys say is the best reference book for C++?
You know how many bytes you [i]want[/i] to send. Whether it has to be split up into multiple send() calls is irrelevant. You can send the message length followed by the message body using as many send() calls as you need to accomplish that, and the other end can look at the length and then do recv() calls repeatedly until it's gotten that many bytes.
[QUOTE=Wyzard;24987352]You know how many bytes you [i]want[/i] to send. Whether it has to be split up into multiple send() calls is irrelevant. You can send the message length followed by the message body using as many send() calls as you need to accomplish that, and the other end can look at the length and then do recv() calls repeatedly until it's gotten that many bytes.[/QUOTE]
Doh. :doh:
I feel so dumb now.
[QUOTE=PvtCupcakes;24987300]Is there an easy or simple way to do a protocol like that? Since theres no way of knowing how many bytes are going to be sent before calling send I can't really send something to the client saying "the next 3 messages belong together"[/QUOTE]
You start every message with a byte representing the type of the message. You also define a structure containing all the information that would be sent for each message type.
The receiver reads the first byte and knows how much information is being sent and what format it's in.
[editline]10:33PM[/editline]
This approach works best for fixed-length messages. If you've got a variable-size string or something, do what Wyzard said.
Remember that the data from a recv() call might span the end of one message and the beginning of the next, so after you process a complete message, don't just clear your buffer — only remove the part you actually processed.
[editline]06:42PM[/editline]
If you're doing variable-length messages with a length field, also remember to account for the possibility of the length field itself being split across recv() calls; you know its size a priori but you still need to check that you have enough bytes before you read its value. (If your length field is one byte then you don't need to worry about this, of course.)
C++ is hard as hell to learn for someone with next to none experience in coding for me. Should I try something easier, or stick through it even though it's really rough? It's really rewarding when I finally get something, but also really frustrating when I don't.
Depends what is it you want o accomplish at the end of it
[QUOTE=Richy19;24988538]Depends what is it you want o accomplish at the end of it[/QUOTE]
I know for a fact I want to learn C++ eventually, since I wish to pursue game development as my job when I graduate. However, I've also been contemplating Java, and C# I heard was good too. Python seems nice, but not really for what I am into.
Is there a function in the C library that takes two strings, concatenates them, and puts the result into a buffer that's malloc'ed to the right length to hold the result of the concatenation?
[QUOTE=shill le 2nd;24993531]Is there a function in the C library that takes two strings, concatenates them, and puts the result into a buffer that's malloc'ed to the right length to hold the result of the concatenation?[/QUOTE]
[cpp]
strcat(realloc(str1, strlen(str1)+strlen(str2)+1), str2);
[/cpp]
That should do the trick.
Oh, forgot about realloc! Thanks. The only problem is that that still doesn't really work for what I'm trying to do, but I'm slowly realizing it's pretty much impossible. I'm trying to convert some PHP to C without expanding one line into many, but I'm realizing that C really doesn't work as a functional language when you're using strings. The problem with your code in my situation is you still have to refer to str2 twice, once to get the length and again to actually pass it to strcat, but in my case str2 is the temporary result of a function I'm calling to concatenate two other strings (long story), so I can't really refer to it twice. Also, I'm realizing that I can't do what I'm doing since I'll never be able to free the pointer I'm passing, which I've malloc'ed in the function, if I pass it to another function without naming it. So yeah, C is not a functional language.
[QUOTE=turb_;24993570][cpp]
strcat(realloc(str1, strlen(str1)+strlen(str2)+1), str2);
[/cpp]
That should do the trick.[/QUOTE]
That would be a possible memory leak and segfault.
realloc() doesn't necessarily give you the same pointer that you put in. If it isn't possible to allocate a larger block of memory in the same spot, it might free the data at str1 and return a new allocation.
You need to do
[cpp]
str1 = realloc(str1, strlen(str1)+strlen(str2)+1);
strcat(str1, str2);
[/cpp]
Yeah, nevermind, I don't even need to realloc if I'm gonna be using multiple lines anyway. I was trying to push C to be a functional language but it's not gonna work.
I can probably use C++ string objects to do what I wanna do, and get the result using .c_str().
You could write a few helper functions if you really need to make it work somewhat like a functional language.
[cpp]#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char** fancy_strcat(char **str1, char* str2) {
*str1 = realloc(*str1, *str1 ? strlen(*str1)+strlen(str2)+1 : strlen(str2)+1);
strcat(*str1, str2);
return str1;
}
int main() {
char *helloworld = NULL;
fancy_strcat(fancy_strcat(fancy_strcat(&helloworld, "Hello "), "World"), "!");
printf("%s\n", helloworld);
return 0;
}
[/cpp]
It's not the prettiest, but it works.
That's diabolically genius.
(Too bad I already switched to C++ strings)
Thank you :science:
[editline]13:37[/editline]
Oh :eng99:
Well it's probably the right choice. Doing anything with strings is a huge pain in the ass in C, and C++ strings are probably a lot easier to work with.
[QUOTE=ROBO_DONUT;24993721]realloc() doesn't necessarily give you the same pointer that you put in. If it isn't possible to allocate a larger block of memory in the same spot, it might free the data at str1 and return a new allocation.[/quote]
I realise that.
[quote]
You need to do
[cpp]
str1 = realloc(str1, strlen(str1)+strlen(str2)+1);
strcat(str1, str2);
[/cpp][/QUOTE]
Fair enough, I assumed that he just wanted to discard str1 and str2 afterwards. I probably should have stuck a free(str2) in there.
[QUOTE=Zally13;24988598]I know for a fact I want to learn C++ eventually, since I wish to pursue game development as my job when I graduate. However, I've also been contemplating Java, and C# I heard was good too. Python seems nice, but not really for what I am into.[/QUOTE]
I would recommend python as a beginners language: what is your particular problem with it?
[QUOTE=Zally13;24988598]I know for a fact I want to learn C++ eventually, since I wish to pursue game development as my job when I graduate. However, I've also been contemplating Java, and C# I heard was good too. Python seems nice, but not really for what I am into.[/QUOTE]
I would say learn C# to get the jist of OOP and then move into C++
[QUOTE=turb_;24996416]I realise that.[/QUOTE]
I was thrown off by the fact that you didn't re-assign str1. I should've known what you meant, but I totally forgot that strcat returns the first argument. So if you add "str1 = " to the beginning of your example it works perfectly.
[QUOTE=turb_;24996416]Fair enough, I assumed that he just wanted to discard str1 and str2 afterwards. I probably should have stuck a free(str2) in there.[/QUOTE]
It's probably better that you didn't. free()ing str2 will cause an error if it wasn't allocated by malloc()/realloc(), so it makes things less flexible.
Hi I want to move this for loop into the timer1_Tick event, how would I go about this
C#
[cpp] for (int i = 0; i < racers.Length; i++)
{
racers[i].returnnewpostion();
racers[i].xcord = racers[i].xcord + racers[i].returnnewpostion();
g.DrawEllipse(myPen, racers[i].xcord, racers[i].ycord, 10, 10);
}
}
public void timer1_Tick(object sender, EventArgs e)
{
}[/code]
[/cpp]
[QUOTE=Richy19;24986432]I need to use Pi in a C++ program so i though the easiest thing would be to use a double and it should keep 16 decimals if im not wrong?
Just wanted to make sure its better to use a double thn a float for this right?[/QUOTE]
Some people tend to do the following:
[code]
#define Pi 3.141592
[/code]
[QUOTE=Mr. Kobayashi;25002292]Hi I want to move this for loop into the timer1_Tick event, how would I go about this
C#
[cpp] for (int i = 0; i < racers.Length; i++)
{
racers[i].returnnewpostion();
racers[i].xcord = racers[i].xcord + racers[i].returnnewpostion();
g.DrawEllipse(myPen, racers[i].xcord, racers[i].ycord, 10, 10);
}
}
public void timer1_Tick(object sender, EventArgs e)
{
}[/code]
[/cpp][/QUOTE]
By pasting it into the timer_tick method ?
Sorry, you need to Log In to post a reply to this thread.