-3

what I wanna to do is get space separated words into a 2d array.what I am thinking is replace space by a '\0',so I can copy the string to array.And then add the string pointer to the place after '\0'.keeping do it till the last.But when I done it ,I consider how can I free the pointer.I consider maybe I can save it first so I use char *t = s.But when I free the t I get a segment error.so how should i free the s pointer after it move to another place. beside this,I also have some questions: 1.after I malloc a sizeof(char)*15 memory ,I expect printf strlen(s) I can print 15,but I get 0,why?If I wanna know the size of s now,how should I do? 2.after strcpy str to s,the strlen s become 11.But I malloc size of 15,where will the left memory go? does that effect program?

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(){

    char buf[10][5];
    char *str = "10 8 6 9 76";
    char *s;
    char *t = s;

    s = (char *)malloc(sizeof(char)*15);
    printf("strlen 1 s:%d\n",strlen(s));

    memset(s,0x00,sizeof(char)*15);
    printf("strlen 2 s:%d\n",strlen(s));

    strcpy(s,str);

    int n = strlen(s);
    int i = 0;
    int j = 0;
    int k = 0;


    for( i = 0; i < n; i++ ){
        if( s[i] == ' '){
            s[i] = '\0';
            strcpy( buf[k], s);
            s += (i+1);
            k++;
            n = n - i - 1;
            i = 0;
        }
    }
    printf("s:%s\n",s);
    strcpy( buf[k], s );


    for( j = 0; j<= k; j++ ){
        printf("buf[%d]:[%s]\n",j,buf[j]);
    }
    printf("j:%d\n",j);

    free(t);
    return 0;
}
Hhdzhu
  • 49
  • 1
  • 7

1 Answers1

3

The main problem is that you are assigning s to t before allocating memory for s:

char *t = s;

and then you try to free t which point to an un-initialised memory.

If you compile your code with -Wall you will get a warning.

You should set t = s after the memory allocation of s.

Also, always check the return value of malloc (there is no need to cast it):

s = malloc(sizeof(char)*15);
if (s == NULL) {
   // Error!
   return 1
}
terence hill
  • 3,354
  • 18
  • 31
  • 1
    While learning C it is always a good idea to enable warnings `-Wall -Wextra`. It prevents a lot of errors. – Boiethios Mar 30 '16 at 12:23
  • Yes and maybe also `-pedantic` – terence hill Mar 30 '16 at 12:24
  • @terence hill Hi,could you help me out of the confusing:after I malloc pointer s sizeof 15,I strcpy a length of 11 to it.what about the left memory unused? – Hhdzhu Mar 30 '16 at 12:36
  • ... and `-Wconversion`! – alk Mar 30 '16 at 12:46
  • @Hhdzhu The memory left is still there, nothing happens to that memory. – terence hill Mar 30 '16 at 12:48
  • @terence hill excuse me,could you do me a favor once more?why after malloc s of size 15,the strlen of s is 0? and is there a good way to find some pointer unfreed? If I assigning s to t after allocating memory for s,I can actually free that memory by free t,I am not sure about this,because they are two different pointers.... – Hhdzhu Mar 30 '16 at 12:55
  • @Hhdzhu `strlen` counts the elements before the terminating character `\0`. The value of the terminating character is `0` so that strlen actually stops when reads a zero. This means that the first two calls to strlen in your code are meaningless. You cannot count the elements in a newly allocated string, you have to check the return value of malloc. The elements in a zeroed strings are zero. (See this for the terminating char: http://stackoverflow.com/questions/16955936/string-termination-char-c-0-vs-char-c-0) – terence hill Mar 30 '16 at 13:11
  • @Hhdzhu. If you assign `s`to `t` then `t`points to the same memory as `s`. If you free `t`you are actually freeing the memory pointed by both `t`and `s`. – terence hill Mar 30 '16 at 13:13