Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

malloc error under microsoft compiler

Status
Not open for further replies.

ushtabalakh

Programmer
Jun 4, 2007
132
int *p;
p = malloc(sizeof(int));

I receive this error : error C2440: '=' : cannot convert from 'void *' to 'int *'

I'm trying to have a dynamically allocated array of integers, After doing this how can I access the third and fourth element of this array?
 
You must be building it as C++. You probably have the /Tp flag set somewhere. Shouldn't need to cast in C. Anyway try
Code:
p = (int*) malloc (sizeof (int));
 
> After doing this how can I access the third and fourth element of this array?
The given malloc call has only ONE allocated int, not 3 or 4.

As xwb noted, you need to make sure you're compiling as C (and not C++).
An alternative to /Tp is to change the source file from [tt]prog.cpp[/tt] to [tt]prog.c[/tt]


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Code:
#include "stdio.h"

#include "stdlib.h"





int *ChangeToDynamical(int a[],int size){

int *p;

if((p=malloc(size *sizeof(int)))==NULL)

	printf("Error\n");
int i;

for(i=0;i<size;size++){
	*p=a[i];
	p++;
}

return p;

}





int main(int argc, char *argv[])

{
	printf("starts here");
	int ii;

	int a[] = {1,2,3,4,5};
	char *c = ChangeToDynamical(a,5);


	for(ii=0;ii<5;ii++)
		printf("%d\n",c++);

}
Hey

This gives a permission error when I run it under linux
 
size++ should be changed to i++

I think there was a permission problem with execution that I solved but now when I run
gcc -c malloc.c
I get a malloc.o file
and when I type ./malloc.o
I get
bash: ./malloc.o: cannot execute binary file

How do I execute this file to see the result?
 
That will not generate an executable. -c only compiles: it does not link with libraries. You will need that for independent compilation when you have more than one source program. To build an executable

gcc -o memalloc malloc.c

The program is called whatever you specify after the -o. In this case memalloc. To execute

./memalloc

If you had just compiled as

gcc malloc.c

It would have generated a.out. You can also build with -c and then link later. Look up man pages on ar and ld on how to do this.
 
> char *c = ChangeToDynamical(a,5);
But your function returns int*, so what's with the cast to char* ?


--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
And you probably might want to printf("%d\n", *c++);
 
Also, system headers are usually #included with <>, and custom headers use "".
Ex.
Code:
#include <stdio.h>
#include <stdlib.h>
#include "MyHeader.h"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top