I have written a program for concatenation two strings, and it is throwing segmentation fault during run time at line s1[i+j] = s2[j], in for loop..... And i am not able to figure out, why it is happening so.... Kindly coreect me, where am i going wrong.
char* concatenate(char *s1, char *s2)
{
int i,j=0;
for(i=0; s1[i] != '\0'; i++);
for(j=0; s2[j] != '\0'; j++)
{
s1[i+j] = s2[j];
}
s1[i+j] = s2[j];
return s1;
}
char *s1 = (char *) malloc(15);;
char *s2 ;
s1 = "defds";
s2 = "abcd";
s1 = concatenate(s1,s2);
// printf("\n\n%s\n\n",s1);
s1 = "rahul";
This line does not copy the string "rahul" into the buffer pointed to by s1; it reassigns the pointer s1 to point to the (not modifiable) string literal "rahul"
You can get the desired functionality by using your concatenate function twice:
char *s1 = (char *) malloc(15);
s1[0] = '\0'; // make sure the buffer is a null terminated string of length zero
concatenate(s1, "rahul");
concatenate(s1, "bagai");
Note that the concatenate function is still somewhat unsafe since it blindly copies bytes, much like strcat does. You'll want either to be very sure that the buffer you pass it is large enough, or to modify it to take a buffer length like strncat takes.
When you do s1 = "rahul"; you are overwriting the memory you just allocated. This line doesn't copy "rahul" to the malloc'ed area, it changes s1 to point to the string constant "rahul" and throws away the pointer to the malloc'ed memory.
Instead, you should use strcpy to copy the string to the malloc'ed area:
// s1 = "rahul";
strcpy(s1, "rahul");
This will fix your call to concatenate since s1 will now point to the correct 15-byte area of memory.
Alternatively, you could eschew the dynamic allocation and allocate+assign the initial string all at once:
char s1[15] = "rahul";
That will allocate 15 bytes on the stack and copy "rahul" into that space. Note a subtlety here. In this case it is in fact correct to use =, whereas it is incorrect when s1 is declared as char *s1.
One important debugging lesson you can learn from this is that when your program crashes on a particular line of code that doesn't mean that's where the bug is. Often you make a mistake in one part of your program and that mistake doesn't manifest itself in a crash until later on. This is part of what makes debugging such a delightfully frustrating process!
You don't have to write your own string concatenation, there are already functions in the standard libary to do this task for you!
#include <string.h>
char *strcat(char *dest, const char *src);
char *strncat(char *dest, const char *src, size_t n);
In the second piece of code, you allocate a buffer of size 15 and then assign it to s1. Then you assign "rahul" to s1, which leaks the memory you just allocated, and assigns s1 to a 6-byte piece of memory that you likely cannot write to. Change s1 = "rahul"; to strcpy( s1, "rahul" ); and you might have better luck.
I agree with the other answers though, the concatenate function is dangerous.
Related
I'm writing my own strcpy due to the fact that the default one in string.h only accept a const char * as a source string to copy from.
I'm trying this very basic prototype (yes, the return isn't meaningful, I'm just trying things):
int copyStrings(char * dest, char * source){
int i=0;
while(source[i]!='\0'){
dest[i]=source[i];
i++;
}
dest[i]='\0';
return 0;
}
and it gives me SIGSEGV, Segmentation Fault error in gdb, at the line dest[i]=source[i], right at the first character. I'm pretty sure dest[i] isn't a string literal, so I should be able to modify it.
What am I doing wrong?
EDIT: here's the calling
int main(){
char * str = (char*)malloc((int)sizeof(double));
char * str2 = (char *)malloc((int)sizeof(int));
str = "hello";
str2 = "hey jude";
copyStrings(str2, str);
free(str);
free(str2);
return 0;
}
This is assigning a string literal to str2 - the very thing that you claim you aren't doing. This is actually the cause of your segfault.
str2 = "hey jude";
It also is causing a memory leak as prior to this, you malloc'd some memory and assigned it to str2 as well. But not enough memory to hold the string. Typically an int is 4 bytes and you need 9 bytes to store that string.
What you want to do is this, which allocates as many bytes as there are in the string, plus an extra one to store the \0 terminating character at the end.
str2 = malloc(strlen("hey jude")+1);
strcpy(str2,"hey jude");
or on some systems you can use POSIX function strdup() which effectively does the job of the above in one handy function call.
str2 = strdup("hey jude");
Let's go at it line by line and see where it goes wrong:
int main(){
char * str = (char*)malloc((int)sizeof(double));
char * str2 = (char *)malloc((int)sizeof(int));
str = "hello";
str2 = "hey jude";
copyStrings(str2, str);
free(str);
free(str2);
return 0;
}
int main(){ - this is an improper definition of main. Should be int main(int argc, char **argv)
char * str = (char*)malloc((int)sizeof(double)); - defines str, then allocates (probably) 8 bytes of memory and assigns its address to str. malloc takes a size_t argument, so the cast (int)sizeof(double) is incorrect. Also, in C the return value of malloc should never be cast. So this line should be char * str = malloc(sizeof(double));
char * str2 = (char *)malloc((int)sizeof(int)); - all the same problems as the preceding line. Should be char *str2 = malloc(sizeof(int));
str = "hello"; - causes a memory leak, because the memory you JUST ALLOCATED two lines earlier is now irretrievably lost. You've got two options here - either don't allocate the memory when defining str or free it first. Let's do the latter:
free(str);
str = "hello";
str2 = "hey jude"; - same problem, similar solution:
free(str2);
str2 = "hey jude";
copyStrings(str2, str); - here you're telling your routine to copy the constant string "hello" over the top of the constant string "hey jude". This will work fine on some systems, but will blow up on other systems. The question is in the treatment of the constant string "hey jude". If it's stored in modifiable memory the code will work just fine. If it's stored in memory which is marked as being unmodifiable, however, it will blow up. It seems that the latter is the case on your system. To fix this you probably want to go back to the previous line and change it to
str2 = malloc(20);
That's more memory than you'll need, but it will work just fine.
free(str); - you're attempting to free the constant string "hello", which is not dynamically allocated memory. This needed to be done prior to the assignment str = "hello";.
free(str2; - same problem as above. This needed to be done prior to the assignment str2 = "hey jude";.
} - correct
Best of luck.
I have a const char* string, I want to copy that string character by character to dynamic `char*.
const char *constStr = "Hello world";
char *str = (char*) malloc(strlen(constStr)+1);
while(*constStr){
*str = *constStr;
constStr++;
str++;
}
printf("%s", str);
free(str);
The problem is that previous code just copies each character of constStr to only the first index of the str. I don't know why?
As others have pointed out, you are incrementing str pointer in each iteration, so you always end up printing the end of the string.
You can instead iterate over each character without incrementing the pointer. The following code worked for me:
const char *constStr = "Hello world";
int len = strlen(constStr);
char *str = (char *) malloc(len + 1);
int i;
for (i = 0; i <= len; ++i) {
str[i] = constStr[i];
}
printf("%s", str);
free(str);
Yes you didn't null terminate the string. That was the primary problem. To be more clear, it is not that you didn't nul terminate the string which is the problem but rather your use of them where a pointer to a nul terminated char array is expected is the problem. But even if you did there was significant amount of problems in the code.
You allocated the memory and the casted the return value of malloc which is unnecessary. void* to char* conversion is implicitly done.
malloc might not be able to service the request, it might return a null pointer. It is important to
check for this to prevent later attempts to dereference the null pointer.
Then you started copying - you copied everything except the NUL terminating character. And then you passed it to printf's %s format specifier which expects a pointer to a null terminated char array. This is undefined behavior.
The one position, in the str is uninitialized - beware that accessing uninitialized value may lead to undefined behavior.
Also there is another problem, From standard ยง7.22.3.3
The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.
Yes so is is the case here? No. when you called free(str) str is not pointing to the dynamically allocated memory returned by the malloc. This is again undefined behavior.
The solution always is to keep a pointer which stores the address of the allocated chunk. The other answers already showed them (without repeating them - both of them provides a good solution).
You can use strdup or strcpy also - even if you don't need them now - get accustomed with them. It helps to know those. And yes strdup is not part of standard, it is a POSIX standard thing.
Example:
const char *constStr = "Hello world";
char *str = malloc(strlen(constStr)+1);
if( !str ){
perror("malloc");
exit(EXIT_FAILURE);
}
char *sstr = str;
while(*constStr){
*str = *constStr;
constStr++;
str++;
}
*str = 0;
printf("%s", sstr);
free(sstr);
Here's the "classical" string copy solution:
const char *constStr = "Hello world";
char *str = malloc(strlen(constStr) + 1), *p = str;
/* Do not forget to check if str!=NULL !*/
while((*p++ = *constStr++));
puts(str);
The problem is that previous code just copies each character of
constStr to only the first index of the str. I don't know why?
Use index variable.
Don't forget terminating '\0' because you have a good chance of segmentation fault.
I'm trying to make a strcpy function from scratch for a class. While I could do it with a for loop and copy the individual characters, I think I could just make a swap using malloc and pointers to make it more efficient. Here's my code, but I've been getting a lot of confusing errors.
void notStrcpy(char s1[], char s2[]) { //copies string s1 into s2
char *s3 = (char *) malloc(strlen(s1)); //s3 is now an alias of s1
s2 = *s3;} //dereference s3 to dump s1 into s2
Why is this happening, and is there any way to make this code work the way I intended it?
You cannot do that: strcpy expects both chunks of memory to be ready - one for reading the string, and the other one for writing the string. Both addresses are expected to have enough memory for the actual content of a null-terminated C string.
On the other hand, malloc gives you a third chunk of memory (you need to allocate strlen(s)+1, but that's a completely different story). String copy algorithm has no use for that chunk of memory. On top of that, assigning parameters of a function has no effect on the values passed into your function, so s2 = *s3 is not doing what you think it should be doing.
Long story short, while ((*s1++ = *s2++)); is your simplest strcpy implementation.
Note: malloc could come in handy in an implementation of string duplication function, e.g. strdup. If you decide to give it a try, don't forget to allocate space for null terminator.
#dasblinkenlight Thank you. My new code is as follows:
void totallyNotstrcpy(char s1[], char s2[]) {
int x = 0;
while (x < strlen(s1)+1) {
s2[x] = s1[x];
x++;
}
}
As a quick side question, how does your code snippet work? Doesn't the while loop need a condition?
I have written the following program (it is given as an example in one of the best text books). When I compile it in my Ubuntu machine or at http://www.compileonline.com/compile_c_online.php, I get "segmentation fault"
The problem is with while( *p++ = *str2++)
I feel it is a perfectly legal program. Experts, please explain about this error.
PS: I searched the forum, but I found no convincing answer. Some people even answered wrong, stating that *(unary) has higher precedence than ++ (postfix).
#include <stdio.h>
#include <string.h>
int main()
{
char *str1= "Overflow";
char *str2= "Stack";
char *p = str1;
while(*p)
++p;
while( *p++ = *str2++)
;
printf("%s",str1);
return 0;
}
Thanks
str1 and str2 point to string literals. You aren't allowed to modify those. Even if you could, there isn't enough memory allocated for the string to hold the characters you're trying to append. Instead, initialize a sufficiently large char array from a string literal:
char str1[14] = "Overflow";
I feel it is a perfectly legal program.
Unfortunately, it is not. You have multiple, severe bugs.
First of all, you are creating pointers to string literals char *str1= "Overflow"; and then you try to modify that memory. String literals are allocated in read-only memory and attempting to write to them results in undefined behavior (anything can happen).
Then you have while(*p) ++p; which looks for the end of the string, to find out where to append the next one. Even if you rewrite the pointers to string literals into arrays, you don't have enough free memory at that location. You must allocate enough memory to hold both "Overflow" and "Stack", together with the string null termination.
You should change your program to something like this (not tested):
#include <stdio.h>
int main()
{
char str1[20] = "Overflow"; // allocate an array with enough memory to hold everything
char str2[] = "Stack"; // allocate just enough to hold the string "Stack".
char *p1 = str1;
char *p2 = str2;
while(*p1)
++p1;
while(*p1++ = *p2++)
;
printf("%s",str1); // should print "OverflowStack"
return 0;
}
Or of course, you could just #include <string.h> and then strcat(str1, str2).
Because you are crossing the string boundary of Overflow (str1) is why you are getting sigsegv.
str1 does not have enough memory allocated to accomodate beyond Overflow.
I am writing a function which expands the string, str1 and stores it as str2. By expansion, I mean if str1 has "a-d", it should be stored in str2 as "abcd". I have written the following code. I get a debug error that stack around the variable str1 is corrupted.
Can someone please point out what's going wrong?
Thanks.
#include <stdio.h>
void expand(char s1[], char s2[]);
int main() {
char s1[] = "Talha-z";
char s2[] = "";
expand(s1, s2);
printf(s2);
}
void expand(char s1[], char s2[]) {
int i = 0;
int j= 0;
int k, c_next;
while ( s1[i] != '\0') {
switch (s1[i]) {
case ('-') :
c_next = s1[i+1];
for ( k = 1; k < c_next; k++) {
s2[j] = s1[i] + k;
j++;
}
break;
}
i++;
j++;
}
s2[j] = '\0';
}
You are not allocating sufficient memory for your target string (s2). But you are attempting to write to it, which means you will be writing into memory that you don't own, causing the corruption.
You will need to use dynamic allocation for s2 (i.e. by using malloc), but you will first need to calculate how much memory you need.
char s2[] = "";
This is equivalent to writing
char s2[1] = { '\0' };
It cannot hold more than a single character (or none at all, if the NUL terminator is required).
The problem is that when you initialize s2, you give it enough room for 1 character (i.e. the null terminating '\0'). Thus when you write into s2:
s2[j] = ...
there are no guarantees about what memory you're writing into.
To allocate memory for s2 dynamically, you need to use malloc. In other words, you need to figure out how much memory is required (i.e. by finding the length of the expanded string) and then give s2 that much memory, and finally fill it in via the procedure you have written.
The string s2 at present is on the stack for local variables for main() and is allocated only one byte for one character. When you call the function, it gets passed stack addresses for s1 and s2. The code is over-writing whatever is next to s2 on the stack of function main(). Hence, the error. Please use dynamic memory allocation as already suggested by Mr. Oli above.
Hope my explanation helps you.