Archived

This topic is now archived and is closed to further replies.

Ferneu

functions that return pointers

Recommended Posts

All I wanna do is create a function that can return an array. In the matter of fact, I have acctually been able to do it... in text mode. When I go the 0x13 (320X200), whenever I try to compile the code it gives the same warning: "non-portable pointer assignment in function xxx", while on text mode, it gives no warnings at all. Look: (this is not the same function I intended to use, but any function which return a pointer produces the same error, so let's use a basic one to make things easy)
typedef unsigned char byte;

byte *hum (void)
{
    static byte *tmp

    tmp = malloc(3*(sizeof(byte)));

    *(tmp)   = 0;
    *(tmp+1) = 1;
    *(tmp+2) = 2;

    return tmp;
}



void main (void)
{
    int  i;
    byte *kk

    kk = hum();

    for(i=0; i<3; i++)
       printf("%d\n", *(kk+i));
}

  
It compiles whitout error or warnings. It prints, as it's supposed to do: 0 1 2 But that is only in text mode. If I go to mode 0x13 and do the exactly same thing, that warning shows up: "non-portable pointer assignment" when I do the kk = hum(); PS: using turbo C [edited by - Ferneu on January 16, 2004 4:26:12 PM]

Share this post


Link to post
Share on other sites
that's definitely a wierd error. I would expect it to compile fine. the big danger about functions that return pointers is that the caller will forget to de-allocate the memory (something you are doing). make sure to call:

free(kk); at the end of your prog or you'll leak memory.

otherwise, i don't know what mode 0x13 is, so i can't help.

the other thing you can try is passing a pointer to the pointer as a program argument and allocating the memory that way:


void hum (byte ** p_ptr)
{
*p_ptr = malloc(3*(sizeof(byte)));

*(*p_ptr) = 0;
*(*p_ptr+1) = 1;
*(*p_ptr+2) = 2;
}

void main (void)
{
int i;
byte *kk;

hum(&kk);

for(i=0; i<3; i++)
printf("%d\n", *(kk+i));

//

// REMEMBER TO ADD THIS

//

if (kk != 0)
free(kk);
}


you should also probably get into the habit of initializing your variables or you'll end up with headaches later:

int i = 0;
byte *kk = 0;

-me

[edited by - Palidine on January 16, 2004 2:28:49 PM]

Share this post


Link to post
Share on other sites
Well, I know what mode 13 is, and I don''t understand how compiling two (almost) identical programs can give you warnings in pointers. Maybe it''s a matter of some #includes that you do in your code or sth like that? Or maybe the compiler is too old?

Share this post


Link to post
Share on other sites
Thx for the advice about freeing the memory, altough I usually don''t forget that. My mistake

About the memory model, I''m using HUGE.

The compiler is kind of old, like I''ve said, I''m doing it on turbo C.

I''ll re-check my includes in to see if I''m the one who screwing it all up. I''ll also try passing a pointer to the pointer. I''ll post the results.

Share this post


Link to post
Share on other sites
I wasn't blamming the mode 0x13, I was just giving the circunstances where the error appeard. By the way, passing the pointer as an argument, like Palidine said worked fine. But still, I would like to know why that warning keep showing up.

PS: there was one little thing that was in my code that I've forgotten to put here. When I've declared the pointer tmp, I've made it static.

static byte *tmp;

yet letting it auto produced the same result.


[edited by - Ferneu on January 16, 2004 4:26:48 PM]

Share this post


Link to post
Share on other sites
Without seeing the actual code that produces the warning, we can only guess.

Edit:

I realize you are not blaming the video mode. However, all you have told us is "the code works in text mode, but not in mode 0x13." Since we both agree that the video mode isn't the problem, that leaves only the code. Clearly, there is some other difference in the two versions of the code.


[edited by - Dave Hunt on January 16, 2004 4:51:26 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Drevay
malloc() and free() make baby jesus cry.

new and delete, however, do no such thing.



This is C not C++.

Back on topic: which line exactly is giving the non portable pointer conversion error? And it''s in huge mode for both the normal build and the mode 13h build?

Share this post


Link to post
Share on other sites
Oh.. sorry about not posting the whole code.

Just the fact of going to the mode 0x13
(just after declaring the vars)
was producing that error.

Anyway, here it goes:


void set_mode (byte mode)
{
union REGS regs;

regs.h.ah = 0x00;
regs.h.al = mode;
int86(0x10, ®s, ®s);
}


And about the memory models, on both cases I was using the HUGE.
And the warning appear on the line where you try to assign the function to a pointer.

like:
kk = hum();



[edited by - Ferneu on January 16, 2004 4:59:56 PM]

Share this post


Link to post
Share on other sites
I use

void set_mode(byte mode)
{
union REGS regs;

regs.h.ah = SET_MODE;
regs.h.al = mode;
int86(VIDEO_INT, ®s, ®s);
}
& regs makes a ®s sign GRRRRR. so change ®s to & regs without a space.


with a LARGE MODEL -ml. You need to use a large model for 13h to work properly.

[Mercury Software] [Google!]

[edited by - DerAngeD on January 16, 2004 5:28:46 PM]

Share this post


Link to post
Share on other sites
In this case you''re right,
but this code is kind of little cause it''s only a test
code. The real one needed HUGE.

Oh.. there''s one thing I''ve forgotten to say.
Acctually, I haven''t even seen that until now.
When I tested the code on the mode 0x13, the function
that returns the pointer is on a different source file.
I don''t know if that matters or not, but I''ll test it
placing the function and the main() on the same place.

Share this post


Link to post
Share on other sites
Actually using a function in a different source file does make a difference because pointers across translation units probably will be based on different segments. Did you try explicitly declaring the pointers to be far pointers?

Share this post


Link to post
Share on other sites