GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-May-2004, 02:32
Rosmayati's Avatar
Rosmayati Rosmayati is offline
New Member
 
Join Date: May 2004
Location: Malaysia
Posts: 20
Rosmayati is on a distinguished road

malloc function


I've tried to use malloc function for dynamic memory allocation since i will be using very large input data.unfortunately, i've got the error like below.could anyone tell me how to solve the error?

Cannot convert void * to int *
Last edited by JdS : 18-May-2004 at 05:54. Reason: fixed typo in title
  #2  
Old 18-May-2004, 03:13
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
Hi Rosmayati,

just like your other question, you posted the error but not the source codes.. in order to help you figure out what the problem is, you should post the codes that is causing you the error..
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #3  
Old 18-May-2004, 04:00
Rosmayati's Avatar
Rosmayati Rosmayati is offline
New Member
 
Join Date: May 2004
Location: Malaysia
Posts: 20
Rosmayati is on a distinguished road
this is the source code
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h> /* required for the malloc and free functions */

int main() {
  int number;
  int *ptr;
  int i;

  printf("How many ints would you like store? ");
  scanf("%d", &number);

  ptr = malloc(number*sizeof(int)); /* allocate memory */
 
  if(ptr!=NULL) {
    for(i=0 ; i<number ; i++) {
      *(ptr+i) = i;
    }

    for(i=number ; i>0 ; i--) {
      printf("%d\n", *(ptr+(i-1))); /* print out in reverse order */
    }

    free(ptr); /* free allocated memory */
    return 0;
  }
  else {
    printf("\nMemory allocation failed - not enough memory.\n");
    return 1;
  }
}
Last edited by JdS : 18-May-2004 at 05:55. Reason: Please enclose c code in [c] & [/c] for syntax highlighting
  #4  
Old 18-May-2004, 04:26
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
the line here is the problem:

CPP / C++ / C Code:
ptr = malloc(number*sizeof(int)); /* allocate memory */
malloc return a void* and you cannot change it to ptr which is an int*.
try casting like this to correct it:
CPP / C++ / C Code:
ptr = (int*)malloc(number*sizeof(int)); /* allocate memory */
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #5  
Old 18-May-2004, 17:15
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
I believe the problem is you are compiling your program as a C++ file, not a C file. In C you are not supposed to specify the type returned from malloc(). When I compile it, your original code works fine. When I compile it with a .cpp extension, I get the same error you do.

Look at your compiler's switches and add the switch that specifies C-compilation. Or if your file ends in .cpp, change it to .c
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #6  
Old 18-May-2004, 21:08
Rosmayati's Avatar
Rosmayati Rosmayati is offline
New Member
 
Join Date: May 2004
Location: Malaysia
Posts: 20
Rosmayati is on a distinguished road
Okey, thanks for the solution. really appreciate it..
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
new to C, need an explanation andre C Programming Language 18 23-Mar-2004 11:09

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 01:54.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.