|
From: Matthews <dem...@uw...> - 2003-09-24 22:22:58
|
Thanks a lot Per. It's working perfectly now. And the
> info.input = (char*)malloc(...);
was a mistype by me in the email, which is why I got no errors :p
At 11:19 PM 21/09/2003 +0200, Per Westermark wrote:
>prj_info.input is a pointer to a pointer to char.
>
>If you want to store 10 lines in it:
>
>info.input = (char**)malloc(10*sizeof(char*));
>info.input[0] = (char*)malloc(sizeof(char)*(strlen("mystr1")+1));
>...
>info.input[9] = (char*)malloc(sizeof(char)*(strlen("mystr10")+1));
>
>strcpy(info.input[0],"mystr1");
>...
>strcpy(info.input[9],"mystr10");
>
>Question: Don't you ever try to get the compiler to generate as many
>warnings as possible, and then check up on the warnings?
>
>The compiler would definitely become a bit worried when you do:
> info.input = (char*)malloc(...);
>since you are typecasting to the wrong type.
>
>Take two steps back and think a bit about your code and what you want to
>do. Check up on all data types, and exactly what you allocate.
>
>That little realloc() line you have is very, very interesting. You are
>reallocating the wrong thing - did you intend to reallocate the array
>of pointers to text strings, or a specific string buffer in the array?
>- and you are forgetting that the new pointer is the RETURN VALUE of the
>realloc() call.
>
>/Per W
>
>On Sat, 20 Sep 2003, Matthews wrote:
>
>> Still giving me the same crash :\
>>
>> At 11:32 AM 20/09/2003 +0200, Per Westermark wrote:
>> >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
>> >>
>> >
>> >
>>
>>
>> -------------------------------------------------------
>> 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
>>
>
>
|