functions that return pointers

Started by
13 comments, last by Ferneu 20 years, 3 months ago
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]
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]
When you compile your mode 0x13 application, what memory model do you use?
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?
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.
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.
malloc() and free() make baby jesus cry.

new and delete, however, do no such thing.

_________________Politics is the ability to foretell what is going to happen tomorrow, next week, next month and next year. And to have the ability afterwards to explain why it didn't happen. -- Winston ChurchillGDNet-0.2 - rate users the easy way with this nifty Firefox extension. Updated with new features.
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]
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]
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?

This topic is closed to new replies.

Advertisement