User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 374,011 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,783 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 275 | Replies: 2 | Solved
Reply
Join Date: May 2008
Posts: 66
Reputation: Clockowl is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
Clockowl Clockowl is offline Offline
Junior Poster in Training

Pointers == NULL upon function return?

  #1  
May 8th, 2008
Hai all,

I ran into a strange issue I never experienced before. Pointers are passed to function, the function allocates memory to them with calloc, and right before the return sanity checks if the pointers still != NULL. That check succeeds, but upon returning and sanity checking in the calling function, all pointers == NULL.

The code is pretty big, so I won't copypaste it all in here but instead i've attached them.

For those unfamiliar with OpenGL: The compiler line for this is:
gcc.exe BowViceJet.c -o BowViceJet.exe -lopengl32 -lglu32 -lgdi32 -Wall

The function I'm talking about is loadObj. As you can see, right before returning it checks:
  if(vertices == NULL || normals == NULL || verttext == NULL || triangleindex == NULL || trianglenormals == NULL || quadindex == NULL || quadnormals == NULL){
    printf("Failed to allocate enough memory, exiting...\n");
    exit(1);
  }
  return 0;
}

and in the calling function it checks again:
  loadObj("C:\\ObjTest.obj", vertices, normals, triangleindex, quadindex, &trianglecount, &quadcount);
  if(vertices == NULL || normals == NULL || triangleindex == NULL || quadindex == NULL){
    printf("Error! One of the essential pointers == NULL!");
    exit(1);
  }

and there it happily prints the message and exits.

How come?

Thanks in advance,
Nick

PS: Also included "ObjTest.txt" so you can experience it in it's full glory. Heh. Don't forget to either adjust source code to load "ObjTest.txt" or change extension to ".obj" so that it reads "ObjTest.obj".
Attached Files
File Type: c BowViceJet.c (6.8 KB, 0 views)
File Type: c loadObj.c (12.5 KB, 0 views)
File Type: txt ObjTest.txt (645 Bytes, 1 views)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,394
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 15
Solved Threads: 260
Moderator
Staff Writer
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampire Moderator

Re: Pointers == NULL upon function return?

  #2  
May 8th, 2008
That's because the memory address contained in the pointer is lost when the function is popped off the stack. In other words, you can modify the value that the pointer points to, but you can't modify the memory address that the pointer holds (well, you can, but the change won't have any effect on the pointer in the parent function).

The solution to this would be to use a pointer of a pointer.
Last edited by John A : May 8th, 2008 at 6:35 pm.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: May 2008
Posts: 66
Reputation: Clockowl is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 3
Clockowl Clockowl is offline Offline
Junior Poster in Training

Re: Pointers == NULL upon function return?

  #3  
May 8th, 2008
Ooooooooooh, really newb mistake right there, haha. Thanks for *pointing* it out. No pun intended ofc.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 10:51 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC