Re: Mixing size_t and other types

On 08/ 1/10 09:31 PM, Navaneeth wrote:

I have a variable

long int size;

Value of this variable will be the size of a file which I am trying to
load into memory. Now I need to allocate memory using malloc. If I


compiler warns about "conversion to ‘size_t’ from ‘long int’ may
change the sign of the result". So I added an explicit cast like

malloc((size_t) size);

I am not sure that this is the correct approach. Is this casting OK?
How can I find out the value of size_t?

Strangely enough, size_t is commonly used for sizes, so just make size a size_t and save to agro.

size_t will be an unsigned type.

Ian Collins

Relevant Pages

  • Re: 2D array of structures
    ... programming habits. ... int str_len; ... // Declaring of menu items, necessary for later reuse in array ... // it's time to allocate memory on the fly, ...
  • Problem in compiling a C code with MSVC++6.00
    ... When I save the file as a cpp file, it compiles and runs ... int p1; ... void checkfile; ... printf("cannot allocate memory for elements!"); ...
  • Re: Basic Pointer Question...
    ... I coded a function to allocate memory and i am passing a pointer to the function. ... In addition, as malloc() is hidden within a function, freecan be easily forgotten. ...
  • Re: I am sorry for this...
    ... > int main ... > is it legal or does the temporary get destroyed at the 'f's closing brace? ... introduced to allocate memory for the result of a function for the ... return path the function returns a valid reference, while in another, ...
  • SetBkMode Transparent takes no effekt
    ... i'm developing a program to print out some richtext with pictures. ... public int FormatRange(bool measureOnly, PrintPageEventArgs e, int charFrom, ... Marshal.StructureToPtr(fr, lParam, false); ...