By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,573 Members | 1,208 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,573 IT Pros & Developers. It's quick & easy.

program hangs after printf() with literal string

P: 19
Hi,

It appears my program can't get past a particular printf() statement.

Code excerpt:
[...]
printf("Sales Report\n--------------");
printf("Testing code - pre loop entry");

while(category != null)
{
printf("Testing code - in-loop");
[...]

(Ignore the category thing, that's just the start of a linkedlist search) The program is written so that at the end of a function, it'll return to main (which will reprint a menu for next selection). However, when I run this option, all i get is:

Sales Report
------------------

and then it hangs - no segfault, it just sits there, like there's an infinite loop. But given it doesn't even print the 'pre loop test', I can't see how it could BE in an infinite loop - it hasn't even gotten to the loop yet. It's stuck between printf() statements.

Furthermore, if I replace the "printf("Testing code - pre loop entry"); with just a 'return;', the funtion returns to the menu after printing the first printf() statement. So it can access a 'return;' statement fine, but it can't read anything else after that first printf().

I have this written pretty much identically in another function, where it has to print out stock quantities rather than sales, and that works perfectly. I can't find any indication in my reference manuals as to what's causing this, and GDB simply tells me there's a segfault in " ?? () ". I'm really at a loss as to diagnose this - help?
Apr 21 '07 #1
Share this Question
Share on Google+
7 Replies


Expert 10K+
P: 11,448
Hi,

It appears my program can't get past a particular printf() statement.

Code excerpt:
[...]
printf("Sales Report\n--------------");
printf("Testing code - pre loop entry");

while(category != null)
{
printf("Testing code - in-loop");
[...]

(Ignore the category thing, that's just the start of a linkedlist search) The program is written so that at the end of a function, it'll return to main (which will reprint a menu for next selection). However, when I run this option, all i get is:

Sales Report
------------------

and then it hangs - no segfault, it just sits there, like there's an infinite loop. But given it doesn't even print the 'pre loop test', I can't see how it could BE in an infinite loop - it hasn't even gotten to the loop yet. It's stuck between printf() statements.

Furthermore, if I replace the "printf("Testing code - pre loop entry"); with just a 'return;', the funtion returns to the menu after printing the first printf() statement. So it can access a 'return;' statement fine, but it can't read anything else after that first printf().

I have this written pretty much identically in another function, where it has to print out stock quantities rather than sales, and that works perfectly. I can't find any indication in my reference manuals as to what's causing this, and GDB simply tells me there's a segfault in " ?? () ". I'm really at a loss as to diagnose this - help?
Does your real while loop looks like this by any chance?
Expand|Select|Wrap|Line Numbers
  1. while(category != null);
Note the trailing semi-colon.

kind regards,

Jos
Apr 21 '07 #2

P: 19
Does your real while loop looks like this by any chance?
Expand|Select|Wrap|Line Numbers
  1. while(category != null);
Note the trailing semi-colon.

kind regards,

Jos
Nope, no semi-colon after the while loop.
Apr 21 '07 #3

Expert 10K+
P: 11,448
Nope, no semi-colon after the while loop.
Can you copy and paste (the relevant parts) of your code here? Don't type
it in by hand because subconsciously you might correct certain typos etc.

kind regards,

Jos
Apr 21 '07 #4

P: 19
Code from start of the function to several lines after the start of the while loop. Yes, I realise the while loop isn't closed; neither is the function, but it's a long loop, and also part of an assignment, so I'd rather not post the whole while loop unless necessary. (My uni has strict plaguarism rules).

Expand|Select|Wrap|Line Numbers
  1. void reportSales(GJCType *menu)
  2. {
  3.    float smallItemSales;
  4.    float medItemSales;
  5.    float largeItemSales;
  6.    float totalItemSales;
  7.    float smallCatSales;
  8.    float medCatSales;
  9.    float largeCatSales;
  10.    float totalCatSales;
  11.  
  12.    CategoryTypePtr category = menu->headCategory;
  13.    ItemTypePtr item;
  14.  
  15.    printf("Sales Report\n------------\n");
  16.    printf("Testing Code - pre loop entry");
  17.  
  18.    /*Loop through each category*/
  19.    while(category != NULL)
  20.    {
  21.       printf("Testing Code - within loop");
  22.  
  23.       /*reset all category variables*/
  24.       item = category->headItem;
  25.       smallCatSales = 0;
  26.       medCatSales = 0;
  27.       largeCatSales = 0;
  28.       totalCatSales = 0;
Apr 21 '07 #5

Expert 10K+
P: 11,448
What happens if you terminate each printed string with '\n'? stdout is line
buffered and it will not print out a line until it has to print a new line character
or when its buffer is full. printf() is the poor man's debugger and it's your friend ;-)

kind regards,

ps. I can't find the cause of the bug given that code snippet.
Apr 21 '07 #6

P: 19
What happens if you terminate each printed string with '\n'? stdout is line
buffered and it will not print out a line until it has to print a new line character
or when its buffer is full. printf() is the poor man's debugger and it's your friend ;-)

kind regards,

ps. I can't find the cause of the bug given that code snippet.
Ah-hah! That is indeed why the testing code won't print. Completely forgot about the newline thing with stdout. (Aaaand the problem exists within the while loop, which is indeed infinite. Oh joy.)

Problem solved - thanks for your help!
Apr 21 '07 #7

Expert 10K+
P: 11,448
Ah-hah! That is indeed why the testing code won't print. Completely forgot about the newline thing with stdout. (Aaaand the problem exists within the while loop, which is indeed infinite. Oh joy.)
Have fun solving the bug ;-)

Problem solved - thanks for your help!
You're welcome of course.

kind regards,

Jos
Apr 21 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.