Buy Direct M-F 9am - 10:30pm EST 1-866-764-1801

Vist our Online Store
+ Reply to Thread
Results 1 to 30 of 30

Thread: C programmers

  1. #1

    Member Sales Rating: (0)

    Join Date
    Nov 2003
    Location
    Dayton, OH
    Posts
    3,459

    Default C programmers

    hey, are there any c programmers out there that want to help a fledgling figure out some funky behavior... in the next 2 hours? :p

    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[i]);
    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

  2. #2

    Member Sales Rating: (19)

    Join Date
    Aug 2003
    Location
    Northern IL/Central IA
    Posts
    5,469

    Default

    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

  3. #3

    Member Sales Rating: (0)

    Join Date
    Nov 2003
    Location
    Dayton, OH
    Posts
    3,459

    Default

    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

  4. #4

    Member Sales Rating: (2)

    Join Date
    Aug 2001
    Location
    CT
    Posts
    739

    Default

    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

    HT/Gaming
    ------------

    RTi6 Mains
    CSi30 Center
    Fxi 3 Surrounds
    PSW 505 Sub
    ONKYO TX-SR605
    Sony PlayStation 3 BR Player/Gaming
    Nintendo Wii Gaming
    Samsung HP-T4254 Plasma


    Headphone Gear
    -------------------

    Ultrasone Pro 900 Headphones
    Little Dot 1+ Headamp
    Musical Fidelity Vdac 2 with PS3 Power Supply
    Squeezebox Touch

  5. #5
    webdude
    Member Sales Rating: (0)

    Join Date
    Jan 2002
    Location
    Nebraska... that's in the USA, right?
    Posts
    1,980

    Default

    I just set up a recursive loop to delete everything, then there are no worries!

  6. #6
    Former Web Intern
    Member Sales Rating: (0)

    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    311

    Default

    For your next project, I would suggest C++. vectors are nicer to work with :)

  7. #7

    Member Sales Rating: (7)

    Join Date
    Jul 2001
    Location
    lookin' for fava beans and a nice chianti
    Posts
    12,404

    Default

    I'm guessing that this a little above my Computer Programming in BASIC that I took back in high school.

    BDT
    I ALWAYS use an ass-gasket. Never hover because of splash down and back splatter. I also float landing pad made from TP for a soft landing to avoid the above. One can never be too cautious when dealing with the general public. - RonP

  8. #8

    Member Sales Rating: (0)

    Join Date
    Nov 2003
    Location
    Dayton, OH
    Posts
    3,459

    Default

    Quote Originally Posted by Kris Siegel
    For your next project, I would suggest C++. vectors are nicer to work with :)
    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 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

  9. #9

    Member Sales Rating: (14)

    Join Date
    Jun 2004
    Location
    Live Free or Die
    Posts
    10,965

    Default

    Quote Originally Posted by neomagus00
    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.

  10. #10

    Member Sales Rating: (13)

    Join Date
    Mar 2004
    Location
    Bedford, TX
    Posts
    4,502

    Default

    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.

  11. #11

    Member Sales Rating: (0)

    Join Date
    Nov 2003
    Location
    Dayton, OH
    Posts
    3,459

    Default

    well then, i guess it's nice to know i'm not learning a dead language :p
    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

  12. #12

    Member Sales Rating: (13)

    Join Date
    Mar 2004
    Location
    Bedford, TX
    Posts
    4,502

    Default

    Quote Originally Posted by neomagus00
    well then, i guess it's nice to know i'm not learning a dead language :p
    You just have to remember that while C and C++ are related, they are very different. You'll see the same syntax in a lot of places but the fundamentals are different. Some say it is better to learn C++ without knowing C but that's not necessarily true as long as you understand what makes C++ different.

  13. #13

    Member Sales Rating: (5)

    Join Date
    Jun 2003
    Location
    northern nj
    Posts
    6,594

    Default

    if you guys were having this conversation in a bar or club, you'd have to beat the chicks off with a stick....LOL :D
    Theater - Polk LSi15, LSiC, LSi9 speaks, DIY Sub (142.5L box, SVS plus driver, 500 Watt plate amp)...Outlaw 990 pre/pro, Carver TFM45, 2 X Outlaw M200 . Rotel RB980 . PS3, Monster 3600 power center

    2 Channel -
    Anthem Pre 2l, Jolida JD100 CD player, CAL Sigma DAC, Carver m4.0, Polk LSi9 w external modified Crossovers (thanks Trey!)

  14. #14

    Member Sales Rating: (2)

    Join Date
    May 2003
    Location
    Sconnie
    Posts
    11,808

    Default

    More like beat the chicks with the stick to get them to go home with them. :p

  15. #15

    Member Sales Rating: (0)

    Join Date
    Nov 2003
    Location
    Dayton, OH
    Posts
    3,459

    Default

    i dunno, i think heaps and pointers are pretty sexy conversation topics... :D
    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

  16. #16

    Member Sales Rating: (2)

    Join Date
    May 2003
    Location
    Sconnie
    Posts
    11,808

    Default

    The internet was the best thing to happen to geeks and nerds, because now we all are, just to different extents. ;)

  17. #17

    Member Sales Rating: (13)

    Join Date
    Mar 2004
    Location
    Bedford, TX
    Posts
    4,502

    Default

    Quote Originally Posted by ohskigod
    if you guys were having this conversation in a bar or club, you'd have to beat the chicks off with a stick....LOL :D
    But why this conversation at a bar? I don't need to talk, just rely on my good looks... :D

  18. #18

    Member Sales Rating: (13)

    Join Date
    Mar 2004
    Location
    Bedford, TX
    Posts
    4,502

    Default

    Quote Originally Posted by neomagus00
    well then, i guess it's nice to know i'm not learning a dead language :p
    Just an update, I think I will be starting at my new job Monday morning. Offer is good, job interesting and has plenty of responsibility so I think I will take it. :D

  19. #19

    Member Sales Rating: (0)

    Join Date
    Nov 2003
    Location
    Dayton, OH
    Posts
    3,459

    Default

    cool, good luck and enjoy :p
    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

  20. #20
    webdude
    Member Sales Rating: (0)

    Join Date
    Jan 2002
    Location
    Nebraska... that's in the USA, right?
    Posts
    1,980

    Default

    Code:
    #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;
    }

  21. #21
    Former Web Intern
    Member Sales Rating: (0)

    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    311

    Cool

    Code:
    #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++) :D

    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.
    Last edited by Kris Siegel; 04-14-2006 at 03:32 AM.

  22. #22

    Member Sales Rating: (0)

    Join Date
    Nov 2003
    Location
    Dayton, OH
    Posts
    3,459

    Default

    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

  23. #23

    Member Sales Rating: (14)

    Join Date
    Jun 2004
    Location
    Live Free or Die
    Posts
    10,965

    Default

    Quote Originally Posted by neomagus00
    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.

  24. #24

    Member Sales Rating: (0)

    Join Date
    Nov 2003
    Location
    Dayton, OH
    Posts
    3,459

    Default

    yeah, it's "#include 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

  25. #25

    Member Sales Rating: (14)

    Join Date
    Jun 2004
    Location
    Live Free or Die
    Posts
    10,965

    Default

    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.

  26. #26
    Former Web Intern
    Member Sales Rating: (0)

    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    311

    Default

    Quote Originally Posted by neomagus00
    the c++ ive used doesn't require the std: prefix?
    std is a namespace which contains all of the STL functions. It's a way to make sure nothing has the same name.
    Quote Originally Posted by neomagus00
    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...
    C++ has pointers and the buffer can also be overflowed. However, you can use C++'s auto_ptr instead of a regular pointer which automatically destroys itself (frees the memory) if the programmer forgets or it goes out of scope. It has regular pointers as well.
    Quote Originally Posted by bobman1235
    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.
    using namespace std;
    Quote Originally Posted by bobman1235
    And people who actually care about low-level performance don't touch C++. :)
    C++ can do anything C can do. It's fairly low level with some high-level functionality. Programmers usually choose C when developing drivers and kernels because it has a much smaller library to port/develop on.
    Quote Originally Posted by bobman1235
    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?
    You don't have less control. If you choose, you can use all of the C functions you want, though the STL functions a very efficient and fast so I'm not sure why you wouldn't want to use them.

    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.

  27. #27

    Member Sales Rating: (13)

    Join Date
    Mar 2004
    Location
    Bedford, TX
    Posts
    4,502

    Default

    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.

  28. #28

    Member Sales Rating: (14)

    Join Date
    Jun 2004
    Location
    Live Free or Die
    Posts
    10,965

    Default

    Quote Originally Posted by Sami
    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.

  29. #29

    Member Sales Rating: (13)

    Join Date
    Mar 2004
    Location
    Bedford, TX
    Posts
    4,502

    Default

    Quote Originally Posted by bobman1235
    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.
    That's exactly where the saying different tools for different jobs applies here. C does well for low level stuff, C++ is great for complex and large systems. I used to work in embedded software side before and after doing Windows software, it is now also moving into C++ and OO as the softwares are getting larger due to more memory and processor power. Nokia's Symbian OS is a good example of this.

  30. #30

    Member Sales Rating: (0)

    Join Date
    Jan 2006
    Posts
    116

    Default

    Quote Originally Posted by neomagus00
    well then, i guess it's nice to know i'm not learning a dead language :p
    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.
    Last edited by kingsqueak; 04-15-2006 at 01:03 PM.
    Harman Kardon AVR-435 Receiver
    Polk RTi6 (L/R) CSi3 (Center) RM3000 (SL/SR)
    SVS 25-31 PCi (Sub)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts