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

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
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;
}
 

xtknight

Elite Member
Oct 15, 2004
12,974
0
71
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?
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
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.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
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.
 

JasonCoder

Golden Member
Feb 23, 2005
1,893
1
81
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.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
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.
 

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
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!
 

SamurAchzar

Platinum Member
Feb 15, 2006
2,422
3
76
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.