•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 397,626 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,405 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: 309 | Replies: 2 | Solved
![]() |
•
•
Join Date: May 2008
Posts: 140
Reputation:
Rep Power: 1
Solved Threads: 7
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:
and in the calling function it checks again:
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".
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". 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.
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Similar Threads
- Pointers (archived tutorial) (C++)
- Unexpected return from string comparison function (C)
- chars and pointers and all that fun stuff (C++)
- Pointers (C++)
- How to be Crash Free (C++)
- c language problm, how to pass pointer to a function (C)
- reading a file into code (Java)
- Safe Array (C++)
Other Threads in the C Forum
- Previous Thread: Having trouble displaying output of binary file
- Next Thread: Flowchart help Please!



Linear Mode