• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Bizarre problem with passing a pointer to a function in C...

slugg

Diamond Member
I have two functions, X and Y. Function Y takes in a pointer to a struct. Function X calls function Y and passes a pointer to a struct through the parameters.

For some reason, no matter what, function Y receives the pointer as null. I can't figure this out.

Here's an example of what I'm talking about.... My actual code is actually very, very long, and it's really messy for the time being (since I'm messing with it trying to get it to work). I have no idea why this is happening. Any ideas?

Example:

void makeList()
{
int listLength;

List *myList = (List*)malloc(sizeof(List));
ListAdd(1, myList);
ListAdd(2, myList);
ListAdd(3, myList);

listLength = GetListLength(myList);

printf("List length: %d\n", listLength);
}

int GetListLength(List* L)
{
//The following line will get a segmentation fault because L is null...
//even though we passed a valid pointer to this function. WTF?!
return L->count;
}
 
Would probably need to see what ListAdd() does. You have verified that L is null by printing it out, or are you getting a segfault and don't know why?
 
Yeah, if you haven't then the first thing to do is narrow down the lifecycle of that pointer to be sure when it is changing value. The way to do that is to break execution at the function call to GetListlength. At that point check the value of the pointer and verify that it is non-null (and points to the right place). Then step into GetListLength() and check it again. If it wasn't null before the call, and is really null on entry then you probably have an issue with stack corruption. Given your sample code that would likely be happening in ListAdd.
 
Originally posted by: slugg
I have two functions, X and Y. Function Y takes in a pointer to a struct. Function X calls function Y and passes a pointer to a struct through the parameters.

For some reason, no matter what, function Y receives the pointer as null. I can't figure this out.

Here's an example of what I'm talking about.... My actual code is actually very, very long, and it's really messy for the time being (since I'm messing with it trying to get it to work). I have no idea why this is happening. Any ideas?

Example:

void makeList()
{
int listLength;

List *myList = (List*)malloc(sizeof(List));
if (myList == NULL) {
fprintf(stderr, "WTF out of memory\n");
return 1;
}


ListAdd(1, myList);
ListAdd(2, myList);
ListAdd(3, myList);

if (myList == NULL) {
fprintf(stderr, "WTF something is wrong with ListAdd\n");
return 1;
}

listLength = GetListLength(myList);

printf("List length: %d\n", listLength);
}

int GetListLength(List* L)
{
//The following line will get a segmentation fault because L is null...
//even though we passed a valid pointer to this function. WTF?!
return L->count;
}

Post the code for ListAdd.
 
Isn't it a convention to take ownership of a pointer by setting it to null? Mayhap that's what ListAdd does... although it doesn't seem like a good design if it does do that.
 
Originally posted by: JasonCoder
Isn't it a convention to take ownership of a pointer by setting it to null? Mayhap that's what ListAdd does... although it doesn't seem like a good design if it does do that.

ListAdd has to be declared as taking a List*& in order to affect myList directly.
 
I'm not quite allowed to post my source code yet... It's for a class. My professor is taking a look on Monday. After that, if it still isn't solved, I'll be able to post the code by then.

Thanks guys!
 
You could be running over your stack, although I'm not sure it's very likely. For debugging you could try declaring
static List *myList = (List*)malloc(sizeof(List));
and see if it helps. If it does, you're corrupting the stack in one of the ListAdd functions.

And as noted, always check return values from system calls.


 
Back
Top