(i m a student
our placements are going on
last week HP visited our campus
in the technical round they asked me this question)
union a{
some data;
};
mem=malloc(80);
and there is some data copied to the mem variable
now how one can map the mem to a
mapping between union and malloc memory
Started by dhiru, Nov 27 2008 09:45 AM
3 replies to this topic
#1
Posted 27 November 2008 - 09:45 AM
|
|
|
#2
Posted 27 November 2008 - 11:52 AM
something like this should work i guess:
union a *new = NULL;
mem=malloc(80);
if(mem != NULL)
{
new = (union a*)(&mem);
}
#3
Posted 27 November 2008 - 10:06 PM
That's not correct, since mem would have to be a pointer. That would assign the data beginning with mem somewhere in memory to the pointer. And you can't use new as a variable in C++, so I wouldn't use that either.
typedef union {
/*some stuff*/
}A;
void *mem = malloc(/*arbitrary size*/);
A *myunion = (A *)mem;
#4
Posted 27 November 2008 - 11:30 PM
ahh yeah indeed mem would have to be a pointer
well about the new i assumed c but yeah its better to avoid it
well about the new i assumed c but yeah its better to avoid it


Sign In
Create Account

Back to top









