Raised This Month: $ Target: $400
 0% 

[SOLVED] Dynamic srtring size?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
fakuivan
Senior Member
Join Date: Nov 2015
Old 11-19-2015 , 22:46   [SOLVED] Dynamic srtring size?
Reply With Quote #1

Hi there!

I know that you can asign a dynamic array size like this int[] numbers = new int[somevar], so I tried to do the same with a string (1 byte array???) like this:

Code:
				stringsize = ((SQL_FetchSize(hQuery, 0)) + 1);
				ReplyToCommand(client, "[TFTDB] length: %i", stringsize);
				char[] phrase = new char[stringsize];
				SQL_FetchString(hQuery, 0, phrase, strlen(phrase));
				ReplyToCommand(client, "[TFTDB] phrase: %s", phrase);
				ReplyToCommand(client, "[TFTDB] strlen(phrase): %i", strlen(phrase));
and got this
Code:
[TFTDB] length: 29
[TFTDB] phrase:
[TFTDB] strlen(phrase): 0
I didn't used sizeof() because the compiler replaces it with a number when compiled. Why is this not working?


Last edited by fakuivan; 11-20-2015 at 00:16. Reason: solved
fakuivan is offline
fakuivan
Senior Member
Join Date: Nov 2015
Old 11-20-2015 , 00:15   Re: Dynamic srtring size?
Reply With Quote #2

SOLVED

The problem was the strlen() function that for some reason was returning 0. I changed the strlen(phrase) func by the stringsize int returned by the db.

Code:
				stringsize = ((SQL_FetchSize(hQuery, 0)) + 1);
				char[] phrase = new char[stringsize];
				SQL_FetchString(hQuery, 0, phrase, stringsize);
				ReplyToCommand(client, "[TFTDB] length: %i", stringsize);
				ReplyToCommand(client, "[TFTDB] phrase: %s", phrase);
				ReplyToCommand(client, "[TFTDB] strlen(phrase): %i", strlen(phrase));
Output:

Code:
[TFTDB] length: 29
[TFTDB] phrase: Si lo construyes, el morirá
[TFTDB] strlen(phrase): 28	//worked now for some reason

Last edited by fakuivan; 11-20-2015 at 00:15. Reason: :crab:
fakuivan is offline
Impact123
Veteran Member
Join Date: Oct 2011
Location: Germany
Old 11-20-2015 , 01:32   Re: [SOLVED] Dynamic srtring size?
Reply With Quote #3

The reason strlen returned 0 is because the string was empty. SQL_FetchString couldn't fill it because you told it to put 0 chars (return value of strlen as 3rd param) at max into it, hence it stayed that way. What's the reason you don't just declare a big enough buffer to hold the string?
__________________

Last edited by Impact123; 11-20-2015 at 02:42.
Impact123 is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 11-20-2015 , 11:06   Re: [SOLVED] Dynamic srtring size?
Reply With Quote #4

I think you misunderstand strlen
Miu is offline
fakuivan
Senior Member
Join Date: Nov 2015
Old 11-20-2015 , 23:28   Re: [SOLVED] Dynamic srtring size?
Reply With Quote #5

Quote:
Originally Posted by Impact123 View Post
The reason strlen returned 0 is because the string was empty. SQL_FetchString couldn't fill it because you told it to put 0 chars (return value of strlen as 3rd param) at max into it, hence it stayed that way. What's the reason you don't just declare a big enough buffer to hold the string?

Got it, strlen iterates over the string untill it finds a null terminator.

The thing is that the smallest phrase in the db is only 2 characters long and the biggest one is 1142 characters long (untill valve decides to put a phrase longer than the current one), don't know if that is a problem yet so I just used the dynamic string size :v.

btw what if I declare a string using decl and do strlen(theNonTerminatedString)?
fakuivan is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 11-21-2015 , 00:34   Re: [SOLVED] Dynamic srtring size?
Reply With Quote #6

It might crash the server because strlen pretty much counts all the bytes up to the null terminator (0), and decl has no null terminator.

You should not be using decl anyway. The SM devs don't recommend it, it never should have existed.
Potato Uno is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 05:57.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode