[Question] C Program crashes after malloc menu

Shout-Out

User Tag List

Results 1 to 8 of 8
  1. #1
    d3rrial's Avatar Contributor Authenticator enabled
    Reputation
    127
    Join Date
    Apr 2010
    Posts
    527
    Thanks G/R
    0/5
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Question] C Program crashes after malloc

    Hey Guys, I have a problem: My Program always crashes when I change values inside a variable that I allocated with malloc. I can't even free the program because it will just crash.

    I also I can't find anything wrong with the code, so could someone please explain to me why my Program always crashes? This is quite important, tonight is the deadline

    Thanks alot!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define Y 30
    int nrreturn();
    
    int main(){
    	char** feld;
    	int i,j, z;
    	feld=(char**)malloc(sizeof(char*)*nrreturn());
    	for(i=0;i<Y;i++){
    		feld[i]=(char*)malloc(sizeof(char)*nrreturn());
    	}
    	for(j=0;j<Y;j++){
    		for(i=0;i<Y;i++){
    			feld[j][i]=0;
    		}
    	}
    	
    	for(j=0;j<nrreturn();j++){
    		for(i=0;i<Y;i++){
    			printf("%d",feld[j][i]);
    		}
    		while(z<1000){
    			z++;
    		}
    		z=0;
    		free(feld[j]);
    		printf("\n");
    	}
    	free(feld);
    	return 0;
    }
    
    int nrreturn(){
    	return 5;
    }
    The Console Output:

    Code:
    X:\perm\perm2>main
    000000000000000000000000000000
    Then after that there's an error message, that the program isn't reacting anymore.

    Thanks alot again!

    Edit 2: This is not the real program, this is more or less a debugging program I used here that is supposed to show the effects of my real program. (Which for example uses a value gathered from a function to determine how many rows the array is supposed to have)
    Last edited by d3rrial; 04-18-2012 at 08:54 AM.

    [Question] C Program crashes after malloc
  2. #2
    macpain78's Avatar Private
    Reputation
    1
    Join Date
    Apr 2012
    Posts
    1
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by _SkHiEnEgP_ View Post
    for(j=0;j<Y;j++){
    You allocated nrreturn not Y.

    So when you try to acess feld[5][0] it segfault.
    The correct statement would be:
    for(j=0;j< nrreturn() ;j++){

    Which, you wrote 3 lines later

    EDIT: feld[5] not 6 sry
    Last edited by macpain78; 04-18-2012 at 05:10 PM.

  3. #3
    d3rrial's Avatar Contributor Authenticator enabled
    Reputation
    127
    Join Date
    Apr 2010
    Posts
    527
    Thanks G/R
    0/5
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    
    #define X 5
    #define Y 20
    
    int main(){
    	int **feld;
    	int i, a;
    	
    	feld = (int**)malloc(X*sizeof(int*));
    	
    	for(i=0;i<5;i++){
    		feld[i]=(int*)malloc(Y*sizeof(int));
    	}
    	
    	feld[0]=(int*)"Hallo";
    	feld[1]=(int*)"Welt!";
    	feld[3]=(int*)"\n";
    	printf("%s %s%s",feld[0],feld[1],feld[3]);
    	for(i=0;i<X;i++){
    		free(feld[i]);
    	}
    	free(feld);
    	return EXIT_SUCCESS;
    }

    That does the very same error. It just crashes in the middle of freeing the allocated memory!

    Also I just used int** this time to demonstrate that this is not a problem of char**
    Last edited by d3rrial; 04-19-2012 at 05:24 AM.

  4. #4
    _Mike's Avatar Contributor
    Reputation
    310
    Join Date
    Apr 2008
    Posts
    531
    Thanks G/R
    0/2
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First, why are you casting the return from malloc? And if your answer is "because I have to" then this isn't C.
    What compiler are you using?

    Secondly,
    Code:
    feld[0]=(int*)"Hallo";
    feld[1]=(int*)"Welt!";
    feld[3]=(int*)"\n";
    You are overwriting your pointers to the allocated memory with pointers to (in the context of free() ) garbage. And again, why the casts? If you have to cast something to get around a compile error then that's often a big hint that you are doing something terribly wrong.
    Strings should be pointer to char. And to be strict, string literals should be pointer to const char.

    Thirdly, using the = operator with a string does not copy the string itself. It copies the pointer to the string. Look up strcpy() or in your case preferably strncpy() since you are using fixed length buffers.

    And from the printf in your first post; %d expects an int-sized argument, not a char. See printf format string - Wikipedia, the free encyclopedia the length field specifically.
    Last edited by _Mike; 04-19-2012 at 07:43 AM. Reason: typos

  5. #5
    Arlyh's Avatar Corporal
    Reputation
    24
    Join Date
    May 2010
    Posts
    25
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I hope this helps you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    //#define Y 30  //This is unused
    int nrreturn();
    
    int main(int argc, char **argv) {
    	char **feld;
    	int i, j, z = 0;  //it is dangerous to use an uninitialized variable, z could be set to a very high value, causing no loop or a very long loop based on sign.
    	feld = (char **)malloc(sizeof(char *) * nrreturn());
    	//for(i = 0; i < Y; i++) {	//Y = 30, but feld only has enough memory allocated for nrreturn() elements
    	for(i = 0; i < nrreturn(); i++) {	
    		feld[i] = (char *)malloc(sizeof(char) * nrreturn());
    	}
    
    	//This has the same problem, you've only allocated 5 elements of 5 char's each, but you're assigning 30 elements of 30 char's.
    	/*for(j = 0; j < Y; j++) {
    		for(i = 0; i < Y; i++) {*/
    	for(j = 0; j < nrreturn(); j++) {
    		for(i = 0; i < nrreturn(); i++) {
    			feld[j][i] = 0;
    		}
    	}
    	
    	for(j = 0; j < nrreturn(); j++) {
    		//for(i = 0; i < Y; i++) {	//same
    		for(i = 0; i < nrreturn(); i++) {	
    			printf("%d",feld[j][i]);
    		}
    		//why are we doing this?
    		while(z<1000) {  
    			z++;
    		}
    		z = 0;
    		free(feld[j]);
    		printf("\n");
    	}
    	free(feld);
    	return 0;
    }
    
    int nrreturn() {
    	return 5;
    }
    printf("%d", some_char) has worked forever, and I believe that printf() properly promotes the char to an int when used in this way. If you believe otherwise, you can just cast the char to an int.

    Assuming you're using Visual Studio, go to the properties page for your project, navigate to C/C++ -> Advanced, locate the Compile As property and change it to Compile as C. Otherwise just call this code C++, so no one gets confused.

  6. #6
    _Mike's Avatar Contributor
    Reputation
    310
    Join Date
    Apr 2008
    Posts
    531
    Thanks G/R
    0/2
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by Arlyh View Post
    printf("%d", some_char) has worked forever, and I believe that printf() properly promotes the char to an int when used in this way. If you believe otherwise, you can just cast the char to an int.
    Yes integer promotion would normally apply for function arguments, except the standard says that if the function prototype ends with ... then the promotion behavior is undefined. But on all the compilers I've tried it works anyways. Normally I wouldn't care but when someone is trying to learn the language they should be aware of the possible limitations. Once they understand the rules they are free to start breaking them
    Last edited by _Mike; 04-19-2012 at 08:51 AM.

  7. #7
    Arlyh's Avatar Corporal
    Reputation
    24
    Join Date
    May 2010
    Posts
    25
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by _Mike View Post
    Yes integer promotion would normally apply for function arguments, except the standard says that if the function prototype ends with ... then the promotion behavior is undefined. But on all the compilers I've tried it works anyways. Normally I wouldn't care but when someone is trying to learn the language they should be aware of the possible limitations. Once they understand the rules they are free to start breaking them
    I was actually looking for that before I replied but couldn't find it. I'm sure you're right regardless, as I was told the same thing a number of years ago. Thanks for explaining the issue

    Additionally, OP, if you could explain what you are trying to accomplish with the while(z < 1000) loop, perhaps we could help you find a better way to do it. I assume you're using this as a delay, but it's first a busy loop, so you're using system resources while it runs, and secondly it's too short to really accomplish much of a delay. All major operating systems have a sleep(uint ms) function, check windows.h for windows, and unistd.h (I believe) for linux.
    Last edited by Arlyh; 04-19-2012 at 02:13 PM. Reason: Question about delay

  8. #8
    d3rrial's Avatar Contributor Authenticator enabled
    Reputation
    127
    Join Date
    Apr 2010
    Posts
    527
    Thanks G/R
    0/5
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally Posted by _Mike View Post
    First, why are you casting the return from malloc? And if your answer is "because I have to" then this isn't C.
    What compiler are you using?
    I am using the Visual Studio compiler
    Secondly,
    Code:
    feld[0]=(int*)"Hallo";
    feld[1]=(int*)"Welt!";
    feld[3]=(int*)"\n";
    You are overwriting your pointers to the allocated memory with pointers to (in the context of free() ) garbage. And again, why the casts? If you have to cast something to get around a compile error then that's often a big hint that you are doing something terribly wrong.
    Strings should be pointer to char. And to be strict, string literals should be pointer to const char.

    Thirdly, using the = operator with a string does not copy the string itself. It copies the pointer to the string. Look up strcpy() or in your case preferably strncpy() since you are using fixed length buffers.
    I was casting because I didn't expect an Integer array being able to handly char array values correctly, I could have done it without it but I guess I did it as a "safety measure"
    Also: I didn't know that the = operator overwrites the pointer. Thanks a lot, that pretty much solved my problem! I know strcopy(), thanks.
    Edith: And I used casts, because I just learned it that way.
    And from the printf in your first post; %d expects an int-sized argument, not a char. See printf format string - Wikipedia, the free encyclopedia the length field specifically.
    I know that printf would expect an Integer argument, but if I had used char, I would have "outputted" (sorry I dont know the correct translation) the actual char stored in there but not the numeric value.
    Last edited by d3rrial; 04-19-2012 at 05:31 PM.

Similar Threads

  1. [Question] WoW keep crashing in new zones or discovery?
    By Frombehind in forum WoW ME Questions and Requests
    Replies: 1
    Last Post: 04-04-2008, 05:27 PM
  2. Ascent Crashes After Fully Loading
    By zachj95 in forum World of Warcraft Emulator Servers
    Replies: 11
    Last Post: 02-16-2008, 01:16 PM
  3. [Question] Head cutted half after model changing
    By stealthanie in forum WoW ME Questions and Requests
    Replies: 5
    Last Post: 02-08-2008, 11:44 AM
  4. [Question] Wich program?
    By spiratesss in forum WoW ME Questions and Requests
    Replies: 1
    Last Post: 12-27-2007, 08:11 AM
  5. [Question] The program closes for some weird reason.
    By MuppetShow in forum WoW ME Questions and Requests
    Replies: 0
    Last Post: 10-14-2007, 08:29 PM
All times are GMT -5. The time now is 09:22 AM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search