2

I am working on this code:

struct box
{
    char word[200][200];
    char meaning[200][200];
    int count;
};

struct root {
    box *alphabets[26];
};
root *stem;
box *access;
void init(){
     //cout<<"start";
    for(int i = 0 ; i<= 25; i++){
        struct box *temp =(struct box*)( malloc(sizeof(struct box)*100));
        temp->count = 0;
        cout<<temp->count;
        stem->alphabets[i] = temp;
    }
    //cout<<" initialized";
}

It got compiled without errors, but during its execution it stops at the point where temp is allocated to stem->alphabets[i]. How to fix this?

6
  • 4
    stem is not initialized. Thus a crash. initialize access too. Commented Sep 14, 2013 at 13:22
  • 1
    are you using C or C++? They are different languages. Commented Sep 14, 2013 at 13:22
  • Why are you using malloc in a C++ program ??? Commented Sep 14, 2013 at 13:24
  • 1
    Choose one C or C++ ? With C++, you have STL, to make you job lot easier. Commented Sep 14, 2013 at 13:26
  • 1
    I told you you would run into more problems :-) Commented Sep 14, 2013 at 13:53

4 Answers 4

6

Make stem a struct, not a pointer:

root stem; // No asterisk

Otherwise, there's no memory allocated to it, so dereferencing it is undefined behavior.

Of course you need to replace stem->alphabets[i] with stem.alphabets[i].

Sign up to request clarification or add additional context in comments.

2 Comments

You meant stem i guess.
@KarthikSivam You are welcome! If this solves your problem, consider accepting the answer by clicking the grey check mark next to it. This lets other visitors of the site know that you are no longer actively looking for an improved solution to this problem, and earns you a brand-new badge on Stack Overflow.
4

You need to allocate memory for the stem variable

root * stem = new root();

Don't forget to dealocate:

delete stem;

Better yet, read something about memory allocations in C++

Comments

3

stem and temp are two different variables. :) you are giving memory to temp and accessing stem.

4 Comments

I am habitual of this :) carry on guyz
That was me -- although I'd swear the "you are giving memory to temp and accessing stem" wasn't there when I voted, it won't let me reverse until you edit. :(
Ok -- spoofed that with a dud edit to get this travesty off my conscience. All apologies.
@goldilocks chill man, and thanks and salute to your honesty :)
0

You are using pointers without initializing them first. The simple answer is not to use pointers in the first place. I can see no reason for pointers in the code you have posted.

struct box
{
    char word[200][200];
    char meaning[200][200];
    int count;
};

struct root {
    box alphabets[26];
};
root stem;

Much easier.

Comments

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.