Thread: Using malloc

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by ulillillia View Post
    Note 7 deals with the WinMain function, essential to Windows itself so these parameters are needed (as far as I know anyway). They worked without reporting warnings earlier, it just happened all of a sudden, the same with the case for note 1.
    Note that if you aren't using the parameters but require them, you can just remove the names after the types in the definition or the implementation. It works just fine and will not harm the function.
    For example, for my _tmain, I did this:
    Code:
    int __cdecl _tmain(int, TCHAR* /*argv[]*/, TCHAR*)
    Note how I removed the names from the variables since I don't use them. This removes the unreferenced warning.
    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.

  2. #17
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Okay, a big clue. I changed the size of the array manually by taking out calculator and punching in lots of numbers. I then ran my program and found that it used 100% CPU and was getting about 50 fps when normally it'd have 30% CPU usage and a smooth 60 fps. If I simply comment out the malloc line, I get the usual 30% CPU usage (this is due to the timer) and the smooth 60 fps. This doesn't seem right....
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Note that Elysia's suggestion only works in C++.

    You can also disable that particular warning if it becomes too annoying.
    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.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dwks View Post
    Note that Elysia's suggestion only works in C++.
    Hmm. I never knew that.
    But then again, just doing a
    argument;
    would also remove that warning, and I think it works in C too?
    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.

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, then you get another warning, to the effect of "that's doesn't do anything". I'll show you an ugly macro you could use:
    Code:
    #define unused(x) \
        do { void * _p = &(x); _p = 0; } while(0)
    It probably wouldn't pass a gcov test, however. (You could also just set the parameter to 0, I suppose. )

    [edit] In C, I tend to just disable that particular warning, because I like to use function pointers, and you can easily end up with lots of unused parameters that way. You can always enable it every once in a while to see which functions really take parameters that could be eliminated. [/edit]
    Last edited by dwks; 02-20-2008 at 03:05 PM.
    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.

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ulillillia View Post
    Okay, a big clue. I changed the size of the array manually by taking out calculator and punching in lots of numbers. I then ran my program and found that it used 100% CPU and was getting about 50 fps when normally it'd have 30% CPU usage and a smooth 60 fps. If I simply comment out the malloc line, I get the usual 30% CPU usage (this is due to the timer) and the smooth 60 fps. This doesn't seem right....
    If malloc uses a lot of time, it's highly likely that you are in "debug" mode - build your code for "release" and it should be fine. Debug mode malloc does a whole lot of extra work, mainly by filling the entire allocated block with a pattern so that it/you can detect uninitialized variables more easily.

    --
    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.

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Hmm. OK, I admit, C just isn't my area of expertise. It's beyond me
    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.

  8. #23
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    Hmm. OK, I admit, C just isn't my area of expertise. It's beyond me
    But C is a subset of C++.

    *jab jab*


  9. #24
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Other way around. C++ is a superset of C.
    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.

  10. #25
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But I still don't get it. What works in C++ doesn't work in C, sooooooo... there you have it
    Anyway, back on topic...
    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. #26
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Not 30% CPU usage, more like 20%, even with several dozen images being drawn already but that'll increase drastically by the time my project is done.... You still left out one thing though. Note that in a previous post I stated of the need to have to change the array size manually (defeating the purpose of malloc) in order to get rid of the "unhandled exception" error. If I drop the array size down from the 1,515,520 value to 1,500,000 for that particular one, the error occurs. Is malloc working? I at least got the program to run now so I can check pointers. I'll report back after this check.

    Edit: I found that the pointers are invalid and I haven't commented out the part where they get set. That could explain it. Fixing....
    Last edited by ulillillia; 02-20-2008 at 03:32 PM. Reason: Reported back as indicated
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  12. #27
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    But I still don't get it. What works in C++ doesn't work in C, sooooooo... there you have it
    Anyway, back on topic...
    http://cboard.cprogramming.com/showt...080#post705080

    You were saying that the two could be one and that you could do both with one.

    Don't mind me, I'm just playing with matches.

    dwks yeah whatever. ;D
    Last edited by robwhit; 02-20-2008 at 03:32 PM.

  13. #28
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Having assigned the pointers (as I forgot), I test ran it and the program is crashing upon closing stating:

    HEAP[Supernatural Olympics.exe]: Invalid Address specified to RtlValidateHeap( 01CB0000, 010C4C40 )
    Windows has triggered a breakpoint in Supernatural Olympics.exe.

    This may be due to a corruption of the heap, and indicates a bug in Supernatural Olympics.exe or any of the DLLs it has loaded.

    The output window may have more diagnostic information
    The program '[2800] Supernatural Olympics.exe: Native' has exited with code 0 (0x0).
    I checked the pointer immediately after setting it and checked it again after loading the file, which contains malloc. The numbers are the same, but when it gets to the "free" instructions at the end of the program (upon closing it), I get this. What does this mean and what is going on? How can it be fixed?

    I've also noticed that my memory usage has gone up as well. It was 400 MB and now it's around 500 MB, probably because of this, a sign of a memory leak apparently.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  14. #29
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My guess is that you are overwriting SOMETHING in one of your allocations.

    --
    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.

  15. #30
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I think it has something to do with the function call. I recall that I had a similar problem with having pointers being assigned within a function where names are just replaced. I think malloc isn't working because of this. I think I need to put it outside of the function in order for it to work. I'm adding the next set of objects and once I get done with that, I'll test something out.

    Edit: It has to work within the function itself in this case. If I do it before the function is called, I won't know how much memory is needed. If I do it after the function is called, the file loading will almost certainly go out of the bounds of the array unless I manually set it.
    Last edited by ulillillia; 02-20-2008 at 04:47 PM. Reason: Potential problem discovered
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. the basics of malloc
    By nakedBallerina in forum C Programming
    Replies: 21
    Last Post: 05-20-2008, 02:32 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM