Getting garbage after I malloc space...

This is a discussion on Getting garbage after I malloc space... within the C Programming forums, part of the General Programming Boards category; So my goal is to get the name of the directory by itself. But when I print it at the ...

  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
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    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
    22,127
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,755
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21