Hey all out there!
I am a kind of new to C coming from Python and Bash
(if bash is a real programming language ) and have a header file to work on String arrays/lists with big size. It's working pretty fine, excepted that my constructor is'nt working. Somtimes it returns
a structure and somtime not.
If it returns no structure I get a segfault and the programs is killed by the Kernel.
I also asked some friends, but they also don't know what the problem is.
In the file in the Appendix are only the relevant parts.
By the way:
- I am using gcc 4.8.x
- On Ubuntu GNU/Linux 14.04 x86_64
- I tried also on Arch (GNU) Linux, but it didn't work, too
Code:#include<stdlib.h> #include<string.h> #include<stdio.h> /* the code is documentation enough */ void exitMemoryError(void) { printf("\n\n\n\tWARN: ERROR: OUT OF MEMORY!\n\n\n"); exit(-105); } /* contains a string, kind of data element */ struct String { char *value; unsigned int len; }; struct String * newString(char * str) { struct String * string=malloc(sizeof(struct String)); if(! string) { exitMemoryError(); } string->value=str; string->len=strlen(str); return string; }