Using malloc

This is a discussion on Using malloc within the C Programming forums, part of the General Programming Boards category; I now know what the problem is, but I don't know how to fix it. To help, I've reposted the ...

  1. #31
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I now know what the problem is, but I don't know how to fix it. To help, I've reposted the code below (the top part is the function itself with the key area in focus and the bottom is how the function is used:

    Code:
    char LoadFile(const char FileName[64], char FileType, BITMAPINFOHEADER *BMPInfo, unsigned char *BMPData, char FogUsed, double ObjectScaling, char LoadType, unsigned char *BMPDataPointer, unsigned long BMPMemoryUsage)
    {
    ...
    			BMPMemoryUsage = BMPInfo->biWidth*BMPInfo->biHeight*3;
    			BMPDataPointer = malloc(BMPMemoryUsage);
    			fread(BMPData, 1, BMPMemoryUsage, FileHandle); // read the main image data // reads in BGR format
    			
    			DebugPointerTest[1] = (int)BMPDataPointer;
    			DebugTest[2] = (double)BMPMemoryUsage; // works fine, but the actual focused one is unchanged
    			DebugTest[3] = (double)DebugPointerTest[1]; // shows 0 - invalid pointer
    ...
    }
    
    ...
    	PrelakeHills04MainDataPointer = PrelakeHills04Data; // an array so the "&" isn't needed
    	LoadFile("PrelakeHills04.tga", 2, &PrelakeHills04Info, PrelakeHills04Data, 1, PrelakeHills04.Scaling, 0, PrelakeHills04MainDataPointer, PrelakeHills04MemoryUsage);
    Here's what the parameters mean:

    Filename - the file name - no problems
    FileType - a "flag" to let me know whether I'm loading a BMP or TGA file so it reads the header data properly - no problems
    BMPInfo - a pointer to the info structure - no problems
    BMPData - a pointer to the image data - no problems
    FogUsed - a flag to indicate whether or not fog needs to added to the object (not everything uses it, such as texts, panels, etc.) - no problems
    ObjectScaling - the object's distance, which determines the fog's intensity - no problems
    LoadType - a "flag" to indicate the type of load to do - a full load (done at startup) or a reload (rarely used, but generally to reapply fog after adjusting the visiblity) - no problems
    BMPDataPointer - a pointer to the data, of which I'm trying to use to get malloc to work - having problems
    BMPMemoryUsage - used to set the "memory usage" amount for the item - having problems

    I'd otherwise have 3 more parameters, but were left out since I couldn't seem to set pointers through a function call (not returning pointers, setting them).
    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

  2. #32
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	PrelakeHills04MainDataPointer = PrelakeHills04Data; // an array so the "&" isn't needed
    	LoadFile("PrelakeHills04.tga", 2, &PrelakeHills04Info, PrelakeHills04Data, 1, PrelakeHills04.Scaling, 0, PrelakeHills04MainDataPointer, PrelakeHills04MemoryUsage);
    The red section is suspicious to me. But you are later on allocating memory that you store into the same pointer - however, the pointer is not changing the PrelakeHills04MainDataPointer itself - you need to pass the ADDRESS OF THE POINTER to change the actual pointer outside of the function. Same with the PrelakeHills04MemoryUsage, it needs to be a pointer to int, so that you can change the value outside of the LoadFile() function.

    --
    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. #33
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Pass the address to it and from 37 warnings, I jump to 41 warnings.

    Code:
    This gives 37 warnings as is:
    LoadFile("PrelakeHills04.tga", 2, &PrelakeHills04Info, PrelakeHills04Data, 1, PrelakeHills04.Scaling, 0, PrelakeHills04MainDataPointer, PrelakeHills04MemoryUsage);
    
    This gives 41 warnings:
    LoadFile("PrelakeHills04.tga", 2, &PrelakeHills04Info, PrelakeHills04Data, 1, PrelakeHills04.Scaling, 0, &PrelakeHills04MainDataPointer, &PrelakeHills04MemoryUsage);
    These are the 4 new warnings that appear:
    c:\my documents\my programs\supernatural olympics\supernatural olympics\supernatural olympics.c(3190) : warning C4047: 'function' : 'unsigned char *' differs in levels of indirection from 'unsigned char **__w64 '
    c:\my documents\my programs\supernatural olympics\supernatural olympics\supernatural olympics.c(3190) : warning C4024: 'LoadFile' : different types for formal and actual parameter 8
    c:\my documents\my programs\supernatural olympics\supernatural olympics\supernatural olympics.c(3190) : warning C4047: 'function' : 'unsigned long' differs in levels of indirection from 'unsigned long *__w64 '
    c:\my documents\my programs\supernatural olympics\supernatural olympics\supernatural olympics.c(3190) : warning C4024: 'LoadFile' : different types for formal and actual parameter 9
    Well, one problem solved, that of the MemoryUsage one getting rid of the last two warnings of the four, but what about the first two, I don't get those.
    Last edited by ulillillia; 02-20-2008 at 05:33 PM. Reason: 1 of 2 problems resolved
    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

  4. #34
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Here are your variables:
    Code:
    unsigned char *PrelakeHills04MainDataPointer; // malloc requires a pointer to the data
    unsigned long PrelakeHills04MemoryUsage;
    Here's the prototype:
    Code:
    char LoadFile(/* ... */, unsigned char *BMPDataPointer, unsigned long *BMPMemoryUsage)
    Therefore, you need to call it like this:
    Code:
    LoadFile(/* ... */, PrelakeHills04MainDataPointer, &PrelakeHills04MemoryUsage);
    As far as I can tell, anyway.
    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.

  5. #35
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I editted my post to note that the one for memory usage is resolved, but the one for the main data pointer is not resolved. Whether or not I have the "&" or the "*", either compilation errors occur, or the pointer is 0 indicating that it is invalid.
    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

Page 3 of 3 FirstFirst 123
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, 11:26 AM
  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, 08:08 AM

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