C programmers
neomagus00
Posts: 3,899
hey, are there any c programmers out there that want to help a fledgling figure out some funky behavior... in the next 2 hours?
specifically, i have this function:
[code]
void loadDict(char * fileName, int * numWords, char ** dict){
char * word;
FILE * filein = fopen(fileName, "r");
int i;
while(fgetline_113(&word, filein)) {
for (i = 0; i < *numWords; i++)
printf("dict[%d]: %s\n", i, dict);
printf("\n");
dict[*numWords] = word;
(*numWords)++;
free(word);
}
}
fgetline_113 is a provided function that reads the next line from the file and stores it at the provided pointer; this is not the problem. What i'm expecting from this function is that it reads a list of about 7 words and inserts them into the array in order - this is a much simplified version of the actual lab. what i'm actually getting when i print it back out is the current word repeated numWords times, as though it's overwriting the words previously loaded into the dictionary.
dict is a pointer to a list of 50 char*s (malloc'd previously), and fgetline_113 mallocs memory for you and sticks the pointer in 'word', as mentioned... therefore, shouldn't i simply be able to say: element i of dict is assigned this pointer, then when i free it and re-malloc a new pointer, the old one - since it's a copy - should be untouched. instead what seems to be happening is it's copying the address just fine, but then that address is re-used by the next call to malloc... that is, it seems that my array is being filled with many copies of the same pointer...
any ideas??
specifically, i have this function:
[code]
void loadDict(char * fileName, int * numWords, char ** dict){
char * word;
FILE * filein = fopen(fileName, "r");
int i;
while(fgetline_113(&word, filein)) {
for (i = 0; i < *numWords; i++)
printf("dict[%d]: %s\n", i, dict);
printf("\n");
dict[*numWords] = word;
(*numWords)++;
free(word);
}
}
fgetline_113 is a provided function that reads the next line from the file and stores it at the provided pointer; this is not the problem. What i'm expecting from this function is that it reads a list of about 7 words and inserts them into the array in order - this is a much simplified version of the actual lab. what i'm actually getting when i print it back out is the current word repeated numWords times, as though it's overwriting the words previously loaded into the dictionary.
dict is a pointer to a list of 50 char*s (malloc'd previously), and fgetline_113 mallocs memory for you and sticks the pointer in 'word', as mentioned... therefore, shouldn't i simply be able to say: element i of dict is assigned this pointer, then when i free it and re-malloc a new pointer, the old one - since it's a copy - should be untouched. instead what seems to be happening is it's copying the address just fine, but then that address is re-used by the next call to malloc... that is, it seems that my array is being filled with many copies of the same pointer...
any ideas??
It's not good, very fundamentally simply not good. - geolemon
"Its not good enough until we have real-time fearmongering. I want my fear mongered as it happens." - Shizelbs
"Its not good enough until we have real-time fearmongering. I want my fear mongered as it happens." - Shizelbs
Post edited by RyanC_Masimo on
Comments
-
It's been a long time since I programed in C, however are you sure you are not loading the array correctly, but not incrementing to the next element in the arrary when you print?DKG999
HT System: LSi9, LSiCx2, LSiFX, LSi7, SVS 20-39 PC+, B&K 507.s2 AVR, B&K Ref 125.2, Tripplite LCR-2400, Cambridge 650BD, Signal Cable PC/SC, BJC IC, Samsung 55" LED
Music System: Magnepan 1.6QR, SVS SB12+, ARC pre, Parasound HCA1500 vertically bi-amped, Jolida CDP, Pro-Ject RM5.1SE TT, Pro-Ject TubeBox SE phono pre, SBT, PS Audio DLIII DAC -
yep - i figured it out... i was copying a pointer into the array, then immediately free-ing and reusing the pointer! remove the free() call, and it all works out... w00t!It's not good, very fundamentally simply not good. - geolemon
"Its not good enough until we have real-time fearmongering. I want my fear mongered as it happens." - Shizelbs -
Well, its been a while that I have coded in C and I hate pointers, I belive your problem lies in following statements:
while(fgetline_113(&word, filein)) -- you dont need to send &word, as word is already address type. try changing this stmt to
while(fgetline_113(word, filein))
you are printing from Dict before storing in it.
dict[*numWords] = word;
free(word);
you are making a shalow copy of word to dict. When the memory is actually cleaned you will lose it from dict also.-izafar
Goldenear Technology Triton 1 - Benchmark AHB2 - Benchmark LA4 - Auralic Vega - Auralic Aries Mini - Marantz TT-15S1 - Clearaudio Nano -
I just set up a recursive loop to delete everything, then there are no worries!
-
For your next project, I would suggest C++. vectors are nicer to work with
-
I'm guessing that this a little above my Computer Programming in BASIC that I took back in high school.
BDTI plan for the future. - F1Nut -
Kris Siegel wrote:For your next project, I would suggest C++. vectors are nicer to work withIt's not good, very fundamentally simply not good. - geolemon
"Its not good enough until we have real-time fearmongering. I want my fear mongered as it happens." - Shizelbs -
neomagus00 wrote:i would if i could... the course is taught in strict ansi '89 C (dunno why, maybe it's cause we eventually write malloc() ? )
It's a very useful thing. Knowing C++ is obviously great, but if you know the basic ANSI C very well everything else is just gravy. Fundamentals are key to being a good programmer.
Nearly everything I do has to be in ANSI C becauase i write firmware and low-level stuff that has to be as efficient as possible.If you will it, dude, it is no dream. -
C still has its place. C++ has its benefits but you really need to do OO stuff to get it out. I have done C++/VS/MFC for about 8 years now and currently talking to a company about a position that would take me back to ANSI C world (CVI/Labview). Sure I will miss a lot of stuff from C++ but for this application C is the right tool.
-
well then, i guess it's nice to know i'm not learning a dead languageIt's not good, very fundamentally simply not good. - geolemon
"Its not good enough until we have real-time fearmongering. I want my fear mongered as it happens." - Shizelbs -
neomagus00 wrote:well then, i guess it's nice to know i'm not learning a dead language
-
if you guys were having this conversation in a bar or club, you'd have to beat the chicks off with a stick....LOLLiving Room 2 Channel -
Schiit SYS Passive Pre. Jolida CD player. Songbird streamer. California Audio Labs Sigma II DAC, DIY 300as1/a1 Ice modules Class D amp. LSi15 with MM842 woofer upgrade, Nordost Blue Heaven and Unity interconnects.
Upstairs 2 Channel Rig -
Prometheus Ref. TVC passive pre, SAE A-205 Amp, Wiim pro streamer and Topping E50 DAC, California Audio Labs DX1 CD player, Von Schweikert VR3.5 speakers.
Studio Rig - Scarlett 18i20(Gen3) DAW, Mac Mini, Aiyma A07 Max (BridgedX2), Totem Mites -
More like beat the chicks with the stick to get them to go home with them.
-
i dunno, i think heaps and pointers are pretty sexy conversation topics...It's not good, very fundamentally simply not good. - geolemon
"Its not good enough until we have real-time fearmongering. I want my fear mongered as it happens." - Shizelbs -
The internet was the best thing to happen to geeks and nerds, because now we all are, just to different extents.
-
ohskigod wrote:if you guys were having this conversation in a bar or club, you'd have to beat the chicks off with a stick....LOL
-
neomagus00 wrote:well then, i guess it's nice to know i'm not learning a dead language
-
cool, good luck and enjoyIt's not good, very fundamentally simply not good. - geolemon
"Its not good enough until we have real-time fearmongering. I want my fear mongered as it happens." - Shizelbs -
#include <stdio.h> int main(void) { int count; for(count=1;count<=500;count++) printf("I will not throw paper airplanes in class.\n"); return 0; }
-
#include <iostream> int main(){ for (unsigned int i = 0; i < 500; ++i){ std::cout << "I will not throw paper airplanes in class." << std::endl; } }
Much nicer (C++)
Though, 500 endls is a bit overboard since endl also flushes the buffer. Probably would be better to use \n on all of them but the last one. -
the c++ ive used doesn't require the std: prefix?
i still like the 'dangerous' lifestyle of C... i mean, honestly, who doesn't want to worry about buffer overflow and dangling pointers?
well, i suppose C++ has the same thing, but...It's not good, very fundamentally simply not good. - geolemon
"Its not good enough until we have real-time fearmongering. I want my fear mongered as it happens." - Shizelbs -
neomagus00 wrote:the c++ ive used doesn't require the std: prefix?
i still like the 'dangerous' lifestyle of C... i mean, honestly, who doesn't want to worry about buffer overflow and dangling pointers?
well, i suppose C++ has the same thing, but...
There's some kind of directive you have to give somewhere so you don't have to use the std:: prefix. "using std" or something like that.
And people who actually care about low-level performance don't touch C++.If you will it, dude, it is no dream. -
yeah, it's "#include <iostream> using namespace std;", i forgot about that bit... but why does C++ lose the low-level performance? isn't it just a superset of C?It's not good, very fundamentally simply not good. - geolemon
"Its not good enough until we have real-time fearmongering. I want my fear mongered as it happens." - Shizelbs -
You just have less control over a lot of things, and there's a lot more overhead. In order to make things easier for the programmer, the compiler has to do all the work, and who really knows what it's doing?If you will it, dude, it is no dream.
-
neomagus00 wrote:the c++ ive used doesn't require the std: prefix?neomagus00 wrote:i still like the 'dangerous' lifestyle of C... i mean, honestly, who doesn't want to worry about buffer overflow and dangling pointers?
well, i suppose C++ has the same thing, but...bobman1235 wrote:There's some kind of directive you have to give somewhere so you don't have to use the std:: prefix. "using std" or something like that.bobman1235 wrote:And people who actually care about low-level performance don't touch C++.bobman1235 wrote:You just have less control over a lot of things, and there's a lot more overhead. In order to make things easier for the programmer, the compiler has to do all the work, and who really knows what it's doing?
Also, it's not the compiler doing the work per say. Yeah it has to deal with classes and namespaces which the C compiler didn't, but the majority of the easy functions (if you want to call them that but templated, nested vectors and queues can get complicated) are contained in the STL libraries. -
Kris got it right, C++ can do the stuff C can. The "inefficiency", overhead, comes only when you start using the advances C++ has over C (OO vs. procedural). But using C++ without OO is just using C with C++ syntax. Sure you can use C++ libraries that are OO but if your program isn't designed to be OO then it kinda loses its point.
-
Sami wrote:Kris got it right, C++ can do the stuff C can. The "inefficiency", overhead, comes only when you start using the advances C++ has over C (OO vs. procedural). But using C++ without OO is just using C with C++ syntax. Sure you can use C++ libraries that are OO but if your program isn't designed to be OO then it kinda loses its point.
Exactly. Saying C++ can do everything C can do is just a silly thing to say. I wasn't referring to writing C code and compiling it with a C++ compiler, I was referring to doing an object oriented approach using C++ over a more basic approach with C. Object oriented stuff is going to have more overhead, because you're hiding the guts of the work in all the C++ libraries, which means more nested function calls, which means less efficiency.If you will it, dude, it is no dream. -
bobman1235 wrote:Exactly. Saying C++ can do everything C can do is just a silly thing to say. I wasn't referring to writing C code and compiling it with a C++ compiler, I was referring to doing an object oriented approach using C++ over a more basic approach with C. Object oriented stuff is going to have more overhead, because you're hiding the guts of the work in all the C++ libraries, which means more nested function calls, which means less efficiency.
-
neomagus00 wrote:well then, i guess it's nice to know i'm not learning a dead language
Heh, dead...no way.
Biggest mistake I ever made was learning to 'program' using Perl and other interpreted languages. I skipped over fundamentals that I have an impossible time trying to discipline myself to learn in C now.
You learn plain old ANSI C well now and you can use anything else later on with impunity.
Wait til later on when you see the atrocities that 'programmers' that never grasped the fundamentals commit. I'm talking about things like enterprise apps where they buy bigger and bigger hardware to run a Java app thinking it's too big and powerful for smaller hardware...but when audited you find two OPENs for a single CLOSE on a database connect. I'm serious too, this crap is very very common.
I audited a particular 'app' that was processing logs for audit reports not long ago. The original was taking 3 hours to run and they had setup massive swap partitions for it to work. Took me an hour to see they overlooked a fundamental issue of how to do their sorting. By simply using hash tables I got it to run in a few _minutes_ and it would run on a PC if we wanted it to, let alone a $50k Sun machine.
I'm no programmer, I'll stick to sysadmin thanks very much, but most of my job security comes from lousy programmers killing a system.
Learn your ANSI C well and you'll have work until yer dead.Harman Kardon AVR-435 Receiver
Polk RTi6 (L/R) CSi3 (Center) RM3000 (SL/SR)
SVS 25-31 PCi (Sub)