0

So, here is my issue, I've been stuck on this for a while now. This is working with linked lists... I have two structs declared as such

typedef struct BankAccountList *BankAccountListP;

struct BankAccountList {
   AccountNodeP head;
   int count;
};

AccountNodeP newNode(char *, int, double);

struct AccountNode {
   char *name;
   int id;
   double balance;
   AccountNodeP next;
};

Up until this pointer, this has worked just fine, when i try to allocate memory however...

AccountNodeP acctNodePtr = (AccountNodeP) malloc(sizeof(AccountNodeP));

Stops my program in its tracks. This is located in a function that returns the accountNodePtr.

The function:

AccountNodeP newNode(char *name, int id, double balance) {
   AccountNodeP acctNodePtr = (AccountNodeP) malloc(sizeof(AccountNodeP));
                        fprintf(stderr, "\nERROR 4\n");

   if (acctNodePtr == NULL) {
       fprintf(stderr, "\nERROR 10\n");         
   }
   acctNodePtr->next = NULL;
   acctNodePtr->id = id;
   acctNodePtr->balance = balance;
   strcpy(acctNodePtr->name, name);
   return acctNodePtr;
}

The first time and second time that this function is called it works but at the end my program still doesnt END correctly, it gets and error and stops running. However, the information is saved in the struct (I was able to tell using fprintf(stderr).) However, the third time I call it, it errors and stops my program at the AccountNodeP acctNodePtr = malloc... statement. I also know this because of using fprintf(stderr).

Can you see anything obviously wrong with my code? I'm not sure that I can pin point the issue. Please help me understand instead of just giving me the answer, I need to understand exactly what is happening.

NOTED: The error is in this function, my tester and adt file work fine other then this function. There is not a real need to give you the rest of my code because I'm sure the error is here somewhere.

1
  • The issue with this program is that both sizeof(struct AccountNode) and allocating memory for the string needed to be done. Commented Feb 6, 2015 at 17:08

2 Answers 2

3

If your AccountNodeP is a pointer to your structure, the sizeof(AccountNodeP) will return the size of the pointer and not of the structure. You might want to put sizeof(struct AccountNode) instead.

P.S. And DO NOT CAST the return of malloc!

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

7 Comments

Also, don't forget to call free when you are done using the dynamically allocated memory, otherwise you will have memory leak.
I changed it to sizeof(struct AccountNode) and now it is saying that the pointer is being set to NULL which if I'm correct means I'm out of memory. I'm going to reboot my computer and see if that is part of the issue, since I haven't writtten the free method yet.
"It is saying".. What is saying? According to your code, you should get ERROR 10 in case the allocation failed.
Sorry, ERROR 10 was displaying in the terminal. Which I believe means that the allocation failed.
EDIT: Sorry, ERROR 10 was displaying in the terminal. Which I believe means that the allocation failed. sizeof(struct AccountNode) causes it to crash on the second call of the function. sizeof(AccountNodeP) causes it to crash on the third call of the function. sizeof(*AccountNodeP) causes the program to not compile.
|
1

You are calling strcpy(acctNodePtr->name, name) without first initializing acctNodePtr->name to point to a valid memory address.

You can initialize it properly with acctNodePtr->name = malloc(strlen(name)+1).

5 Comments

Noted, however my error isn't there. at least not yet. lol i will go ahead and fix it though to get it out of the way.
I'm not sure what the issue is, but that didn't work and actually just caused my program to crash again so for the time being I will ignore it.
@PacoProgramming: Do whatever you want, but calling strcpy(acctNodePtr->name, name) without first initializing acctNodePtr->name to point to a valid memory address yields undefined behavior!!!
No don't get me wrong I believe you, I just am trying to figure out the malloc issue before worrying about that. Thank you.
You were right, it was a combination of your answer and @Eugene Sh.

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.