Jump to content
  • Advertisement
Sign in to follow this  
Genjix

c equivalent of new operator for structs

This topic is 4970 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

how can i allocate new structs in C? (my_obj*)malloc(sizeof(my_obj)) doesnt seem to work.. why?

Share this post


Link to post
Share on other sites
Advertisement
I don't see why it wouldn't work. Since it just allocates memory, and your using C, it won't call your structs constructor if there is one.

What's not working properly about it??

Cheers
Chris

Share this post


Link to post
Share on other sites

... it magically compiles now, spooky.



#include <malloc.h>

#define new(type) (struct type*)malloc(sizeof(struct type))

struct a {
int a , b;
char c;
};

int main()
{
struct a *apo;

//apo = (struct a*)malloc(sizeof(struct a));
apo = new(a);

free(apo);

return 0;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

#include <stdlib.h>

typedef struct my_struct_ my_struct;
struct my_struct_ {
int dummy;
};

int main(void) {
my_struct *my_obj = malloc(sizeof *my_obj);
if (my_obj) {
free(my_obj);
return EXIT_SUCCESS;
} else {
return EXIT_FAILURE;
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by noVum
"free" does nothing when its argument is NULL, so that if is uneccessary.


That was my first reaction as well, but in this case the test is required to determine the correct return path. Putting the free inside the path that actually needs it is then only natural.

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by Genjix
what is the use of that typedef when you declare multiple structs?


In C, you would otherwise need to type the 'struct' keyword as part of the type:


typedef struct foo {
int x;
} foo_struct;

foo x; // error! Compiler can't make any sense out of 'foo'
struct foo x; // This is ok
foo_struct x; // as is this.


IIRC, giving the typedef the same name as the actual struct is legal too. You will often see a pointer to the struct being typedef'd in C code as well.

Share this post


Link to post
Share on other sites
yes i know this, my point being, what is the point of typedef'ing one struct out of the many structs you use.

Share this post


Link to post
Share on other sites
What do you mean? In C you'd normally want to typedef all your structs, so you don't have to type struct all day. That's pretty much the reason why.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!