0

here is example how Pointers are used to store and manage the addresses of dynamically allocated blocks of memory

#include <iostream>
#include <stdio.h>
using namespace std;
struct Item{
    int id;
    char* name;
    float cost;
    };
   struct Item*make_item(const char *name){
        struct Item *item;
        item=malloc(sizeof(struct Item));
        if (item==NULL)
             return NULL;
        memset(item,0,sizeof(struct Item));
        item->id=-1;
        item->name=NULL;
        item->cost=0.0;

         /* Save a copy of the name in the new Item */
        item->name=malloc(strlen(name)+1);
        if (item->name=NULL){
            free(item);
            return NULL;
        }

        strcpy(item->name,name);
         return item;

        }

int main(){



     return 0;
}

but here is mistakes

1

>------ Build started: Project: dynamic_memory, Configuration: Debug Win32 ------
1>  dynamic_memory.cpp
1>c:\users\david\documents\visual studio 2010\projects\dynamic_memory\dynamic_memory.cpp(11): error C2440: '=' : cannot convert from 'void *' to 'Item *'
1>          Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>c:\users\david\documents\visual studio 2010\projects\dynamic_memory\dynamic_memory.cpp(20): error C2440: '=' : cannot convert from 'void *' to 'char *'
1>          Conversion from 'void*' to pointer to non-'void' requires an explicit cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

what is wrong?please help

3
  • 1
    This is very nearly correct C, but it's bad C++ (even with the compiler errors fixed with a cast). I'd suggest picking one language for a program and using it properly. Commented Jul 27, 2010 at 21:13
  • 1
    I just don't get it. 202 questions. Still no idea how to format a post or how to write a proper question. Reputation continously growing. Commented Jul 27, 2010 at 22:23
  • Why is this marked as C++. This is obviously C code. Commented Jul 28, 2010 at 3:02

4 Answers 4

6

Since this is C++, you need to cast the return from malloc as C++ does not automatically convert a void * to a T *:

item=static_cast<Item *>(malloc(sizeof(struct Item)));

Or even better, stop using malloc and use new instead and you won't have to cast:

item = new Item;
item->name = new char[strlen(name + 1)];

That said, if you do use new, you need to free with delete:

delete[] item->name;
delete item;

Also, if you use new, by default the runtime will inform you about out of memory by throwing an exception. While it's best to learn how to deal with exceptions as a temporary stop gap, you can have the nothrow version of new so that it will return 0 when out of memory:

item = new (std::nothrow) Item;
Sign up to request clarification or add additional context in comments.

2 Comments

If this is C++, it shouldn't be using malloc in the first place. Instead, use new to get an array of bytes.
Also consider using a constructor rather than a function to create a new object. This would be absolutely trivial with std::string and a constructor.
2

I absolutely agree with previous answers -- if this is going to be used in a C++ program, do it the C++ way.

Try this:

#include <iostream>
using namespace std;
struct Item{
    int id;
    string name;
    float cost;
    Item(char *pName) : id(-1), name(pName), cost(0) {}
    };
// Look ma, no "make_item"!

Then, where you would have used make_item:

    ...
    pItem = make_item("hoozit");
    ...

replace that code with:

    ...
    pItem = new Item("hoozit");
    ...

1 Comment

Only thing this is missing is the obligatory "There, I fixed it for ya" comment.
0

At row 11 item=malloc(sizeof(struct Item));
must become item=(Item *)malloc(sizeof(struct Item));

At row 20 item->name=malloc(strlen(name)+1);
must become item->name=(char *)malloc(strlen(name)+1);

At row 21 if (item->name=NULL){
must become if (item->name==NULL){.

1 Comment

The memset call is probably also superfluous.
0

If you just want to fix your compilation errors...

Change this:

item=malloc(sizeof(struct Item));

To:

item=(item*)malloc(sizeof(struct Item));

and this:

item->name=malloc(strlen(name)+1);

To:

item->name=(char*)malloc(strlen(name)+1);

1 Comment

(item*) must be (Item*). :)