crash on malloc

This is a discussion on crash on malloc within the C Programming forums, part of the General Programming Boards category; I've written a function which is called a few times. After a few times, the program craches. I searched a ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    61

    crash on malloc

    I've written a function which is called a few times.
    After a few times, the program craches. I searched a little, and I found that it crashes when it reaches a malloc call (where it allocates type and value).

    This error doesn't make the program crash the first time, but after maybe 40 times. Does anyone know what I'm doing wrong ?
    Code:
    void fileProcessArg(char *arg, int where){
    	char *type, *value;
    
    	type  = malloc(sizeof(char) * SKY_FILE_MAX_ARG_LENGTH);
    	value = malloc(sizeof(char) * SKY_FILE_MAX_ARG_LENGTH);
    
    	// split arg in type and value
    	fileSplitsArg(arg, type, value);
    
    	// body of the function, uses type and value
    	// ...
    
    	free(value);
    	free(type);
    	free(arg);
    }
    In case it matters : here is the fileSplitArg() function.
    Code:
    void fileSplitsArg(char *arg, char *type, char *value){
    	char *t;
    	int count = 0;
    	int equals = 0;
    
    	t = arg;
    	while (*t != '\0'){
    		if (*t == '='){
    			*(type+count) = '\0';
    			equals = 1;
    			count = 0;
    			t++;
    		}
    
    		if (equals){
    			*(value+count) = *t;
    			count++;
    			t++;
    		}
    		else{
    			*(type+count) = *t;
    			count++;
    			t++;
    		}
    	}
    	*(value+count) = '\0';
    }
    Thanks in advance.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Are you actually allocating memory for arg? It's hard to tell what's going on without seing all of the relevant code.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User joed's Avatar
    Join Date
    Mar 2004
    Posts
    59
    try:
    Code:
    type  = (char *)malloc(sizeof(char) * SKY_FILE_MAX_ARG_LENGTH + 1);
    value = (char *)malloc(sizeof(char) * SKY_FILE_MAX_ARG_LENGTH + 1);

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Quote Originally Posted by joed
    try:
    Code:
    type  = (char *)malloc(sizeof(char) * SKY_FILE_MAX_ARG_LENGTH + 1);
    value = (char *)malloc(sizeof(char) * SKY_FILE_MAX_ARG_LENGTH + 1);
    I don't think that will help anything since the OP's not even checking the max length where it matters.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    Quote Originally Posted by joed
    try:
    Code:
    type  = (char *)malloc(sizeof(char) * SKY_FILE_MAX_ARG_LENGTH + 1);
    value = (char *)malloc(sizeof(char) * SKY_FILE_MAX_ARG_LENGTH + 1);
    This won't help since type and value will never reach SKY_FILE_MAX_ARG_LENGTH normally.

    Are you actually allocating memory for arg? It's hard to tell what's going on without seing all of the relevant code.
    I allocate memory for arg (I don't think the problem is located here) :
    Code:
    void fileProcessPoint(char *zin){
    	char *t;
    	char *arg;
    	int haak, te;
    	FilePoint *fp;
    	
    	printf ("[point]\n");
    
    	arg = malloc(sizeof(char) * SKY_FILE_MAX_ARG_LENGTH);
    	haak = 0;
    	te = 0;
    
    	t = zin;
    	while (*t != '\0'){
    		if (*t == '"'){
    			if (haak == 0){
    				haak = 1;
    				t++;
    			}
    			else if (haak == 1){
    				*(arg+te) = '\0';
    				fileProcessArg(arg, SKY_FILE_POINT);
    				haak = 0;
    				te = 0;
    				
    			}
    		}
    		if (haak == 1){
    			*(arg+te) = *t;
    			te++;
    		}
    		t++;
    	}
    	fp = maakFilePoint(cp);
    }
    
    // maakFilePoint() is implented like this : 
    
    FilePoint *maakFilePoint (Point p){
    	FilePoint *point;
    
    	point = malloc(sizeof(FilePoint));
    	
    	// harmless code
    	// ...
    
    	// playing with pointers, well tested, works.
    	addFilePointToList(point);
    
    	return (point);
    }

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    Looking at the code I just copied, I suddenly realised what the solution is.

    In fileProcessPoint(), it is possible that fileProcessArg() is called more than once. So if I deallocate arg at the end of fileProcessArg(), the program will still try to use arg even if it's already deallocated.

    The solution was to deallocate arg at the end of fileProcessPoint(), outside the while-loop.

    Although I found the solution, thanks anyway.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Not like we had a chance to answer...the first code you submitted had nothing to do with the problem you were having...
    If you understand what you're doing, you're not learning anything.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Also, stop typecasting the return value of the memory allocation functions. It isn't required, and actually should be avoided.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    76
    this has nothing to do with the thread but only with the last remark: "Also, stop typecasting the return value of the memory allocation functions. It isn't required, and actually should be avoided"


    im using devcpp and if i have the line:

    destBuf =malloc(MEDIUMBUFF) ;

    i get a nice "invalid conversion from `void*' to `char*'"

    so i wonder why it is so bad to use typecasts.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by johny145
    this has nothing to do with the thread but only with the last remark: "Also, stop typecasting the return value of the memory allocation functions. It isn't required, and actually should be avoided"


    im using devcpp and if i have the line:

    destBuf =malloc(MEDIUMBUFF) ;

    i get a nice "invalid conversion from `void*' to `char*'"

    so i wonder why it is so bad to use typecasts.
    Well for one, because you'd miss this message which tells you that you are using a C++ compiler. And if you're writing C++ code, why wouldn't you be using new/delete instead, or perhaps an STL container?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    76
    Ahh now i see .
    I am writing c and i also unchecked the "compile file as cpp option", but devcpp is still compiling as c++. Probabely because the file has a cpp extension or something.

    I made a new project and if you leave out the typecasts and if it compiles as C you indeed get no errors.

    Anyway, thanks for clearing this up.... i was under the impression i was already compiling as C.

  12. #12
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    when you compile a file, dev will ask you what to save it as, c or c++. if you choose nothing and you have it set as "compile as c++ on default",that's what you get, unless you implicity write the file as file.c

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Quote Originally Posted by caroundw5h
    unless you implicity write the file as file.c
    I think you mean explicitly
    If you understand what you're doing, you're not learning anything.

  14. #14
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by itsme86
    I think you mean explicitly
    that darn C lingo....sorry

  15. #15
    Registered User
    Join Date
    Feb 2005
    Posts
    61
    Quote Originally Posted by itsme86
    Not like we had a chance to answer...the first code you submitted had nothing to do with the problem you were having...
    I thought it had. Because I printf'ed untill nothing showed up on the screen. It printed 'ok' before the two lines I showd, and it didn't do anything after.

    Although I know that this doesn't mean (obviously) that the problem was there, it gave me an argument to believe it was there.

    Also, stop typecasting the return value of the memory allocation functions. It isn't required, and actually should be avoided.
    I know, someone told me that already in another thread. I did this to make sure that that wasn't the problem, and forgot to remove it. Because as you can see, the other code doesn't have malloc typecasts.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc function and program crash
    By totalnewbie in forum C Programming
    Replies: 16
    Last Post: 11-19-2008, 11:39 AM
  2. Malloc and strange system crash
    By VanJay011379 in forum C Programming
    Replies: 1
    Last Post: 09-14-2006, 12:13 AM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 11:26 AM
  4. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 08:08 AM

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