Jump to content
  • Advertisement
Sign in to follow this  
_WeirdCat_

String To Char* :(

This topic is 849 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So i convert lets say its a std::string to char *

 

anyway debugger claims its there, but whenever i try to get value from char* it says its null and i get access violation 000000

 

 

i think its because of * in a void parameter anyway heres a code:

 

typedef std::string AnsiString;

void main()
{
int p;
char * u = 0;
AnsiString pk = "R";
CopyToPCHAR(pk, &p, u);



ShowMessage(AnsiString(u,p)); //error

}


template <class type> void DeleteIfPersist(int * len, type * p)
{
 if (p != 0) delete [] p;
 p = 0;
 (*len) = 0;
}

inline void CopyToPCHAR(AnsiString str, int * len, char * p /* i think heres a problem */)
{
DeleteIfPersist(len, p);

(*len) = str.Length();

if ((*len) > 0)
{
p = new char[ (*len) ];
strcpy(p, str.c_str());
}

}


so what is going on?

Share this post


Link to post
Share on other sites
Advertisement
CopyToPCHAR needs to take either a pointer or reference to char*, otherwise all changes are done to local variables and lost on return.

Share this post


Link to post
Share on other sites

p = new char[ (*len) ];


That's not enough space to store the string and a terminating zero.



if ((*len) > 0)


What's the point of that condition? What would you like to happen if str is an empty string? Edited by Álvaro

Share this post


Link to post
Share on other sites

Thanks all changing to:

inline void CopyToPCHAR(AnsiString str, int * len, char *& p) solved the case

 

Alvaro my program by default uses char* without null terminator

[spoiler]

 

 

i have another question because i have similar problem:

template <class type> void Copycptr(const int * s, type * sp, int * d, type *& dp) //changed it like above from type * dp
{
DeleteIfPersist(d, dp);
(*d) = (*s);
int k = (*d);

if (k <= 0) return;
dp = new type[ k ];
for (int i=0; i < k; i++) dp[i] = sp[i]; //and i have error here with the same access violation 00000 thing
}

i try to copy with that function both int * or char *

 

 

for ints

 

Copycptr(&in.qual_len, in.qualID, &qual_len, qualID);

where qual_len is int

 

and int * qualID;

 

for chars

 

Copycptr(&in.stanowisko_len, in.stanowisko, &stanowisko_len, stanowisko);

int stanowisko_len

 

char * stanowisko;

 

 

boths in. structure is 

 

employee& operator=(const employee& in)

 

 

 

and crap it may be something else, andi changed whole application base code, to use that and didint check it till now x_X

 

[/spoiler]

 

 

Spoiler is fixed because i was trying to copy pointer1 to pointer1 instead of pointer1 to pointer2 :/

 

Copycptr(&in.ulen[month], uwagi[month], &ulen[month], uwagi[month]); where i had to write:

 

Copycptr(&in.ulen[month], in.uwagi[month], &ulen[month], uwagi[month]);

 

 

And alvaro i just wrote that if statement because if theres an empty string to copy = do nothing. (all pointers and lengths are zeroed)

Edited by WiredCat

Share this post


Link to post
Share on other sites

 

 

That's not enough space to store the string and a terminating zero.

 

 

Just got that error, luckily you mentioned that.

 

a bucket of kittens for you :P

 

bucket-of-kittens-featured.jpg

Edited by WiredCat

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!