issue with structs :(

This is a discussion on issue with structs :( within the C++ Programming forums, part of the General Programming Boards category; Hi Following is the code Code: struct a{ string k; int count; } a *create() { a *b; b=(a *)malloc(sizeof(a)); ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    issue with structs :(

    Hi Following is the code
    Code:
    struct a{
    	string k;
    	int count;
    }
    
    a *create()
    {
    	a *b;
    	b=(a *)malloc(sizeof(a));
    	b->count=0;
    	b->k="XX";
            return b;
    }
    
    int
    main(int argc, char ** argv)
    {
    		a* test;
                     test=create();
                     return 0; 
    }
    I am getting an error when calling the create function.when assigning value to k as "XX" , and not while assigning value to count. please help

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    My guess would be that std::string constructor isn't called properly since you use "malloc" instead of "new".
    Use b = new a();
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    China,Nanjing
    Posts
    15
    Code:
    struct a{
    	string k;
    	int count;
    }
    maybe you should change it like this.
    Code:
    struct a{
    	string k;
    	int count;
    }a;

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    Quote Originally Posted by wxjeacen View Post
    Code:
    struct a{
    	string k;
    	int count;
    }
    maybe you should change it like this.
    Code:
    struct a{
    	string k;
    	int count;
    }a;
    that syntax only works with a typedef, and has nothing to do with it.
    Its most likely what magos said.
    Spidey out!

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,616
    Use this instead...

    Code:
    struct a
    {
    	string k;
    	int count;
    };

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    All malloc does is allocating x bytes of memory. What it doesn't do is actually creating an instance of a particular type. The fact that you have to cast the result to appropriate pointer is a huge give-away.

    Therefore it cannot be used for creating objects whose constructor actually does something. In case of your struct the compiler-generated constructor does something, and namely, it calls the default constructor for the string member.

    If you want to use malloc, you'll also need to create the object in the returned memory, and this can be done with placement new:

    Code:
    a *create()
    {
        void* p = malloc(sizeof(a));
        a* pa = new (p) A;
        pa->count=0;
        pa->k="XX";
        return a;
    }
    Because the compiler-generated destructor also does something, namely call the destructor of the string member which releases the memory allocated by the string object, you must also remember to call the destructor before freeing the pointer.

    Code:
    int main()
    {
        a* test = create();
        //...
        test->~a();
        free(test);
        return 0; 
    }
    Those steps are combined in new and delete.

    In addition new also handles errors, so as not to leak memory if constructing the object throws an exception, performing something analogous to this:

    Code:
        void* p = malloc(sizeof(a));
        try {
            return new (p) a;
        }
        catch (...) {
            free(p);
            throw;
        }
    In short, new is what one uses for allocating anything other than plain-old-data types.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    China,Nanjing
    Posts
    15
    Quote Originally Posted by Spidey View Post
    that syntax only works with a typedef, and has nothing to do with it.
    Its most likely what magos said.

    actually , I mean there should be a ";".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multidimentional structs + memcpy() == FAIL
    By Viper187 in forum C Programming
    Replies: 8
    Last Post: 06-18-2008, 02:46 AM
  2. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  3. Issue with Structs
    By eponymous in forum C Programming
    Replies: 35
    Last Post: 03-16-2008, 03:45 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21