Wierd Malloc Problem

This is a discussion on Wierd Malloc Problem within the C Programming forums, part of the General Programming Boards category; Hey guys, I'm trying to port a linux library to a embedded environment. Now the code was running fine untill ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    6

    Wierd Malloc Problem

    Hey guys,
    I'm trying to port a linux library to a embedded environment. Now the code was running fine untill i encountered this problem.
    Code:
    main(){
    dvd = DVDOpen("c:/CliffHanger");
    }
    dvd_reader_t *DVDOpen( const char *ppath )
    {
    /*Lots of init stuff here*/
    ret_val = DVDOpenPath( path );
    }
    static dvd_reader_t *DVDOpenPath( const char *path_root )
    {
        dvd_reader_t *dvd;
    
        dvd = (dvd_reader_t *) malloc( sizeof( dvd_reader_t ) );
        if( !dvd ) return 0;
        /* lots of other stuff here*/
    }
    Now if i try to just open c:/ which has another dvd then the file malloc goes properly and i proceed to read the DVD files properly. But when i change the parameter to cliffHanger (or anything else for that matter) the malloc fails.
    This is wierd becuase i dont think there is any connectiong between the malloc and what I give as the parameter...
    Another intresting observation is that the malloc on fail gives a errno of 0.. so if i try stderror(errno) it prints sayin no error. Can some one unravel this mystery before i lose my temper and break the board..
    Thanks guys for your patience.

    P.S: The filesystem i'm running is a Fat32 on the hard disk on the embedded board. Just a info. Dont think its related though.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    To achieve a string of C:/... you need to tell the compiler that you want a backslash, since backslash is an escape (using in for example '\n'), and you do that by using a double backslash - it has nothing to do with malloc tho'.

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

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    6

    Huh!!!

    Isnt \ backslash... wers is a backslash in the string "c:/" .. so why do i need two backslashes? And moreover c:/ is working fine. its c:/<something> thats triggering malloc error. And if it was a slash problem why should malloc return NULL...

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Just try "c:\\Cliffhanger". It's already been explained why you need two backslashes.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are of course correct. A forwardslash is not a problem - it was a pavlovian response to seeing "C:... ", which is USUALLY followed by a single backslash which should be a double one.

    So, you are saying, I presume that malloc returns NULL, but no errno? The setting of errno may depend on the C library implementation - since the C library for an embedded OS may not be a standard implementation, it may have bugs in this area.

    What is the size of dvd_reader_t?

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

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    6
    Code:
    struct dvd_reader_s {
        /* Basic information. */
        int isImageFile;
      
        /* Hack for keeping track of the css status. 
         * 0: no css, 1: perhaps (need init of keys), 2: have done init */
        int css_state;
        int css_title; /* Last title that we have called dvdinpute_title for. */
    
        /* Information required for an image file. */
        dvd_input_t dev;
    
        /* Information required for a directory path drive. */
        char *path_root;
      
        /* Filesystem cache */
        int udfcache_level; /* 0 - turned off, 1 - on */
        void *udfcache;
    };
    Where dvd_input_t is just a wrapper for FILE *fd.
    Hope this helps. And thanks for the lightning quick responses guys. Btw since this is a custom file system, its not windows and hence not c:\ ...

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Doesn't look like something that should cause a problem.

    I don't have any good idea on how to solve this problem. Is is possible that some other function has clobbered internal data that malloc() uses to find the memory to dole out (or you GENUINELY is out of memory)?

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

  8. #8
    Registered User
    Join Date
    Sep 2008
    Posts
    6
    nah man!!! i increased the heap size and checked.. just cant be out of memory.. Dont know.. I"m also stumped.. Hmm.. k will sleep over it and think.. Please keep posting if you guys find any ideas as to why this is happening guys.

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mohankarthik View Post
    nah man!!! i increased the heap size and checked.. just cant be out of memory.. Dont know.. I"m also stumped.. Hmm.. k will sleep over it and think.. Please keep posting if you guys find any ideas as to why this is happening guys.
    Then I'd say the most likely scenario is that something else is clobbering the malloc' memory management, so that the linked list (or whatever it uses) says that there is no more memory available. Check malloc calls BEFORE this function, and correct usage.

    [Also, check that you are includign stdlib.h, since you are casting the result of malloc, you may not realize that the prototype is missing, and some architectures return pointers in a different register than the integer return value].

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

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,175
    Quote Originally Posted by rags_to_riches View Post
    Then I'll add to that - main must return int!
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Are you getting a valid pointer from malloc()? Can you post your "lots of other stuff" code in the form of an attachment containing your C files?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wierd problem
    By darsatdusk in forum C Programming
    Replies: 7
    Last Post: 11-03-2008, 08:49 AM
  2. malloc problem
    By Ideswa in forum C Programming
    Replies: 4
    Last Post: 10-23-2007, 04:11 PM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. Malloc problem
    By freeindy in forum C Programming
    Replies: 6
    Last Post: 05-02-2007, 12:13 PM
  5. wierd Problem on Small If Prog
    By God in forum C++ Programming
    Replies: 7
    Last Post: 07-13-2004, 08:08 AM

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