0

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

  • The #include<..> is another Include like conio.h, etc.
  • I'm forget to put the using namespace std; in the code..
13
  • #include <...> ??? And the presence of string makes this C++, a different language to C. Commented May 23, 2013 at 2:55
  • Those macros are extremely pointless when you could just use the data members themselves. Even if not doing that, a normal function is still much better. And this is really C inside a C++ shell. Using std::string is the only C++ I can find. malloc is pretty much deprecated, typedefing structs is no longer useful, objects shouldn't be passed by value when only reading them, and we have nullptr. Commented May 23, 2013 at 2:57
  • If you're writing C++, you should forget about using malloc() and use new instead. If you're using malloc(), you should be writing C code. Commented May 23, 2013 at 3:05
  • @paxdiablo The '#include<..>' is another Include like conio.h, etc. About the 'string' I didn't know if it is C++ or C language. Commented May 23, 2013 at 3:06
  • 1
    And, if you're using conio, get rid of it. The Borland compiler is seriously old tech and hasn't kept up to date with standards. Commented May 23, 2013 at 3:20

3 Answers 3

2

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Commenting here to avoid the mess under the questions...The reason the OK doesn't appear is because you didn't output a newline after it. If you want a line of output to appear, output the newline: printf("OK\n"); as otherwise the C standard I/O library buffers the output until a newline is printed, or the buffer overfills (which is probably over 500 characters on the one line of output).
0
  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++.

3 Comments

@JonathanLeffler - Thanks - Was in the process of doing that edit myself.
[1] Ok, thank you for that suggestion. [2] Because it will make simple instead if I must write P-->Parent [3&4] Thanks, I forget about it
@AhmadNabili - For point 2 - How does it make it simpler? Certainly makes debugging a bit harder with the #define all over the shop.
0

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;

1 Comment

I'd define in the top of the code #define Info(P) (P)->Info So if I write Info(P) it means P->info. And about the dot(.) Info(P).pName Because I'd declare that the "info" is another typedef data type :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.