Can't dump 2D arrays

This is a discussion on Can't dump 2D arrays within the C Programming forums, part of the General Programming Boards category; Hello community. I need some help with my C program... spent hours and still couldn't figure out what's wrong. I'm ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    4

    Can't dump 2D arrays

    Hello community. I need some help with my C program... spent hours and still couldn't figure out what's wrong. I'm kinda freaking out because it's for school and time is running out! There is a function called dbread() that is supposed to read an ini-like file. User provides the file name, section, key and a string where the stored value should go in. Problem is I just can't free memory after I use it, otherwise it just won't work! It gives me memory errors (run-time errors). There are two string vectors dynamicly allocated (char**). Well they can't be dumped.

    I'm going to attach two files, one is the program itself and the other is an ini file that should be in the same path as the program. This is a "malloc", "realloc" challange for anyone who feels interested. Please rename db.txt to db.ini, or simply modify the source code. Should work either way.

    Thanks in advance for any help.
    Attached Files Attached Files

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Two things just from glancing through the file:
    1) Don't use strcpy, it's not safe!
    2) You mean "free" a 2D array. "Dumping" generally refers to outputting to the screen.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Error 1 error C3861: 'malloc': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 78
    Warning 2 warning C4244: '=' : conversion from 'int' to 'char', possible loss of data g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 79
    Error 3 error C3861: 'realloc': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 81
    Error 4 error C3861: 'malloc': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 88
    Warning 5 warning C4018: '<' : signed/unsigned mismatch g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 90
    Error 6 error C3861: 'realloc': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 93
    Error 7 error C3861: 'free': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 95
    Error 8 error C3861: 'free': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 96
    Error 9 error C3861: 'malloc': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 115
    Error 10 error C3861: 'malloc': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 116
    Error 11 error C3861: 'malloc': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 119
    Warning 12 warning C4244: '=' : conversion from 'int' to 'char', possible loss of data g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 120
    Warning 13 warning C4244: '=' : conversion from 'int' to 'char', possible loss of data g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 122
    Error 14 error C3861: 'realloc': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 124
    Error 15 error C3861: 'malloc': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 127
    Error 16 error C3861: 'free': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 141
    Warning 17 warning C4018: '<' : signed/unsigned mismatch g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 150
    Error 18 error C3861: 'realloc': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 154
    Error 19 error C3861: 'free': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 160
    Error 20 error C3861: 'realloc': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 166
    Error 21 error C3861: 'free': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 172
    Error 22 error C3861: 'free': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 178
    Error 23 error C3861: 'free': identifier not found g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 184
    Warning 24 warning C4244: '=' : conversion from 'int' to 'char', possible loss of data g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 212

    ...and...

    Warning 1 warning C4244: '=' : conversion from 'int' to 'char', possible loss of data g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 79
    Warning 2 warning C4018: '<' : signed/unsigned mismatch g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 90
    Warning 3 warning C4244: '=' : conversion from 'int' to 'char', possible loss of data g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 120
    Warning 4 warning C4244: '=' : conversion from 'int' to 'char', possible loss of data g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 122
    Warning 5 warning C4018: '<' : signed/unsigned mismatch g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 150
    Warning 6 warning C4101: 'value' : unreferenced local variable g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 110
    Warning 7 warning C4244: '=' : conversion from 'int' to 'char', possible loss of data g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 212
    Warning 8 warning C4101: 'j' : unreferenced local variable g:\w00t\Visual Studio 2008\Projects\Temp\Temp3.cpp 221
    Warning 9 warning C4706: assignment within conditional expression g:\w00t\visual studio 2008\projects\temp\temp3.cpp 112
    Warning 10 warning C4706: assignment within conditional expression g:\w00t\visual studio 2008\projects\temp\temp3.cpp 76

    I will leave it as an exercise for you to fix those warnings and errors.
    Last edited by Elysia; 10-29-2008 at 06:20 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > I will leave it as an exercise for you to fix those warnings and errors.
    You mean YOUR warnings and errors.
    You saved it as a .cpp file, when the OP posted a .c file.

    That gets rid of all the malloc cast stuff right off the bat.

    However, Farrapo needs to include stdlib.h and then remove all the spurious casts of malloc et al.

    > str=(char*)realloc(str,strlen(str)+2);
    1. str starts off as 1 UNINITIALISED char, so who knows what strlen is going to return.
    2. If realloc fails, you leak memory.
    Always do
    temp = realloc( str,strlen(str)+2) ); if ( temp != NULL ) str = temp;
    // else do some tidying up

    > sprintf(str,"%s%c",str,ch);
    There are FAR better ways of appending a char to a string.
    And they don't invoke undefined behaviour.

    In general, consider a few more utility functions to replace some of the code you keep repeating.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    I see you pointed out a lot of rookie mistakes (I'm no experienced programmer). I did add strlib.h and removed the casts, quit using strcpy() and I'm no longer using fprintf to append the char to the string. Now I'm testing malloc and realloc. But the errors are exactly the same! Are you sure these stuff are the cause? Did you guys make it work? I don't usually ask for help but I am REALLY out of time and already spent many hours in this...

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > Are you sure these stuff are the cause?
    No idea - we advise.

    There are many errors in the code, some more serious than others. It's not like we could come up with a complete list right away anyway, since what you do to fix some problems may have implications on future discussions.

    Here's a biggie:
    int n = dbsection("db.ini",0,NULL);

    int dbsection(const char *filename,int sec,char *out)

    strcpy(out,str2);

    Do you have a debugger?
    With a debugger, a crash puts you back in the debugger, with the instruction pointer pointing at the instruction (or line of code) causing the problem. From there you can back-track to find out how it got into that situation.

    > Did you guys make it work?
    Nope, I just read the code and found some things wrong with it.

    Besides, us removing all the bugs wouldn't teach you anything as such. It's your code, all we can do is tell you how to fix it.

    Consider, for your next program, writing the program in stages

    Also, if you've changed the code, then you need to post it again. It's all very well describing what you've done, but we see with all to regularity that it's somehow different from what we imagined it would be like.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    4
    Well I'm sick of trying to find out what's wrong. Too many hours already spent. I'll just not free any memory and get a lame grade lol! Thank you people.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Quote Originally Posted by Salem View Post
    > I will leave it as an exercise for you to fix those warnings and errors.
    You mean YOUR warnings and errors.
    You saved it as a .cpp file, when the OP posted a .c file.
    Perhaps I did, but even so, those errors and warnings are legitimate. I would not post them if I did not think they were valid.
    Even though a C compiler might give a warning, it might still be a programming error.

    That gets rid of all the malloc cast stuff right off the bat.
    True, it would work without, but I was not the one who added them.

    However, Farrapo needs to include stdlib.h and then remove all the spurious casts of malloc et al.
    The failure to include the correct header was one of the errors, another reason I compiled it as C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    There's no reason to assume that the OP wanted C compiled as C++.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Well uh, no. I compiled it as C++ only to iron out problems in the code and present them (plus I do tend to compile C code as C++ to add C++ debugging techniques to code sometimes).
    Perhaps they would have warnings instead of errors if compiled as C, but hey, regardless of that, they should all be fixed, if possible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    >> Perhaps they would have warnings instead of errors if compiled as C, but hey, regardless of
    >> that, they should all be fixed, if possible.
    That's your personal preference and not important to the language being used, thus my point. There is no reason to assume the OP wanted C compiled as C++.

    >> (plus I do tend to compile C code as C++ to add C++ debugging techniques to code sometimes).
    As far as I'm aware, C and C++ have the same debugging features in step, next, breakpoints, watches, et cetera for any given professional IDE.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Quote Originally Posted by citizen View Post
    >> Perhaps they would have warnings instead of errors if compiled as C, but hey, regardless of
    >> that, they should all be fixed, if possible.
    That's your personal preference and not important to the language being used, thus my point. There is no reason to assume the OP wanted C compiled as C++.
    If I compiled that as C, I would have gotten a warning on the calling of malloc instead of an error. Not much else of a difference.
    Is that a bad thing?

    >> (plus I do tend to compile C code as C++ to add C++ debugging techniques to code sometimes).
    As far as I'm aware, C and C++ have the same debugging features in step, next, breakpoints, watches, et cetera for any given professional IDE.
    Forgive me. I meant things such as replacing arrays with boost::array to get assertions and such things.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    It's not about it being a "bad thing", Elysia. Mind where you are in the forums; there is a difference, and we do not make exceptions on the part of the poster. That's why Salem pointed it out. You would have gotten a similar response from someone if you were asking a C question in the C++ forum. It was not an attack on your person.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    No, it was an attack on the reply, I gather
    But I figured it was alright to post what output I got from compiling it as C++ (after filtering out certain things that are C++-related, of course). Perhaps I should just have compiled as C and posted the output or removed the indication that it was compiled as C++ (removing the filenames?)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    No, actually compile it with a C compiler next time. There is no reason to try and deceive someone, especially since you may not be the best at filtering C++ errors or warnings for C code. It's much better to simply acknowledge the difference between the two languages these days. After all, you are in the C forum. There is no reason to assume that whatever C++ can do for you will be relevant.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 2d arrays
    By thamiz in forum C Programming
    Replies: 25
    Last Post: 05-25-2008, 05:06 AM
  2. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  3. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  4. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  5. how can i pass 2d arrays
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2001, 06:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21