this function crashes my program, help pls
This is a discussion on this function crashes my program, help pls within the C++ Programming forums, part of the General Programming Boards category; hi there
this function:
Code:
char* strmerge(const char str1[], char *str2)
{
int x=0, chars=0;
char* str_merged;
while (str1[x]!='
-
this function crashes my program, help pls
hi there
this function:
Code:
char* strmerge(const char str1[], char *str2)
{
int x=0, chars=0;
char* str_merged;
while (str1[x]!='\0')
{
str_merged[chars]=str1[x];
x++;
chars++;
}
x=0;
while (str2[x]!='\0')
{
str_merged[chars]=str2[x];
x++;
chars++;
}
str_merged[chars+1]='\0';
return str_merged;
} crashes my program and i cant seem to figure out why, however if i put a "getch()" anywhere in the function it doesnt crash, for example this:
Code:
char* strmerge(const char str1[], char *str2)
{
int x=0, chars=0;
char* str_merged;
getch();
while (str1[x]!='\0')
{
str_merged[chars]=str1[x];
x++;
chars++;
}
x=0;
while (str2[x]!='\0')
{
str_merged[chars]=str2[x];
x++;
chars++;
}
str_merged[chars+1]='\0';
return str_merged;
} would not crash.
my main just calls this function, like so:
Code:
int main()
{
strmerge("hi ", "there");
} i dont see any reason for this function to crash the program, and even more strange is that it gets fixed with a simple randomply placed getch();
Help pls
-
of course it's gonna crash if you have bad pointers (str_merged), and the getch() simply waits for you to input a character before all hell breaks loose
Also, you could clean your code by changing this condition (str1[x]!='\0') to this one : (str1[x])
Last edited by thefeedinghand; 07-30-2010 at 05:09 PM.
-
the problem is that when i put the getch() the hell doesnt break loose... at all, when i click a key the program ends normaly, i even tryed to put a cout and the function returns the right strings.
-
just allocate some memory for char* str_merged, using new char[]. And change this str_merged[chars+1]='\0'; to this str_merged[chars]='\0'; so the string is properly ended without any garbage characters.
-

Originally Posted by
thefeedinghand
just allocate some memory for char* str_merged, using new char[]. And change this str_merged[chars+1]='\0'; to this str_merged[chars]='\0'; so the string is properly ended without any garbage characters.
Better yet, pass a char *str_merged as argument to your function.
-
Ok, i solved the problem and i think i know why, heres the function now:
Code:
char* strmerge(char *str1, char *str2)
{
int x=0, chars=0;
char str_merged[40];
while (str1[x]!='\0')
{
str_merged[chars]=str1[x];
x++;
chars++;
}
x=0;
while (str2[x]!='\0')
{
str_merged[chars]=str2[x];
x++;
chars++;
}
str_merged[chars]='\0';
return str_merged;
} I think this solved the problem because maybe when i had only the pointer to the first char it would ,at a certain point, overwrite used space on the ram, but correct me if im wrong.
anyway tks for all your help
-

Originally Posted by
shiroaisu
Ok, i solved the problem and i think i know why, heres the function now:
Code:
char* strmerge(char *str1, char *str2)
{
int x=0, chars=0;
char str_merged[40];
while (str1[x]!='\0')
{
str_merged[chars]=str1[x];
x++;
chars++;
}
x=0;
while (str2[x]!='\0')
{
str_merged[chars]=str2[x];
x++;
chars++;
}
str_merged[chars]='\0';
return str_merged;
} I think this solved the problem because maybe when i had only the pointer to the first char it would ,at a certain point, overwrite used space on the ram, but correct me if im wrong.
anyway tks for all your help
that's not going to solve anything because you're returning the address of a local stack variable that just went out of scope. Secondly, what do you think will happen if the combined length of your input strings (plus one for the null terminator) is greater than 40?
Popular pages Recent additions
Similar Threads
-
By AlakaAlaki in forum C++ Programming
Replies: 1
Last Post: 06-27-2008, 11:45 AM
-
By Tonto in forum C++ Programming
Replies: 5
Last Post: 06-19-2007, 02:39 PM
-
By GameGenie in forum C++ Programming
Replies: 9
Last Post: 08-29-2005, 11:21 PM
-
By kron in forum Linux Programming
Replies: 1
Last Post: 11-19-2004, 09:18 AM