Jakeebill
Jakeebill

Reputation: 3

Using String and Pointer with malloc

I cannot figure out why this program is not working. I got Access Violation message when trying to push a variabel with the type of data is string to another variable that had allocated at memory with malloc.

For example, first I declare the variable..

string pName;
address temp;

After that, I call the Allocate module..

temp = Allocate(pName, 1, 1, 200);

And here's the module..

#include <...>
#include<string>
#define Info(T) (T)->info
#define FirstSon(T) (T)->ps_fs
#define NextBro(T) (T)->ps_nb
#define Parent(T) (T)->ps_pr

using namespace std;
typedef struct infoElmt{
    string pName;
    float number;
    int type;       
    float price;
}compInfo;
typedef compInfo infotype;

typedef struct tElmtTree *address;
typedef struct tElmtTree {
    infotype info;
    address ps_fs, ps_nb, ps_pr;
} node;

typedef address DynTree;

address Allocate (string pName, float number, int type, float price)     //(string pName, float number, int unit, int type, float price
    {

        address P;

        P = (address) malloc (sizeof(node));

        if (P != NULL)
        {
            Info(P).type = type;
            Info(P).number = number;
            Info(P).price = price;

            FirstSon(P)  = NULL;
            NextBro(P) = NULL;
            Parent(P) = NULL;

            printf("OK");
            Info(P).pName = pName;
        }

        return (P);
    }

The error is came when the program run the Info(P).pName = pName; , I know it because if the printf("OK"); moved to below Info(P).pName = pName; , the "OK" doesn't showed in the console.

Is it problem with malloc and string?

Edit

Upvotes: 0

Views: 158

Answers (3)

SaintLike
SaintLike

Reputation: 9579

If you're using typedef to define your struct, you need to use a pointer

address *p //you should always use lowercase to declare variables.

to have access to struct fields, since you're using a pointer you need to use -> instead of .

Info(p)->type=type;

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 60017

  1. Use C++ new operator - not malloc
  2. Why use these

    #define Info(T) (T)->info
    #define FirstSon(T) (T)->ps_fs
    #define NextBro(T) (T)->ps_nb
    #define Parent(T) (T)->ps_pr
    

    when you could use the member variables directly (or better still define getters and setters).

  3. This line is pointless

    typedef compInfo infotype;
    
  4. Look up cout - C++ way of printing to the console. printf is C.

When you fix these issues then the bug will be more evident.

i.e. Either program in C or C++.

Upvotes: 0

Alok Save
Alok Save

Reputation: 206566

You should use new and not malloc. Your structure seems to include a std::string and it cannot be initialized correctly when you allocate the structure using a malloc.

In C++ just don't use malloc at all unless you have some rare scenarios where you need just a block of uninitialized memory. Just use get yourself used to new.

On a different note do avoid dynamic allocations as much as possible. Perhaps you may want to use:

std::vector<std::string> obj;

Upvotes: 2

Related Questions