Raised This Month: $ Target: $400
 0% 

Dynamic String Allocation


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Mysterious
Junior Member
Join Date: Apr 2009
Old 02-12-2010 , 13:26   Dynamic String Allocation
Reply With Quote #1

Hello,

i wanted to concatenate two strings but i had to allocate the memory on my own but the server crashed all the time.
I am using the standard serverplugin_empty as template and tried these ways:
Code:
1. char *lol = (char *)calloc(strlen(term)+2,sizeof(char));
   free(lol);
2. char *lol = new char[strlen(term)+2];
    delete lol;
Both ways already fail on allocation.
Actually they don't fail but the memory access creates some errors.
Symptoms are:
- The memory usage raises really fast.
- memory raise stops at about 90.000K
- crash: "The command at "0x11507b19" refers to the memory at "0x00000010". The operation "read" couldn't be executed.

Am I so incredible stupid or is something going badly wrong?
I guess I am the fault.
Please help me:>

Yours
Mysterious
Mysterious is offline
recon0
Veteran Member
Join Date: Sep 2007
Location: US
Old 02-12-2010 , 21:56   Re: Dynamic String Allocation
Reply With Quote #2

You're doing something seriously wrong...

PHP Code:
char *newStr = new char[strlen(str1) + strlen(str2)];
strcpy(newStrstr1);
strcat(newStrstr2); 
__________________
recon0 is offline
Keeper
Senior Member
Join Date: Nov 2006
Old 02-12-2010 , 22:05   Re: Dynamic String Allocation
Reply With Quote #3

I actually tried your code in one of my plugins and it worked fine. I had string sizes up to a 1MB. Never crashed. I'm betting it's not the allocation, but more likely something to do with the concatenation ( since it's a read error ). I would use recon0's method. Also, make sure you test lol ... if it had an issue allocating the memory it would return NULL.

Last edited by Keeper; 02-12-2010 at 22:08.
Keeper is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 02-13-2010 , 06:00   Re: Dynamic String Allocation
Reply With Quote #4

Also make sure you're not doing this every frame on large strings without ever freeing memory.
__________________
hello, i am pm
PM is offline
Mysterious
Junior Member
Join Date: Apr 2009
Old 02-13-2010 , 15:48   Re: Dynamic String Allocation
Reply With Quote #5

Oh my god >.<
I did a stupid mistake:

Code:
        if(g_SHPtr->GetStatus() == MRES_OVERRIDE)
        {
        }
        else{
            if(strlen(META_RESULT_ORIG_RET(const char*)) >6)
            {
                if(checkpathstart(META_RESULT_OVERRIDE_RET(const char*),"models")){}
            }
        }
I copy-pasted some code. It easy to understand that it's impossible to use a value that doesn't exist...
So it wasn't about memory allocation.

Sorry for bothering you^^
and thanks anyways.
Mysterious is offline
Chrisber
AlliedModders Donor
Join Date: Jul 2007
Location: localhost
Old 02-13-2010 , 18:52   Re: Dynamic String Allocation
Reply With Quote #6

It must be delete[], not delete on arrays.

Chris
Chrisber is offline
Mysterious
Junior Member
Join Date: Apr 2009
Old 02-14-2010 , 06:40   Re: Dynamic String Allocation
Reply With Quote #7

Oh!
Thank you Chrisber.
Mysterious 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 20:25.


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