Thread: Getting garbage after I malloc space...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    22

    Thumbs down Getting garbage after I malloc space...

    So my goal is to get the name of the directory by itself.
    But when I print it at the end, I get the name + some garbage.
    I don't understand why, since I am malloc'ing the right amount of characters...

    Code:
    int main () {
    	char address[] ="./testrun/forfun";
    	char *dirName;
    	char *pch;
    	int i = 0, lastslash = 0;
    
    	pch = strrchr(address, '/');
    	lastslash = pch - address + 1;
    	pch = NULL;
    	dirName = (char *)malloc(sizeof(char) * (strlen(address) - lastslash));
    
    	while (address[lastslash] != '\0'){
    		dirName[i] = address[lastslash];
    		i++;
    		lastslash++;
    	}
    
    	printf ("%s\n", dirName);
    
    	return 0;
    }
    Would somebody help me?
    I have tried doing realloc on dirName inside the while and all, but it doesn't like that either...

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    First, I presume you're including stdio.h, stdlib.h, and string.h. Second, I'd recommend not casting malloc(). There's no need for it and it might just be hiding a warning indicating that you forgot stdlib.h.

    The null character (that is, the '\0' character) is part of a string. If you're making a new string, you have to remember to allocate enough space for it, and you must actually write a null character to the new string. Since you did neither of these, you're not actually creating a string and thus your call to printf() invokes undefined behavior.

    There's also no need to multiply by sizeof(char), since sizeof(char) is always 1.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    The problem could be due to you allocating only 6 chars, forgetting about the null chararacter in "forfun". Also, note that sizeof(char) is always 1.

    Incidentally, you should use strncpy() instead of an explicit loop to copy over the characters. You would only need to use pch and compute the length of dirName (which you have to do anyway to malloc(), but note that the length is one less than that for malloc()). Remember to use free() when you are done.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You need to append a \0 to make it a proper C string.
    And see the FAQ on casting the result of malloc (in a word, don't).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. Out of space when compiling kernel
    By NuNn in forum Linux Programming
    Replies: 3
    Last Post: 04-01-2009, 02:43 PM
  3. Garbage Collection
    By Orborde in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2005, 11:18 PM
  4. malloc always setting length of 8?
    By Zarkhalar in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2004, 11:36 PM
  5. help with malloc
    By geetee in forum C Programming
    Replies: 1
    Last Post: 10-23-2001, 12:07 PM