|
From: Per W. <pw...@ia...> - 2003-09-20 09:33:24
|
When you allocate buffers for strings, you must allocate room for
strlen(str)+1
characters, or the termination character '\0' will not fit.
/Per W
On Fri, 19 Sep 2003, Matthews wrote:
> I'm working on a program to generate makefiles for me, and I currently have
> it set up to read different sections of a file to generate the options.
> One section is %input, which has a list of filenames to include, eg.
>
> prj.txt
>
> %input
> main.c
> vid.c
> gui.c
>
> I read these into a struct organized like so:
>
> struct prj_info
> {
> ...
> char **input;
> ...
> }
>
> And I allocate space based on how many files there are (an array of
> strings, basically)
>
> My problem is that I get errors when running the following C code:
>
> if(!strncmp("%include",t,strlen(t)-1))
> {
> count = 0;
> skip_line(buf);
> getword(buf,t);
> longest_length = strlen(t);
> info.input = (char *)malloc((sizeof(char)*longest_len)*1);
> if(!info.input)
> {
> printf("OH NOS\n");
> return 0;
> }
> //CRASH HERE
> strcpy(info.input[count],t);
> while(*buf!='%')
> {
> while(*buf=='#'||isspace(*buf)) skip_line(buf);
> getword(buf,t);
> if(strlen(t)>longest_length) longest_length = strlen(t);
> count++;
> realloc(info.input,(sizeof(char)*longest_length)*count);
> if(!info.input)
> {
> printf("ERR0RZ\n");
> return 0;
> }
> strcpy(info.input[count],t);
> skip_line(buf);
> }
> }
>
> Buf is the dumped contents of a file (like prj.txt), getword() places a
> word from buf into t and skip_line() skips to the next line in buf.
>
> The code compiles fine, but crashes at the first strcpy(). Any help would
> be appreciated.
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|