Maybe simpe strcpy mem error

Started by
1 comment, last by Anthony Serrano 16 years, 9 months ago
I can't figure out why

strcpy(str[0],str[1]);

causes a memory error. I've looked over arrays and pointers and it seems it should be fine. the first line

strcpy(temp_str,str[0]);

works, but not the second.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"


int main(void) {

    char *str[2];
    char temp_str[10];
    str[0] = (char*)malloc(10*sizeof(char)); 
    str[1] = (char*)malloc(10*sizeof(char));
    str[0] = "TEST 1";
    str[1] = "TEST 2";
    
    printf("BEFORE:\n%s %s\n\n",str[0],str[1]);
    //switch the content of str[0] and str[1]
    strcpy(temp_str,str[0]);
    strcpy(str[0],str[1]);
    strcpy(str[1],temp_str);
    
    printf("AFTER:\n%s %s\n\n",str[0],str[1]);
      
    system("pause");
    return(0);
}

-BourkeIV
Advertisement
You are missunderstanding how strings work:

str[0] = (char*)malloc(10*sizeof(char));
str[1] = (char*)malloc(10*sizeof(char));
str[0] = "TEST 1";
str[1] = "TEST 2";


first you allocate memory for str0 and str1, then you throw away that memory and assign them to constant string literals.

You are not supposed to modify the contents of constant string literals, but due to some legacy issues (left overs from the old C days) you can assign a constant string literal to a non-constant char *: char *t= "TEST";

What you probably want to do is something along the lines of:

str[0] = (char*)malloc(10*sizeof(char));
str[1] = (char*)malloc(10*sizeof(char));
strcpy(str[0], "TEST 1");
strcpy(str[1], "TEST 2");

assuming my function usage is correct you are copying string literals to the memory you allocated, and everything else should work the way you expect it to.

hope this helps

Your code doesn't do what you think it does.

char *str[2];char temp_str[10];str[0] = (char*)malloc(10*sizeof(char)); str[1] = (char*)malloc(10*sizeof(char));str[0] = "TEST 1";str[1] = "TEST 2";

The first four lines of this segment are fine. But then you get to the fifth line, str[0] = "TEST 1";. Now, str[0] is of type char*, and the string literal "TEST 1" is of type char[], so this statement functions as a pointer assignment, which puts the memory address of that string literal into str[0], leaking the memory you previously allocated. Note that string literals are stored in the program's code segment; this becomes important later.

Next, we get to the swap code:
    strcpy(temp_str,str[0]);    strcpy(str[0],str[1]);    strcpy(str[1],temp_str);

Now the first line works just fine, because temp_str is an array of characters located on the stack. But then we get to the next line. As I mentioned above, the ill-formed string assignment has caused str[0] to point to a string literal in the code segment. On many platforms, the code segment is read-only by default, so when strcpy attempts to copy the data to the area pointed to by str[0], you get an error (such as a General Protection Fault of SIGSEGV).

The moral of the story is: In C, strings are not a basic data type, so literals should never be assigned to variables using =. Use strcpy instead.

EDIT: Darn, someone else said almost exactly the same thing while I was typing my response.

This topic is closed to new replies.

Advertisement