Jump to content
  • Advertisement

Archived

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

Ferneu

functions that return pointers

This topic is 5421 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
The compiler doesn''t know (and doesn''t care) what video mode you are in. So, there must be something else different in the two versions of your code.

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!