Hello,

i have 2 small doubts regarding pointers.

1)im trying to defererence 4 bytes as shown, and im initializiing each byte using a char pointer. But im getting runtime errors.

Code

char a = 65;
	char *ptr = (char *)malloc(4); 	
	int *ptr1;
	
	ptr  = &a;
	ptr1 = &a;		

	*ptr = 0;
        *(ptr + 1) = 0;
	*(ptr + 2) = 0;
	*(ptr + 3) = 1;

	printf("\n%d", *ptr1); // dereferencing 4 bytes here.

	
}

2)

char a = 65;
        char *ptr = (char *)malloc(4); 	
	int *ptr1;
	
	ptr  = &a;
	ptr1 = &a;		

	*ptr = 0;
        *(ptr + 1) = 0;
	*(ptr + 2) = 0;
	*(ptr + 3) = 1;

	printf("\n%d", *ptr1); // dereferencing 4 bytes here.

Now, i have not allocated memory for the char pointer but im trying to access subsequent addresses. But it did not give me runtime errors.

Can anyone please explain these two cases?

Thanks.

You might be getting errors from here.

char *ptr = (char *)malloc(4);

change to something like.

char * ptr = malloc(sizeof(char)*2)

might do something for you.

Can you please tell me what is the difference between the code snippet 1 and 2 ?
Also tell me which runtime error you are getting with code snippet 1.

Thanks for the replies.

@atramposch

i tried that. It did not work.

@harish_s

Here is the code:

http://ideone.com/IdOlO

im getting segmentation fault for it. In the first one, i allocated memory for it. In the second one, i did not. im really sorry. i did not paste it right.

Here is the second one:

char a = 65;
  char *ptr; 		
	
  ptr  = &a;			

  *(ptr + 1) = 0; // since im trying to dereference these bytes(without allocating memory, should it not be an error?
  *(ptr + 2) = 0;
  *(ptr + 3) = 1;

Actually there is no need to allocate memory to "ptr" because you are assigning address of "a" to it.
Obviously it is going to give you segmentation fault because you are assigning the address of a "char" to an "int *".
"int *" is suppose to point to a location of 4 byte at a time and you are assigning it a address of "char" whose size if one byte.
When you are dereferencing " *ptr1 " it assumes the address of "char" as if it is a address of "int" and tries to read 4 byte which results in segmentation fault.

to correct your program, replace
char a=65;
by
int a=65;

>>Actually there is no need to allocate memory to "ptr" because you are assigning address of "a" to it.

Well, im trying to try and access memory that i did not make it point to. So, will that not lead to illegal access? (im referring to the second one)

In the first case, im trying to dereference 4 bytes, but why an error? i had tried the following:

http://ideone.com/A96HK

That works fine.

In the statements
*(ptr+1) = 0
*(ptr+2) = 0
*(ptr+3) = 1
you are assigning some value to a location that is, somehow, not allocated to any resource in your process address space.
And that's why, somehow, your code snippet 2 is working fine.
Have you tried to print them using %c ?
I would like to give you one example.
1. Take one pointer, allocate memory to it.
2. Then assign a valid value to it.
3. Print its value. (it should print the value, you have assigned to it)
4. Then free your pointer (using free()).
5. Now print its value again, you will find that it will again give you the same value you have allocated to it.

This happens because when you free() a pointer, the memory allocated to has been recorded as free memory and that memory can be allocated to some other variable/pointer in future. But since you have immediately printing it after free(), it give you same value that you have assigned because pointer is still pointing to same memory which is now ILLEGAL to access.

Your code snippet 2 can also gives you segmentation fault at any time because you are accessing ILLEGAL memory using a pointer.

Thanks for the explanation.

I would like to give you one example.
1. Take one pointer, allocate memory to it.
2. Then assign a valid value to it.
3. Print its value. (it should print the value, you have assigned to it)
4. Then free your pointer (using free()).
5. Now print its value again, you will find that it will again give you the same value you have allocated to it.

Well i got a runtime error for that.
http://ideone.com/JWCaf

Also, i still did not understand why snippet 1 isn't working fine.

Anyone? i tried searching for it, but i couldn't find anything.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.