7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
And do you think the NULL pointer points at a character array?
And for that matter, you're using &line, which means that the string is being put into the memory address of line (not where line points!), but you've only got room for maybe three letters there, not to mention you won't ever get them back out, since how do you plan to reinterpret line as a char instead of a char*?
You can't store any information in an ordinary char* pointer that isn't pointing at allocated memory. You should either use an array, or dynamically allocate some memory for the pointer to point to.
I haven't read the rest of this thread, but since you have free(), I'm guessing you want to use the dynamic memory approach. Try something like this
[edit] It seems a few posts appeared while I was typing this one up. [/edit]Code:char *line = malloc(256);
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
You're doing wrong again. Twice, in fact.
Did you read the link I gave you? It should give insight as to why this fails.Code:char* line = NULL; size_t line_length; int chars_read = getline(&line, &line_length, fr);
If you DID read it and didn't understand something, then ask.
doesn't actually get line fills the line for us?
Yes, getline() should fill in the pointer for you.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
If we assume the example here is a working one, then it will:
http://linux.die.net/man/3/getline
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
yeah..matsp.. thanks for pointing that out.. that's the getline what I mean
sorry guys, I should have mentioned it to you before... so why do I keep get a seg fault even though that's right?
So we'll keep reading:
1. When we get here, I don't see how rule.target is set.Code:if (line[0] != '\t'){ if (rule.target != NULL){ rule.dependency = dep; rule.buildcmd = bld; rule.depSize = depIndex + 1; rule.bldSize = bldIndex + 1; HashTableInsert(t, rule.target, &rule); RuleDestruct(&rule); } char* pch = strtok(line, ":"); int h; for (h = 0; pch != NULL; h++){ if (h == 0){ rule.target = pch; printf("Target %s\n", pch); bldIndex = 0; depIndex = 0; }
2. In your for-loop, I don't see how pch is going to change. (You don't change it inside the loop.) Edit: I lost a brace -- so found where pch changes; never mind.
okay rule.target is set? what do you mean? how can I set that?
and it seems that I am missing a free(line) here, but don't know where to put..
my point here is actually just to create a temporary rule which I can put into the HashTable.. do I have to do a malloc first? if yes then how much space do I have to malloc?
Last edited by -EquinoX-; 04-14-2008 at 03:15 PM.
Well, I don't know. Reading the code, this is the first time I ever saw rule.target, and right away we're comparing it to something. Who knows what value is stored in there? I'm not sure what's going on here -- it appears that you're deleting the old rule maybe; but there's no guarantee that rule.target is NULL the first time you get to this point. So it's quite possible you're following garbage pointers here.
okay fixed that already, next