Thread: Problem to dereference variable

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    235

    Question Problem to dereference variable

    I need to update RevOne

    Code:
    float * RevOne;
    test(&RevOne);
    void test(float ** f1){
        *f1 = malloc((sizeof (float)) * 256 );
        (*f1)[0]=1.0;
        (*f1)[1]=2.0;
        (*f1)[2]=3.0;
        printf("%d,%d,%d",(*f1)[0],(*f1)[1],(*f1)[2]);
    }
    Prints 0,1072693248,0
    Instead something like 1.0,2.0,3.0
    I just cannot find out what I do wrong.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Do you want to print floats or integers? printf - C++ Reference
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Like AndrewHunter said, you're using the format specifier for ints; you should be using %f instead.

    The link is to Linux on-line man pages, which are a better reference for C library stuff than a C++ reference. Although it is a Linux project, the information is not Linux-specific. Just look at the Conforming To section on each page, to see which standard(s) a function relies on.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Nominal Animal View Post
    The link is to Linux on-line man pages, which are a better reference for C library stuff than a C++ reference.
    Eh, the format specifiers were the important aspect and are shown in the link.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    Ok, this was just a test. Now I replace the code with this:
    Code:
        int a;
        for (a=0; a<256; a++){
            (*f1)[a]=a/255; // saturation
            (*f2)[a]=a/255*360; // hue
            }
    I want to get float but how would you solve this? Should I use float a? Otherwise I got 0. 0. 0. as a result

  6. #6
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by AndrewHunter View Post
    Eh, the format specifiers were the important aspect and are shown in the link.
    True. I just don't like the two languages, C and C++, being conflated. Sure, they have similarities, but they are different languages. Just because something works in one, does not mean it works (or works the same way) in the other, that's all. No offense intended.

    Quote Originally Posted by barracuda View Post
    I want to get float
    Then use floating-point division instead of integer division.

    In other words, (float)a / 256.0f and (float)a * 360.0f / 256.0f.

  7. #7
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    Once again. I have strange problem. Few days ago I have completed a rgb2hsv function which uses another function. All worked. Now I run the program and it crashes.

    Code:
    void bmp_rgb2hsv(BMPFILE * bmpfile, float ** f1, float ** f2)
    {
    uint32_t min, max;
    // actual values:
    min=22;
    max=51;
    a=0;
    bmpfile->buffer[a+1]=(unsigned char) '32'; // (uninitiated)
    bmpfile->buffer[a+1]=(uint32_t)((max==min?0:f2[max][min])); // crashes
    }
    types:
    Code:
    float ** MinMaxRevDelta; // MinMaxRevDelta_createArray(float ** MinMaxRevDelta, float ** SatDeltaDivMax)
    float ** SatDeltaDivMax;  // MinMaxRevDelta_createArray
    function for initiation - (*f2)[51][22]: initiated (result is float number)
    Code:
    void MinMaxRevDelta_createArray(float *** f1, float *** f2){
        uint32_t c; c=0;
        *f1 = malloc((sizeof (uint32_t)) * 254 );
        *f2 = malloc((sizeof (uint32_t)) * 254 );
        unsigned short int a,b;
        /*Pozor: Může některé položky budou nealokované,
        protože min. nikdy není větší než max.
            use: f1[max][min]; */
        for (a=1; a<256; a++) // a = max
        {
        (*f1)[a] = malloc((sizeof (float)) * a );
        (*f2)[a] = malloc((sizeof (float)) * a );
        for (b=0; b<a; b++) // b = min
            {
            (*f1)[a][b]=1.0/(a-b); // delta: max / min
            (*f2)[a][b]=(a-b)*255.5/a; // Saturation: delta / max
            c++;
            }
        }
    }
    Initiation:
    Code:
    // Here is created array of results
    MinMaxRevDelta_createArray(&MinMaxRevDelta, &SatDeltaDivMax);
    // here the result for MinMaxRevDelta[51][22] and SatDeltaDivMax[51][22] is also OK floating point number
    bmp_rgb2hsv(bmpfile, MinMaxRevDelta, SatDeltaDivMax);
    But in the function bmp_rgb2hsv it crashes immediately. What's wrong?

  8. #8
    Registered User
    Join Date
    Sep 2014
    Posts
    235
    The 9th line of the first code:
    This is f2[51] addesss (float *) 0x42900000
    f2[51][22] Cannot access memory at address 0x42900058
    f2[51][22] Cannot access memory at address 0x42900058
    (*f2)[51] Cannot access memory at address 0xcc

    *f1[51] 0.0196078438
    f1[51][0] 0.0196078438
    f1[51][1] 0.0199999996
    f1[51][22] 0.0344827585

    So the f1 looks good, but the f2 does not have any value
    Last edited by barracuda; 02-23-2015 at 08:15 AM.

  9. #9
    Registered User
    Join Date
    Sep 2014
    Posts
    235

    Exclamation

    I have checked all variables & declarations & calls at least 5x and I don't see any difference between the two variables f1 and f2. Then I checked when the f2 value is lost. I found this:

    Code:
    MinMaxRevDelta_createArray(&MinMaxRevDelta, &SatDeltaDivMax);
    rev_255_createArray(&Rev255); // here f2 is replaced by different pointer
    So this function make problem on line 3:
    Code:
    void rev_255_createArray(float ** f1, float ** f2){
        *f1 = malloc((sizeof (float)) * 256 );
        *f2 = malloc((sizeof (float)) * 256 );
        int a;
        for (a=0; a<256; a++){
            (*f1)[a]=(float) a/255.0f;
            (*f2)[a]=(float) a/255.0f*360.0f;
            }
    }
    So my question is: why it overwritte different pointer? f2 not global! And why f1 - I mean MinMaxRevDelta stays allocated to original?

    It is declared as:
    Code:
    float * Rev255;

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,507
    It looks like you defined rev_255_createArray as having two parameters, but you called it with only one argument. You should forward declare rev_255_createArray before calling it so that the compiler will more readily inform you of such mistakes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Sep 2014
    Posts
    235

    Thumbs up

    Wow. It helped. Thank you. I have removed the second argument. But I don't understand how this could rewrite the variable referenced by different function... I did not forward declare it because the function declare rev_255_createArray may be not finished.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,507
    Quote Originally Posted by barracuda
    I don't understand how this could rewrite the variable referenced by different function
    Undefined behaviour.

    Quote Originally Posted by barracuda
    I did not forward declare it because the function declare rev_255_createArray may be not finished.
    You should forward declare the function even if it is only a stub.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer dereference problem in sortFunction
    By csharp100 in forum C Programming
    Replies: 15
    Last Post: 04-19-2012, 06:35 PM
  2. problem with dual dereference of a struct pointer
    By dan_paul in forum C Programming
    Replies: 5
    Last Post: 11-12-2011, 09:13 PM
  3. null pointer dereference
    By qwertylurker in forum C Programming
    Replies: 3
    Last Post: 03-14-2011, 12:06 AM
  4. Pointer dereference
    By taurus in forum C Programming
    Replies: 1
    Last Post: 11-09-2008, 07:41 AM
  5. dereference pointers
    By PC TECH in forum C++ Programming
    Replies: 2
    Last Post: 02-06-2003, 04:43 PM