A bunch of other questions

What actually happens when you type cast? Is the variable reallocated? Which leads to my second question: Is allocation an expensive process? I heard it was...but it could be wrong. It didn't seem bad on my computer when i was doing some allocations, but thats cause i have a hyper-threading Pentium 4 at 3.4Ghz...I think, is that why unions are used rather than typecastings, which could be substituted i think.
(tell me again if im wrong).
Thats it...thank u again.

Comments

  • : What actually happens when you type cast? Is the variable reallocated? Which leads to my second question: Is allocation an expensive process? I heard it was...but it could be wrong. It didn't seem bad on my computer when i was doing some allocations, but thats cause i have a hyper-threading Pentium 4 at 3.4Ghz...I think, is that why unions are used rather than typecastings, which could be substituted i think.
    : (tell me again if im wrong).
    : Thats it...thank u again.
    :

    As you know, the computer can only save binary values into a memory cell. The type is only the description of how the data in that cell should be treated. So when you typecast, you usually only take the memory of one type and express it as another type. The data remains on the same place in the memory. That is why it is impossible to cast from/to certain types, such as custom-made structs, because it probably don't make sence to express them in another way.

    As for allocation, there are two kinds: static allocation and dynamic allocation. The most common is static allocation (for example when you write "int x;"). The variables is then placed on the stack.
    The less common is dynamic allocation, which you might need to use if you need to change the the allocated amount of memory at runtime. This is probably what you refer to in your post: it is slow.

    On modern PC computers it is hard to detect the efficiency of algorithms just by running them, everything will go extremly fast. You will need a lot of data to detect any difference (there are ways to clock the execution time, if you need to).

    Dynamic allocation has nothing to do with typecasting. The compiler will never generate any code that allocates dynamicly unless it is explicitly told to.

    Some memory might be saved to the stack when you typecast from/to floating types though. Because when you do so "invisible code" will run to format the float variable properly.

    This is not why you use union. union is used when you have a block of memory that you wish to express in several different ways.
    For example:

    [code]
    union
    {
    struct
    {
    int A;
    int B;
    int C;
    }singleVal;

    int array[3];

    }myUnion;
    [/code]

    Now the variables can be accessed either one-by-one with their given names, or as an array.


  • : : What actually happens when you type cast? Is the variable reallocated? Which leads to my second question: Is allocation an expensive process? I heard it was...but it could be wrong. It didn't seem bad on my computer when i was doing some allocations, but thats cause i have a hyper-threading Pentium 4 at 3.4Ghz...I think, is that why unions are used rather than typecastings, which could be substituted i think.
    : : (tell me again if im wrong).
    : : Thats it...thank u again.
    : :
    :
    : As you know, the computer can only save binary values into a memory cell. The type is only the description of how the data in that cell should be treated. So when you typecast, you usually only take the memory of one type and express it as another type. The data remains on the same place in the memory. That is why it is impossible to cast from/to certain types, such as custom-made structs, because it probably don't make sence to express them in another way.
    :
    : As for allocation, there are two kinds: static allocation and dynamic allocation. The most common is static allocation (for example when you write "int x;"). The variables is then placed on the stack.
    : The less common is dynamic allocation, which you might need to use if you need to change the the allocated amount of memory at runtime. This is probably what you refer to in your post: it is slow.
    :
    : On modern PC computers it is hard to detect the efficiency of algorithms just by running them, everything will go extremly fast. You will need a lot of data to detect any difference (there are ways to clock the execution time, if you need to).
    :
    : Dynamic allocation has nothing to do with typecasting. The compiler will never generate any code that allocates dynamicly unless it is explicitly told to.
    :
    : Some memory might be saved to the stack when you typecast from/to floating types though. Because when you do so "invisible code" will run to format the float variable properly.
    :
    : This is not why you use union. union is used when you have a block of memory that you wish to express in several different ways.
    : For example:
    :
    : [code]
    : union
    : {
    : struct
    : {
    : int A;
    : int B;
    : int C;
    : }singleVal;
    :
    : int array[3];
    :
    : }myUnion;
    : [/code]
    :
    : Now the variables can be accessed either one-by-one with their given names, or as an array.
    :
    :
    :
    Dynamic memory allocation is done by calling malloc(). Memory is allocated from the system memory (aka heap)in the form of a linked list. Around 3 bytes of memory are used up with every call to malloc() regardless of the size of the allocation.

    If there is not enough heap left, malloc() will return NULL, otherwise it will return a pointer to the start of the allocated memory.

    Call free() to free allocated memory; e.g.

    HeapPtr = malloc(size);
    free(HeapPtr);
  • : Dynamic memory allocation is done by calling malloc(). Memory is allocated from the system memory (aka heap)in the form of a linked list. Around 3 bytes of memory are used up with every call to malloc() regardless of the size of the allocation.

    Really! I didn't know that. Man! So malloc DOES do more than i thought, sadly :{

    I'm wondering if allocation( calls to alloc and realloc )
    is okay as part of a normal routine in a program. Not to be done once, but all the time (well not all the time...most, or alot of the time).
    Please tell.

    Thanx again for the info.


  • : : Dynamic memory allocation is done by calling malloc(). Memory is allocated from the system memory (aka heap)in the form of a linked list. Around 3 bytes of memory are used up with every call to malloc() regardless of the size of the allocation.
    :
    : Really! I didn't know that. Man! So malloc DOES do more than i thought, sadly :{
    :
    : I'm wondering if allocation( calls to alloc and realloc )
    : is okay as part of a normal routine in a program. Not to be done once, but all the time (well not all the time...most, or alot of the time).
    : Please tell.
    :
    : Thanx again for the info.
    :
    :
    :
    First of all, I meant that malloc has an overhead of 3 bytes - i.e if you malloc 10 bytes, you use 13 etc. etc.

    As far as using it is concerned, if you're running your program on a PC, there's not much point 'cos you've got tone of RAM at your disposal. It's more for embedded systems (that's what I'm working on at the moment) where you don't want to use RAM when you don't have to, so you allocate it depending on start-up criteria or something like that.
    It can get a bit hairy if you start running out of heap and malloc won't give you any memory. Lots of error checking and defensive programming needed!
  • : : : Dynamic memory allocation is done by calling malloc(). Memory is allocated from the system memory (aka heap)in the form of a linked list. Around 3 bytes of memory are used up with every call to malloc() regardless of the size of the allocation.
    : :
    : : Really! I didn't know that. Man! So malloc DOES do more than i thought, sadly :{
    : :
    : : I'm wondering if allocation( calls to alloc and realloc )
    : : is okay as part of a normal routine in a program. Not to be done once, but all the time (well not all the time...most, or alot of the time).
    : : Please tell.
    : :
    : : Thanx again for the info.
    : :
    : :
    : :
    : First of all, I meant that malloc has an overhead of 3 bytes - i.e if you malloc 10 bytes, you use 13 etc. etc.
    :
    : As far as using it is concerned, if you're running your program on a PC, there's not much point 'cos you've got tone of RAM at your disposal. It's more for embedded systems (that's what I'm working on at the moment) where you don't want to use RAM when you don't have to, so you allocate it depending on start-up criteria or something like that.
    : It can get a bit hairy if you start running out of heap and malloc won't give you any memory. Lots of error checking and defensive programming needed!
    :


    Yeah, a good rule is to avoid dynamic allocation in embedded systems. Depending on how much freedom you have, you might not need it - it depends on if you have an OS or not, and how the OS handles the memory.
    If you can avoid dynamic memory, you should. It has three major problems: it is slow, it makes it hard to determine the program's memory usage (which might lead to unsafe programs) and you get a fat heap sitting in your RAM where you could have stored other things instead.
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