Question:
C programming: Why it happens?
2013-07-14 04:40:58 UTC
When I put initialize a char type pointer as:

char *string= '+' ;
*(string + 1)='\0' ;
puts(string);

in a program..The program crashes after reaching this line of code.

But if i declare it as follows no error occurs:

char *string;
string = (char*)malloc(2);
*string = '+';
*(string+1) = 0;
puts(string);

Please tell me why it happens.
Five answers:
green meklar
2013-07-14 22:31:06 UTC
In the first example, you set the value of the pointer to '+', i.e. the ASCII value 0x2b. Unsurprisingly, that's not a valid memory location and when you try to write data to the next position in memory (0x2c), you get a segfault.



If you did this, on the other hand:



char *string= "+" ;

*(string + 1)='\0' ;

puts(string);



it might actually work. The difference between single quote marks and double quote marks IS important, you cannot just substitute one for the other.
cja
2013-07-14 17:18:54 UTC
In the first fragment your compiler might have given you this warning:



    warning: initialization makes pointer from integer without a cast



... on the declaration: char *string='+';



Check out this example:



#include



int main(int argc, char *argv[]) {

    char *string = '+';



    printf("string = %p\n", string);



    return 0;

}



#if 0



Program output:



string = 0x2b



#endif



So the memory location referenced by string is 0x2b, which is '+'. Where is that? Most likely a place to which you do not have permission to write. Hence the segmentation fault. No memory is allocated by that first fragment of code. What you've done in the second fragment is fine. If you want to use memory from the heap, you need to use malloc or calloc or realloc to get it. Also, don't forget to call free when you're done using dynamically allocated memory.
_Object
2013-07-14 20:29:39 UTC
In the first case:

You never create a string. You initialize a pointer with the integer value for the character '+'.

This is somewhere around 40, I guess, without checking.

On most architectures, the low application address is 0x00010000 (I polled this from an error info screen I wrote), so essentially you've equivalently initialized the pointer to NULL.

Than you get a segfault because you've tried to write to an address which below that which the computer considers a valid address for user mode apps, and that you've never allocated the next space over. Either one of those problems causes a potential segfault, but writing NULL, or near null (below address 0x00010000) guarantees it.

The second case, you actually allocate the memory the address points to (that isn't pointing to address '+'), and the next space over for another char as well.

0 and '\0' compare equal, and everything is valid.
?
2016-12-30 14:07:27 UTC
You delete each and all the data that enable any courses on your laptop to run. in case you attempt to delete it, domicile windows will likely develop an errors asserting that this is locked via the working gadget (or if not, an errors that asserts that the data are in use). there are a thank you to get around those errors, yet they require slightly command instant understanding. If the folder does take place to get deleted, the laptop will probable nonetheless have the skill to start, yet a great sort of the courses that have been on the laptop will purely not run simply by fact they technically do not exist anymore. in case you actual need to brick a domicile windows working gadget, then delete the "domicile windows" folder, which includes all the data required via the working gadget to run. (as quickly as back it particularly is much extra stable to do once you're on the OS which you're attempting to delete simply by fact the OS itself will attempt to circumvent this in any respect expenditures, and calls for which you quickly give up some needed centers from working to realize this.
jplatt39
2013-07-14 05:19:35 UTC
Because you didn't explicitly allocate two spaces in the first example. Computers do exactly what we tell them to so in your first example it allocates one space for the char. To change the size of a malloc'd block of text use realloc or it will also crash.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...