Thread: Problem in malloc

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    Problem in malloc

    was studying about dynamic memory today but when i did this code it didnt compile with
    msv6 but compiled with dev C++
    anyways here the code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
    int *integers;
    int required=3;
    int i;
    integers=malloc(required*sizeof(int));
    if(integers==NULL)
    {
    puts("Malloc failed");
    exit(1);
    }
    for(i=0;i<required;i++)
    {
    printf("Please enter number #%d: ",i+1);
    getchar();
    scanf("%d",&integers[i]);
    }
    for(i=0;i<required;i++)
    {
    	printf("Number #%d was %d\n",i+1,integers[i]);
    	getchar();
    }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What errors did you get?

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    For those of us that don't have either Dev C++, or msV6, or who might not even be on Windows, pray tell, what's the error you get?
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    D:\c_LESSONS_TUTRIALS_MADE_BY_ME\malloc\main.cpp(8 ) : error C2440: '=' : cannot convert from 'void *' to 'int *'

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yaah srry to forgot to post error

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    you are compiling your C code as C++ (C supports implicit casts from void pointers, C++ doesn't).

  7. #7
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    I think you need an array of pointesr to do the job:

    Code:
    int *integers[3];
    You have only allocated memory to integers, which is really integers[0], therefore integers[1] & integers[2] have not had memory allocated.

    ================

    EDIT: If

    Code:
    int *integers;
    is declared, but

    Code:
    integers[i]
    is used in the program, shouldn't the compiler give a warning?

    ===================

    So you have to use malloc to allocate memory to all 3 pointers, right before the user inputs each number:

    Code:
    for ( i = 0; i < required; i++ )
    {
        printf("Please enter number #%d: ",i+1);
       intergers[i] = malloc( sizeof(int) );
       scanf("%d",&integers[i]);
    }
    and then you have to free the memory malloc has allocated to each pointer:
    Code:
    for(i=0;i<required;i++)
    {
    	printf("Number #%d was %d\n",i+1,integers[i]);
    	getchar();
             free(integers[i]); 
    }
    Last edited by happyclown; 01-06-2009 at 05:13 PM. Reason: another typo
    OS: Linux Mint 13(Maya) LTS 64 bit.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cyberfish View Post
    you are compiling your C code as C++ (C supports implicit casts from void pointers, C++ doesn't).
    And to fix that, all you actually need to do is rename the file from something.cpp to something.c and MS VC will automatically use the C flavour of it's compiler, rather than the C++ flavour.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yah thanks it compiles cool now thanks mats i dunno why in msv6 you cant compile as .c alawys .cpp

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lolguy View Post
    yah thanks it compiles cool now thanks mats i dunno why in msv6 you cant compile as .c alawys .cpp
    You CAN compile C as well as C++ in MSVC6, you just have to make sure that when you create the file, you name it as .c rather than .cpp which the IDE insists on if you don't give it a different extension.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    yah i have to rename but when i start new project and new file there option only C++ source file there is no C source file

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by lolguy View Post
    yah i have to rename but when i start new project and new file there option only C++ source file there is no C source file
    Yeah. I use the "Add new item..." in the Project view instead of the New File ... dialog - but I'm also using the next release of Visual Studio (.Net) rather than 6 - I think I still have 6 somewhere on my machine, but I'm not sure. ... Actually, I have VS 5.0 - and I think VS 6 is similar - when you select "New file ...", just click on the C++ file type, and then make the extension .c, and you should be fine.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    oh cool thanks alot mate

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by happyclown View Post
    ================

    EDIT: If

    Code:
    int *integers;
    is declared, but

    Code:
    integers[i]
    is used in the program, shouldn't the compiler give a warning?
    Well, but, why? Given that the OP's usage was perfectly correct, and in fact declaring a pointer and then mallocing memory is pretty much the only way to do dynamic memory allocation in C, then a whole heck of a lot of code would fall under this restriction.

  15. #15
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Well, is it to okay to declare a pointer of type int( int *integers), and then use an array of type int[integers[i]), which hasn't been declared? But the compiler didn't give a warning about the undeclared array?

    EDIT: so is

    Code:
    int *integers;
    the same as

    Code:
    int integers[i]?
    where i > 0?

    I am a newbie, so maybe I am missing something(s)?
    Last edited by happyclown; 01-06-2009 at 06:46 PM. Reason: typo
    OS: Linux Mint 13(Maya) LTS 64 bit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  3. malloc and realloc
    By odysseus.lost in forum C Programming
    Replies: 3
    Last Post: 05-27-2005, 08:44 AM
  4. freeing problem with concurrent processes
    By alavardi in forum C Programming
    Replies: 2
    Last Post: 03-07-2005, 01:09 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM