I need help on "malloc();"

I'm using Turbo C++ 3.0, and I need to set up a 64k buffer for the video. The current line I have isn't working : "unsigned char *double_buffer = (char *)malloc(64000);"

Please message me back if you know the right way to do this.

Thanks.


Comments

  • : I'm using Turbo C++ 3.0, and I need to set up a 64k buffer for the video. The current line I have isn't working : "unsigned char *double_buffer = (char *)malloc(64000);"

    : Please message me back if you know the right way to do this.

    : Thanks.


    Try this:

    void double_buffer;

    *double_buffer = (char *)malloc(sizeof(char[64000]);


    Or something like that..


    Personally I think you should swap to DJGPP and Allegro if you wish to do graphics programming.


    You can download DJGPP (it's a free C/C++ compiler which is pretty much just like Turbo C++ but it's more powerful) at http://www.delorie.com/

    You can get the Allegro graphics library for DJGPP at http://www.talula.demon.co.uk/allegro/


    It's all free and it's very good.


    -Xotor-


  • : I'm using Turbo C++ 3.0, and I need to set up a 64k buffer for the video. The current line I have isn't working : "unsigned char *double_buffer = (char *)malloc(64000);"


    1) 'isn't working' is vague. Next time offer a compile-time error or warning for us to investigate.


    2) Just for the sake of the obscure notion of compatibility, it's probably better to do:


    malloc(sizeof(unsigned char)*64000)


    which is guaranteed to give you enough space for the exact type of data your variable purports to point to. Yes, yes, I know malloc is supposed to return a number of bytes anyway. Humour me. Other types of mallocs are more likely to follow the format I and others suggested.


    3) malloc returns a void pointer. You are casting it to a pointer-to-char. Your variable is of type pointer-to-unsigned-char. This won't work. Typecast the return of malloc to type 'pointer-to-unsigned-char' and you'll be fine.


  • : I'm using Turbo C++ 3.0, and I need to set up a 64k buffer for the video. The current line I have isn't working : "unsigned char *double_buffer = (char *)malloc(64000);"

    : Please message me back if you know the right way to do this.

    : Thanks.


    I think it soesn;t work for this reason. You are trying to call a function during the declaration time... I am not sure if you can do that... Anyway, try this... I've done it and it works...


    char *double_buffer;


    ....


    double_buffer = (char *)malloc(64000);

    ....


    I am not sure about this... but I've tried my version in Turbo C, and it works...





  • : I think it soesn;t work for this reason. You are trying to call a function during the declaration time... I am not sure if you can do that... Anyway, try this... I've done it and it works...


    It works just fine. You can initialize something to a value as you declare it. See my message below yours for why. However this has no bearing on why he's getting the error message.




    : char *double_buffer;


    : ....


    : double_buffer = (char *)malloc(64000);




    This is not the same as his statement, and so therefore works just fine. His problem was mismatched pointers. I don't advocate pointer casts either except where necessary (like your statement above, which of course is absolutely necessary).


    I still don't like the use of the magic number 64000, nor its direct use in malloc. A char and a byte are not always guaranteed to be the same size. (Though, I guess, in all fairness, the number of machines that it is not true on is pretty limited).


  • : I'm using Turbo C++ 3.0, and I need to set up a 64k buffer for the video. The current line I have isn't working : "unsigned char *double_buffer = (char *)malloc(64000);"

    : Please message me back if you know the right way to do this.

    : Thanks.


    Well, if malloc fails to allocation 64000 bytes

    and if you are using TC++, you can try something like,..


    char far *double_buffer;


    double_buffer = farmalloc ( (unsigned long) 64000 );


    if (!double_buffer)

    exit(1);


    well, it worked for me, but this wont work if you run it under the IDE, just quit TC and run your .exe, it should work







  • try


    double_buffer = (char far*) farmalloc (unsigned long) 64000 );


    you probably will need the cast or TCC will probably give you a incompatible pointer type warning


  • The problem was that it was crashing my computer when I ran it with in TCC ide. It works fine out of the IDE though

    Thanks again


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories