Dinamic allocation for numberic types

This is a discussion on Dinamic allocation for numberic types within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a problem using dinamic allocation for numberic types. That's what I'm doing Code: //IDE = DevC++ //compiler ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287

    Dinamic allocation for numberic types

    Hello, I have a problem using dinamic allocation for numberic types. That's what I'm doing

    Code:
    //IDE = DevC++
    //compiler = MingW
    //compiled as cpp
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct _DATA
    {
    double *dbl;
    } DATA,*LPDATA;
    
    int main()
    {
    double dbl;
    DATA d;
    
    d.dbl=(double*)malloc(sizeof(*d.dbl));
    if(!d.dbl) {return 0;}
    dbl=125.15f;
    *d.dbl=dbl;
    printf("%f %f\n",dbl,d.dbl);
    
    free(d.dbl);
    getch();
    return 0;
    }
    The printf shows
    Code:
    125.150002 0.000000
    My question is, ┐how should I setup a dynamic double value and be able to work with it? Maybe I'm missing something, but I have been thinking about it but I'm no able to find a working solution.

    Thank's in advance
    Niara

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    1.
    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    Should be:
    Code:
    #include <cstdio>
    #include <cstdlib>
    2. Indentation.
    3.
    Code:
    d.dbl=(double*)malloc(sizeof(*d.dbl));
    ...
    free(d.dbl);
    Should be:
    Code:
    d.dbl = new double;
    ...
    delete d.dbl;
    4.
    Code:
    getch();
    Use something standard.
    5. This is C++. Use cout not printf.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Dereference your pointer when you print it.
    Code:
    printf("%f %f\n",dbl,*d.dbl);
    Also, don't cast the result of malloc() (if you're using C... but you put this in C++, so...).
    And "dynamic".
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >dbl=125.15f;
    If you want to assign to a double, don't use the f specifier. The default is double:
    Code:
    dbl = 125.15;

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    Hello manutd, Cactus_Hugger and swoopy, thank's for your help and time.

    manutd: I have always the same problem: C synthax on C++ mode compilation. I like C but I compile as C++ because on certain moment I can do some 'debug' operations without the strict C model type (I say 'debug' operations, but also other things).

    Cactus_Hugger: that was what I was missing. Because I usually work with dynamic (sorry for my english syntax) memory on char* types and that's always dereferenced; I forgot to use it here.

    swoopy: I didn't kwon that but I know that compiler doesn't alert any error when omit the 'f' specifier on double types, I'll have in mind.


    Bye and have a good day
    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid types 'int[int]' for array subscript
    By kolistivra in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2010, 12:57 PM
  2. Replies: 6
    Last Post: 08-23-2008, 02:16 PM
  3. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 03:25 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  5. allocation on heap, scalar/non scalar types
    By curlious in forum C++ Programming
    Replies: 10
    Last Post: 12-30-2003, 05:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21