I am trying to write a program that will dynamically allocate enough space to store all the words in a 1D char array separated by a space.
ex:
char *literal = "The quick brown fox";
char **words = { "The", "quick", "brown", "fox" };
The program I wrote keeps segfaulting when trying to strncpy(str[buff_ptr],tok,strlen(tok));
I will post my code bellow:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *mutableString(char *lit) {
int size = strlen(lit);
char *str = (char *)malloc(sizeof(char) * size);
strncpy(str, lit, size + 1);
return str;
}
int numTokens(char *str, const char *DELIM) {
char* clone = (char*)malloc(sizeof(char*));
strncpy(clone, str, strlen(str) + 1);
int count = 0;
for (char *tok = strtok(clone, " "); tok != NULL; tok = strtok(NULL, " "))
count++;
free(clone);
return count;
}
char **tokenize(char *str, const char *DELIM) {
printf("tokenize-------------------------\n");
int size = numTokens(str, DELIM);
//allocate space on heap for buffer
char **buff = (char **)malloc(size * sizeof(char *));
//get first word
char *tok = strtok(str, DELIM);
int buff_ptr = 0;
while (tok != NULL) {
strncpy(buff[buff_ptr], tok, strlen(tok) + 1);
printf("buff[%d]%s\n", buff_ptr, buff[buff_ptr]);
//increment to next word for storage
buff_ptr++;
//find next word in string
tok = strtok(NULL, DELIM);
}
for (int i = 0; i < size; i++) {
printf("%s\n", buff[i]);
}
//return 2D pointer
return buff;
}
int main() {
char *literal = "some literal string.";
//convert string to mutable string for strtok
char *str = mutableString(literal);
//set 2D pointer equal to the pointer address returned
char **no_spaces_str = tokenize(str, " ");
printf("%s\n", str);
for (int i = 0; i < numTokens(str, " "); i++) {
printf("%s\n", no_spaces_str[i]);
}
//free heap allocated memory
free(str);
free(no_spaces_str);
return 0;
}
Please see attachment of lldb stack variables:
Within the function mutableString there is dynamically allocated the character array str that does not contain a string
char* mutableString(char* lit){
int size = strlen(lit);
char* str = (char*)malloc(sizeof(char)*size);
strncpy(str,lit,size);
return str;
}
So other functions invoke undefined behavior as for example in this for loop
int numTokens(char* str, const char* DELIM){
int count = 0;
for(; *str != '\0'; str++)
//...
Moreover if the array contained a string nevertheless the function numTokens is incorrect because for example it returns 0 when a passed string contains only one word.
Also in the function tokenize
strncpy(buff[buff_ptr],tok,strlen(tok));
there are used uninitialized pointers buff[buff_ptr] allocated like.
char **buff = (char**)malloc(size*sizeof(char*));
And again you are trying to copy strings without including the terminating zero character '\0; using eth functions strncpy.
So this call in main
printf("%s\n",no_spaces_str[i]);
also will invoke undefined behavior.
This is the corrected version of the code above
When you copying string you should add 1 char for '\0'
int size = strlen(lit)+1;
Tokens buffer size should be size+1
int size = numTokens(str, DELIM)+1;
Strncpy is not required strncpy(buff[buff_ptr], tok, strlen(tok) + 1);
you already copied string char* str = mutableString(literal);
just point to n-th buffer every next token buff[buff_ptr]=tok;
for (int i = 0; i<numTokens(str, " "); i++){
printf("%s\n", no_spaces_str[i]);
}
This code wouldn't work correctly. strtok manipulates the string you pass in and returns a pointer to it, so no memory is allocated.
so all spaces will be replaced by '\0'
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma warning(push)
#pragma warning(disable : 4996)
char* mutableString(char* lit){
int size = strlen(lit)+1;
char* str = (char*)malloc(sizeof(char)*size);
strncpy(str, lit, size);
return str;
}
int numTokens(char* str, const char* DELIM){
int count = 0;
for (; *str != '\0'; str++)
{
if (*str == ' ')
count++;
}
return count;
}
char** tokenize(char* str, const char* DELIM){
printf("tokenize-------------------------\n");
int size = numTokens(str, DELIM)+1;
//allocate space on heap for buffer
char **buff = (char**)malloc((size)*sizeof(char*));
//get first word
char* tok = strtok(str, DELIM);
int buff_ptr = 0;
while (tok != NULL){
buff[buff_ptr]=tok;
printf("buff[%d]%s\n", buff_ptr, buff[buff_ptr]);
//increment to next word for storage
buff_ptr++;
//find next word in string
tok = strtok(NULL, DELIM);
}
for (int i = 0; i<size; i++){
printf("%s\n", buff[i]);
}
//return 2D pointer
return buff;
}
int main(){
char* literal = "some literal string.";
//convert string to mutatable string for strtok
char* str = mutableString(literal);
//set 2D pointer equal to the pointer addres returned
char** no_spaces_str = tokenize(str, " ");
printf("%s\n", str);
for (int i = 0; i<numTokens(str, " "); i++){
printf("%s\n", no_spaces_str[i]);
}
//free heap allocated memory
free(str);
free(no_spaces_str);
return 0;
}
results
tokenize-------------------------
buff[0]some
buff[1]literal
buff[2]string.
some
literal
string.
some
char* mutableString(char* lit){
int size = strlen(lit)+1;
char* str = (char*)malloc(sizeof(char)*size);
strncpy(str,lit,size);
return str;
}
int numTokens(char* str, const char* DELIM){
int size = strlen(str)+1;
char* clone = (char*)malloc(sizeof(char)*size);
strncpy(clone,str,size);
int count = 0;
for(char* tok = strtok(clone," "); tok != NULL; tok=strtok(NULL, " "))
count++;
free(clone);
return count;
}
char** tokenize(char* str, const char* DELIM){
int size = strlen(str)+1;
char* clone = (char*)malloc(sizeof(char)*size);
strncpy(clone,str,size);
// printf("tokenize-------------------------\n");
int size = numTokens(str, DELIM);
//allocate space on heap for buffer
char **buff = (char**)calloc(size,sizeof(char*));
//get first word
char* tok = strtok(clone,DELIM);
int buff_ptr = 0;
while(tok != NULL){
// printf("token%d:%s\n",buff_ptr,tok);
buff[buff_ptr] = (char*)malloc(sizeof(char)*strlen(tok)+1);
strncpy(buff[buff_ptr],tok,strlen(tok)+1);
//increment to next word for storage
buff_ptr++;
//find next word in string
tok = strtok(NULL, DELIM);
}
//return 2D pointer
free(clone);
return buff;
}
int main(){
char* literal = "some literal string.";
//convert string to mutatable string for strtok
char* str = mutableString(literal);
//set 2D pointer equal to the pointer addres returned
char** no_spaces_str = tokenize(str, " ");
int num_words = numTokens(str," ");
char* oneD = (char*)calloc(strlen(str)+1,sizeof(char));
for(int i = 0;i<num_words;i++){
strncat(oneD,no_spaces_str[i],strlen(no_spaces_str[i])+1);
printf("%s\n",oneD);
}
//free heap allocated memory
free(str);
free(no_spaces_str);
free(oneD);
return 0;
}
Is solution to my problem. Thanks to all those who commented and helped me understand dynamic memory better.
Apart from the #Vlad from Moscow mentioned points,
malloc return values must not be type-casted Do I cast the result of malloc?
I tried to clean up the code find the snippet below, DEMO
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct{
char** buff;
int size;
}Array_2d;
char* mutableString(const char* lit){
int size = strlen(lit);
char* str = malloc(size);
strncpy(str,lit,size+1);
return str;
}
int getNextWordLength(const char* str){
int index = 0;
while(*str && (*str != ' ')){
//printf("%c",*str);
++index;
++str;
}
return index;
}
int numTokens(const char* str){
int count = 0;
for(; *str != '\0'; str++)
{
if(*str == ' ')
count++;
}
return count;
}
void tokenize(const char* str, const char *DELIM, Array_2d *array){
int len = strlen(str)+1;
if(!str && !len){
array->buff = 0;
array->size = 0;
}
int number_of_words = numTokens(str)+1;
//allocate space on heap for buffer
char **buff = (char**)malloc(number_of_words*sizeof(char*));
int index = 0;
do{
//get first word
int word_length = getNextWordLength(str);
//To compensate null terminal
buff[index] = malloc(word_length+1);
strncpy(buff[index], str,word_length);
buff[index][word_length+1] = '\0';
str += word_length+1;
++index;
}while(index < number_of_words);
//update return value
array->buff = buff;
array->size = number_of_words;
}
int main(){
char* literal = "hello world this is test";
//convert string to mutatable string for strtok
char* str = mutableString(literal);
printf("Complete String is : %s\n",str);
Array_2d array;
// set 2D pointer equal to the pointer addres returned
tokenize(str, " ",&array);
printf("Tokenized String\n");
for(int i=0;i<array.size;i++){
printf("%s\n",array.buff[i]);
}
free(str);
for(int i =0;i< array.size; ++i)
free(array.buff[i]);
free(array.buff);
return 0;
}
Related
I'm building a word counter program. To achieve this, I was thinking about saving the string the user inputted, and using strtok() to split the sentence with space as the delimiter. But first I want to allocate enough memory for each word. Let's say the sentence is "Hello World". I've already dynamically allocated memory for the string itself. Now I want to split Hello World into 2 strings, "Hello" and "World". My goal is to allocate enough memory so that there's not too much empty space but I also don't want to allocate too little space. Here is my code so far:
#include <stdio.h>
#include <stdlib.h>
char *strmalloc(char **string);
char *user_input = NULL;
char *word_array[];
int main(void) {
printf("Enter a sentence to find out the number of words: ");
user_input = strmalloc(&user_input);
return 0;
}
char *strmalloc(char **string) {
char *tmp = NULL;
size_t size = 0, index = 0;
int ch;
while ((ch = getchar()) != '\n' && ch != EOF) {
if (size <= index) {
size += 1;
tmp = realloc(*string, size);
if (!tmp) {
free(*string);
string = NULL;
break;
}
*string = tmp;
}
(*string)[index++] = ch;
}
return *string;
}
How would I go about doing this? Should I do the splitting first or allocate the space required for the array first?
You can count words without splitting the sentence, here is an example :
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
// Change this to change the separator characters
static inline char isSeparator(char ch) { return isspace(ch) || ispunct(ch); }
char * jumpSeparator(char *string) {
while(string[0] && isSeparator(string[0])) string++;
return string;
}
char * findEndOfWord(char *string) {
while (string[0] && !isSeparator(string[0])) string++;
return string;
}
int countWords(char *string) {
char * ptr = jumpSeparator(string);
if (strlen(ptr) == 0) return 0;
int count = 1;
while((ptr = findEndOfWord(ptr)) && ptr[0]) {
ptr = jumpSeparator(ptr);
if (!ptr) break;
count++;
}
return count;
}
int main() {
char * sentence = "This is,a function... to||count words";
int count = countWords(sentence);
printf("%d\n", count); //====> 7
}
EDIT : Reusing the same functions here is another example that allocates substrings dynamically :
int main() {
char * sentence = "This is,a function... to||split words";
int count = countWords(sentence);
char * ptr = sentence, *start, *end;
char ** substrings = malloc(count * sizeof(char *));
int i=0;
while((ptr = jumpSeparator(ptr)) && ptr[0]) {
start = ptr;
ptr = findEndOfWord(ptr);
end = ptr;
int len = end-start;
char * newString = malloc(len + 1);
memcpy(newString, start, len);
newString[len] = 0;
substrings[i++] = newString;
}
// Prints the result
for(int i=0; i<count; i++) printf("%s\n", substrings[i]);
// Frees the allocated memory
for(int i=0; i<count; i++) free(substrings[i]);
free(substrings);
return 0;
}
Output :
This
is
a
function
to
split
words
I have a variable length string that I am trying to divide from plus signs and study on:
char string[] = "var1+vari2+varia3";
for (int i = 0; i != sizeof(string); i++) {
memcpy(buf, string[0], 4);
buf[9] = '\0';
}
since variables are different in size I am trying to write something that is going to take string into loop and extract (divide) variables. Any suggestions ? I am expecting result such as:
var1
vari2
varia3
You can use strtok() to break the string by delimiter
char string[]="var1+vari2+varia3";
const char delim[] = "+";
char *token;
/* get the first token */
token = strtok(string, delim);
/* walk through other tokens */
while( token != NULL ) {
printf( " %s\n", token );
token = strtok(NULL, delim);
}
More info about the strtok() here: https://man7.org/linux/man-pages/man3/strtok.3.html
It seems to me that you don't just want to want to print the individual strings but want to save the individual strings in some buffer.
Since you can't know the number of strings nor the length of the individual string, you should allocate memory dynamic, i.e. use functions like realloc, calloc and malloc.
It can be implemented in several ways. Below is one example. To keep the example simple, it's not performance optimized in anyway.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
char** split_string(const char* string, const char* token, int* num)
{
assert(string != NULL);
assert(token != NULL);
assert(num != NULL);
assert(strlen(token) != 0);
char** data = NULL;
int num_strings = 0;
while(*string)
{
// Allocate memory for one more string pointer
char** ptemp = realloc(data, (num_strings + 1) * sizeof *data);
if (ptemp == NULL) exit(1);
data = ptemp;
// Look for token
char* tmp = strstr(string, token);
if (tmp == NULL)
{
// Last string
// Allocate memory for one more string and copy it
int len = strlen(string);
data[num_strings] = calloc(len + 1, 1);
if (data[num_strings] == NULL) exit(1);
memcpy(data[num_strings], string, len);
++num_strings;
break;
}
// Allocate memory for one more string and copy it
int len = tmp - string;
data[num_strings] = calloc(len + 1, 1);
if (data[num_strings] == NULL) exit(1);
memcpy(data[num_strings], string, len);
// Prepare to search for next string
++num_strings;
string = tmp + strlen(token);
}
*num = num_strings;
return data;
}
int main()
{
char string[]="var1+vari2+varia3";
// Split the string into dynamic allocated memory
int num_strings;
char** data = split_string(string, "+", &num_strings);
// Now data can be used as an array-of-strings
// Example: Print the strings
printf("Found %d strings:\n", num_strings);
for(int i = 0; i < num_strings; ++i) printf("%s\n", data[i]);
// Free the memory
for(int i = 0; i < num_strings; ++i) free(data[i]);
free(data);
}
Output
Found 3 strings:
var1
vari2
varia3
You can use a simple loop scanning the string for + signs:
char string[] = "var1+vari2+varia3";
char buf[sizeof(string)];
int start = 0;
for (int i = 0;;) {
if (string[i] == '+' || string[i] == '\0') {
memcpy(buf, string + start, i - start);
buf[i - start] = '\0';
// buf contains the substring, use it as a C string
printf("%s\n", buf);
if (string[i] == '\0')
break;
start = ++i;
} else {
i++;
}
}
Your code does not have any sense.
I wrote such a function for you. Analyse it as sometimes is good to have some code as a base
char *substr(const char *str, char *buff, const size_t start, const size_t len)
{
size_t srcLen;
char *result = buff;
if(str && buff)
{
if(*str)
{
srcLen = strlen(str);
if(srcLen < start + len)
{
if(start < srcLen) strcpy(buff, str + start);
else buff[0] = 0;
}
else
{
memcpy(buff, str + start, len);
buff[len] = 0;
}
}
else
{
buff[0] = 0;
}
}
return result;
}
https://godbolt.org/z/GjMEqx
I need help with a couple of things:
I'm trying to delete a word from a pointer and put it in a new pointer with a new length but i am not able to copy it to the new pointer
I'm not sure when should I use the free() function.
when I use the free(str) in the delete function it crashes.
After I copy the "str" to the "newStr" what is the best way to copy the "newStr" back to the "str" with the new length?
Please help me understand it, I'm new with this and I googled it, I tried looking here and didn't find something that could help me.
void delete(char *str)
{
int i, indexStart = 0, indexEnd = 0, wordlen = 0, newLen = 0, len = 0;
printf("Enter the index of the word that you want to remove: ");
scanf("%d", &i);
indexs(i, str,&indexStart,&indexEnd,&wordlen);
len = strlen(str);
newLen = len - wordlen - 1;
char *newStr = (char*)malloc(newLen * sizeof(char));
if (newStr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
for (int j = 0; j < len; j++)
{
if (j< (indexStart - 1) || j > indexEnd)
{
*newStr = *str;
newStr++;
}
str++;
}
free(str);
//free(newStr);
printf("The new string: %s\n", newStr);
}
void main()
{
char *str = (char*)malloc(1 * sizeof(char));
if (str == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
text(str);
if (str != NULL)
{
delete(str);
}
free(str);
system("pause");
}
According to the structured programming paradigm your functions should solve small separate tasks. But your function delete() prints to the console, scans input, allocates new string and fills this new string. But what's event worse is the call exit() in this function. If something went wrong, function must to return an error, but not to stop the program. Also names of functions should relfect what they do.
Use free() for every memory block allocated by malloc().
So, this is a working code:
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
const char *findWord(const char *str, const char *delimiters, unsigned index) {
// validate input parameters
if (!str || !delimiters) {
return NULL;
}
// count words
unsigned count = 0;
while (*str && count < index) {
// skip delimiters
while (*str && !strchr(delimiters, *str)) {
str++;
}
if (*str) {
count++;
}
// skip word
while (*str && strchr(delimiters, *str)) {
str++;
}
}
// if index is too big returns NULL
return *str ? str : NULL;
}
unsigned countLengthOfWord(const char *str, const char *delimiters) {
// validate input parameters
if (!str || !delimiters) {
return 0;
}
// count length
unsigned length = 0;
while (*str && !strchr(delimiters, *str++)) {
length++;
}
return length;
}
char *cutWord(char *str, const char *delimiters, unsigned index) {
// validate input parameters
if (!str) {
return NULL;
}
str = (char *)findWord(str, delimiters, index);
// if index is too big, return NULL
if (!str) {
return NULL;
}
// allocate new string for word
unsigned length = countLengthOfWord(str, delimiters);
char *word = malloc(length + 1);
// check if allocation was successfull
if (!word) {
return NULL;
}
// copy word
strncpy(word, str, length);
word[length] = '\0';
// cut word from string
const char *ptr = str + length;
while (*ptr) {
*str++ = *ptr++;
}
*str = '\0';
return word;
}
int main() {
char str1[] = "Hello, my world!";
char str2[] = "Hello, my world!";
char str3[] = "Hello, my world!";
char *word1 = cutWord(str1, " ,!", 0);
char *word2 = cutWord(str2, " ,!", 1);
char *word3 = cutWord(str3, " ,!", 2);
if (word1) {
printf("word: %s\nstring: %s\n\n", word1, str1);
}
if (word2) {
printf("word: %s\nstring: %s\n\n", word2, str2);
}
if (word3) {
printf("word: %s\nstring: %s\n\n", word3, str3);
}
// release allocated memory
free(word1);
free(word2);
free(word3);
getchar();
return 0;
}
I'm loosing my mind.
I want to split string (char* text) with spaces and insert the string results into array and return this array.
I have the following method in C
char *read_command(char *text)
{
int index=0;
char *res=NULL;
char *command= (char*)malloc(strlen(text)+1);
strcpy(command, text);
char *tok = strtok(command, " ");
while(tok!=NULL && index ==0)
{
res = (char*)realloc(res, sizeof(char)*(index+1));
char *dup = (char*)malloc(strlen(tok)+1);
strcpy(dup, tok);
res[index++] = dup; //Error here
tok = strtok(NULL, " ");
}
res[index++]='\0';
return res;
}
from main method
char *input="read A B C";
char *command = read_command(input);
Thank you
You are using a wrong type to calculate the size in this call:
res = realloc(res, sizeof(char)*(index+1));
You need to use char*, not char, with sizeof, like this:
res = realloc(res, sizeof(char*)*(index+1));
Since your code returns a pointer to C strings (represented as char*) the return type should be char**.
You need to remove the index == 0 condition from the while loop, otherwise it wouldn't go past the initial iteration.
This assignment
res[index++]='\0';
should be
res[index++]=NULL;
You also need to call free(command) before returning the results to the caller. Finally, you should not cast results of malloc in C.
Here is your code after the fixes above:
char **read_command(char *text) {
int index=0;
char **res=NULL;
char *command= malloc(strlen(text)+1);
strcpy(command, text);
char *tok = strtok(command, " ");
while(tok!=NULL) {
res = realloc(res, sizeof(char*)*(index+1));
char *dup = malloc(strlen(tok)+1);
strcpy(dup, tok);
res[index++] = dup;
tok = strtok(NULL, " ");
}
// Need space to store the "terminating" NULL
// Thanks, BLUEPIXY, for pointing this out.
res = realloc(res, sizeof(char*)*(index+1));
res[index]=NULL;
free(command);
return res;
}
Demo on ideone.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **read_command(const char *text){
int index=0;
char **res=NULL;
char *command= malloc(strlen(text)+1);
strcpy(command, text+strspn(text, " \t\n"));//strspn for skip space from top
char *tok = strtok(command, " ");
res = realloc(res, sizeof(char*)*(index+1));
while(tok!=NULL){
res[index++] = tok;
res = realloc(res, sizeof(char*)*(index+1));
tok = strtok(NULL, " ");
}
res[index++]=NULL;
return res;
}
int main(void){
char *input="read A B C";
char **command = read_command(input);
int i;
for(i=0;command[i]!=NULL;++i){
printf("s[%d]=%s\n", i, command[i]);
}
free(command[0]);//for free command of read_command
free(command);//for free res of read_command,,
return 0;
}
I've the following program:
// required include statements...
static char ***out;
char* get_parameter(char* key)
{
char *querystr = /*getenv("QUERY_STRING")*/ "abcdefg=abcdefghi";
if (querystr == NULL)
return (void*)0;
char s[strlen(querystr)] ;
strcpy(s, querystr);
const char delim = '&';
const char delim2 = '=';
static size_t size = 0;
if (out == 0)
{
out = (char*) malloc(sizeof(char*));
size = split(s, &delim, out);
}
int i=0;
for (; i<size; i++)
{
if ((*out)[i] != NULL)
{
char ***iout = NULL;
iout = (char*) malloc(sizeof(char*));
int isize = split((*out)[i], &delim2, iout);
if (isize > 1 && ((*iout)[1]) != NULL && strcmp(key, (*iout)[0]) == 0)
{
size_t _size = strlen((*iout)[1]);
char* value = (char*) malloc(_size*sizeof(char));
strcpy(value, (*iout)[1]);
free(iout);
return value;
}
}
}
return (void*) 0;
}
static size_t count(const char *str, char ch)
{
if (str == NULL) return 0;
size_t count = 1;
while (*str)
if (*str++ == ch) count++;
return count;
}
size_t split(const char *const str, const char* delim, char ***out)
{
size_t size = count(str, *delim);
*out = calloc(size, sizeof(char));
char* token = NULL;
char* tmp = (char*) str;
int i=0;
while ((token = strtok(tmp, delim)) != NULL)
{
tmp = NULL;
(*out)[i] = (char*) malloc(sizeof strlen(token));
strcpy((*out)[i++], token);
}
return size;
}
main()
{
char* val = get_parameter("abcdefg");
printf("%s\n", val); // it should prints `abcdefghi`, but it prints `abcd?`
free(val);
}
as appears in the main method, the function get_parameter should prints abcdefghi, but it prints abcd? where ? is a controls character with value of 17.
Why the reset of string is not printed? I think I mis-used the malloc to allocate appropriate space.
Also, is there any tool that I can use to know the internal representation of memory for my pointers?
You're dealing with C-Strings here. You must consider 1 additional byte for the NULL-termination ('\0')
Therefore:
char s[strlen(querystr)] ;
strcpy(s, querystr);
Is incorrect.
strlen will return 4 for string "abcd" but what you want is to allocate space for "abcd\0"
So you need strlen + 1
The lines
out = (char*) malloc(sizeof(char*));
iout = (char*) malloc(sizeof(char*));
are a problem.
sizeof() returns the number of bytes required to store an object of the given type, in this case, the size of a pointer (to a char). malloc() then allocates that many bytes (apparently 4 bytes on your architecture). To fix this, you need to give malloc the desired string length instead of using sizeof.
Additionally, the line
char* value = (char*) malloc(_size*sizeof(char));
has a completely unnecessary use of sizeof(). sizeof(char) is guaranteed by the standard to be 1.
You should use gdb to run your binary step by step and see what's wrong.
Valgrind is a very good tools, it will tell you what's line overwrite in memory, etc..