Thread: Compiler at fault??

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    11

    Question Compiler at fault??

    Hi everybody, a new member writing here. I have a head scratcher on my hands.

    Code: (the original source code is a bit longer with unneeded info (i think) so I didn't include all of it. Here is the problematic part:

    Code:
    buf = calloc(size + 1, sizeof(char));
    
    ZeroString(buf, strlen(buf)+1);
    for (i = 0; i < k; i++)
    {
    	strcpy(&buf[strlen(buf)], token[i]);   // char **token; is just a
    	strcpy(&buf[strlen(buf)], "\n");       // bunch of words 
    }                                           
    
    printf(buf);
    Problem: we'll my Pelles C compiler won't print anything with the printf. BUT if I put a line
    Code:
    printf("", &buf);
    just as long as there is
    Code:
    &buf
    somewhere in the code then it works fine.
    And on the code blocks with the gcc it works correct, with the original version.

    So what dark forces are at fault here, am I missing something that I messed up or could the pelles c compiler be missfiring?

    Thank you for all the help,

    cheers.
    Last edited by juvan; 10-24-2011 at 08:59 AM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    This has worked for me in the past; but, might not be standard.
    Code:
    printf(buf);
    Try this
    Code:
    printf("%s", buf);
    You need to check that buf is NOT NULL; in case the alloc function failed.

    Tim S.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > ZeroString(buf, strlen(buf)+1);
    You do know what calloc filled the memory with don't you?

    > strcpy(&buf[strlen(buf)], token[i]);
    Otherwise known as
    strcat(buf, token[i]);

    > printf(buf);
    VERY DANGEROUS!
    Uncontrolled format string - Wikipedia, the free encyclopedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    11
    I don't think it's the printf's fault. For the sake of clarity I will just paste the whole code, because I think the problem could be quite hidden in with all the pointers and what not. So here is the whole code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    const int size_of_token = 30;
    
    
    char* ZeroString(char *str, int size)
    {
    	int i;
    
    	for (i = 0; i < size; i++)
    	{
    		str[i] = 0;
    	}
    
    	return str;
    }
    
    int main(int argc, char *argv[])
    {
    	FILE *f;
    	char *buf;
    	char filename[] = "test.txt";
    	int size = 0, len = 0;
    	int i = 0, j = 0, k = 0;
    	char **token = NULL;
    	char *tmp_token = calloc(size_of_token, sizeof(char));
    
    	f = fopen(filename, "r");
    	if (f == NULL)
    	{
    		printf("Error opening file %s\n", filename);
    		return -1;
    	}
    	fseek(f, 0, SEEK_END);
    	size = ftell(f);
    	rewind(f);
    
    	buf = calloc(size + 1, sizeof(char));
    
    	while (fgets(&buf[strlen(buf)], size, f) != NULL)
    		;
    
    	fclose(f);
    
    	len = strlen(buf);
    
    	token = calloc(len / 2, sizeof(char *));
    	for (i = 0; i < len/2; i++)
    		token[i] = calloc(size_of_token, sizeof(char));
    
    	for (i = 0; i < len; i++)
    	{
    		if (buf[i] != ' ' && buf[i] != '\n' && buf[i] != '\t')
    			tmp_token[j++] = buf[i];
    		else if (tmp_token[0] != 0)
    		{
    			j = 0;
    			strcpy(token[k++], tmp_token);
    			ZeroString(tmp_token, size_of_token);
    
    		}
    	}
    	strcpy(token[k++], tmp_token);
    
    	f = fopen("token.txt", "w");
    	
    	//printf("", &buf);
    
        ZeroString(buf, strlen(buf)+1);
    	for (i = 0; i < k; i++)
    	{
    		strcpy(&buf[strlen(buf)], token[i]);
    		strcpy(&buf[strlen(buf)], "\n");
    	}
    
    	printf("%s", buf);
    	fputs(buf, f);
    
    	fclose(f);
    
    	return 0;
    }

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    11
    Hmmm, I think it has something to do with the two lines:

    Code:
    		strcpy(&buf[strlen(buf)], token[i]);
    		strcpy(&buf[strlen(buf)], "\n");
    more specifically the
    Code:
    strlen(buf)
    function, because if I replace those two functions with the
    Code:
    strcat
    it works, fine. So therefore I would assume that the
    Code:
    strlen
    function is misbehaving.

    Odd:
    Code:
        ZeroString(buf, strlen(buf)+1);
    	printf("%d\n", strlen(buf));
    this prints out
    Code:
    1059
    Is there something wrong with the
    Code:
    ZeroString
    function I wrote??

    Ooo dear am I confused.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    11
    I have come to the conclusion that the IDE was acting up, because when I copy->pasted the exact same source code in a new project in the same IDE (Pelles C) it worked. So I can only conclude that IDE got a bit crazy on me. I guess this topic is solved.

    Thank you all, and have a nice day.

    Bye.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 09-12-2009, 01:10 PM
  2. Replies: 2
    Last Post: 02-04-2008, 02:34 AM
  3. Compiler seg fault
    By Buckshot in forum C++ Programming
    Replies: 18
    Last Post: 06-16-2005, 09:35 AM
  4. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM