Archived

This topic is now archived and is closed to further replies.

SDL Segmentation Fault

This topic is 5012 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

Howzit When I try to concat strings in C++ using SDL with the strcat() function, it bombs out and throws a segmentation fault. What could cause this? Is there another way to concat strings, char*? Shot

Share this post


Link to post
Share on other sites
SDL has nothing to do with that; it just catches the exception. More than likely what is happening is you are writing past the end of your destination string. Make sure the destination string (the first character pointer passed) has enough room allocated to hold the final string, including the 0 character at the end. Also, make sure both source and dest are initially properly NULL terminated as well.


Golem
Blender--The Gimp--Python--Lua--SDL
Nethack--Crawl--ADOM--Angband--Dungeondweller

Share this post


Link to post
Share on other sites
But how''s that going to work? I have a constructor that accepts two strings. What I want to do then is concat them both together.
I don''t know the length of the strings.

Share this post


Link to post
Share on other sites
Why not do something like this:

int strlen1 = strlen(firststring);
int strlen2 = strlen(secondstring);

char *newstring = new char[strlen1 + strlen2 + 1];

strcpy(newstring, firststring);
strcpy(&newstring[strlen1], secondstring);


I think that''ll work; I wrote that off the top of my head.

Game Development Wiki

Share this post


Link to post
Share on other sites
Normally that''s true, but if you''re talking about my post, I don''t think it''d really matter in this case since newstring is created using the lengths of the two strings being copied to it.

IRL you''d want to check the values of strlen1 and strlen2 in case of infinite strings.

Game Development Wiki

Share this post


Link to post
Share on other sites
Of course, if the OP really wants to do it right, since he is using C++ he should use std::string, and avoid all of the assorted unsafe garbage related to using C-style character arrays. When in Rome, do as the Romans do...

[edited by - VertexNormal on March 29, 2004 3:55:40 PM]

Share this post


Link to post
Share on other sites