Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
This is my code:
int *p;
p=4;
printf("p is %p\n",p);
free(p);
//need p=NULL but I don't
int *q;
q=5:
printf("q is %i",*q);
Then the error comes.
I just need explanation for it.
int *p;
is a pointer to an int.
p = 4;
makes it point to the address 0x4
free(p);
try to deallocate the address 0x4
basically you are trying to free a ressource that can not be freed.
int *q;
q = 5;
points q to the address `0x5;
*q;
reads from the 0x5 address, which most likely will crash. (also this address is not aligned).
Pointers are not integers... the program you wrote shows a lack of understanding of what pointers are and why/how they should be used.
Int *p; // You had better use int* p = NULL; we don't like wild pointers
//p = new int;
p=4; // I guess you want put the value of 4 to a variable, so *p = 4;
printf("p is %p\n",p);// printf("p is %d\n",*p);
free(p);//just correct in theory, no one can tell what happens in reality.
/*
if ( NULL != p )
{
delete p;
p = NULL;
}
*/
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Why this version of C code is not working (cause segmentation fault)
#include <stdio.h>
int main()
{
int *p;
*p = 10;
return 0;
}
while this one is working?
int main()
{
char c = 'c';
int *p;
*p = 10;
return 0;
}
Both code snippets are wrong, we can't say that one is more wrong than the other, p is uninitialized in both cases and therefore it may or may not contain a valid memory address, it's impossible to predict, this means that the behavior is undefined. That being the case, working, whatever that may mean, is well within the realm of possible outcomes.
For int* p, no storage space is allocated for the actual integer number.
You need to modify your code as follows to make it work properly.
In C++, you can use new, but in C you can use malloc.
#include <stdio.h>
int main()
{
int* p = malloc(sizeof(int));
*p = 10;
free(p);
return 0;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I dunno why this doesn't work. the code has a problem with the *c in
charToInt function but should be a legal statement in c. at least so I thought. I am excited to learn something new here.
int charToint(char *c) {
return *c - '0';
}
int main(void) {
char c = '3';
printf("%d\n", charToint(c));
{
You're passing a char to a function that expects a char *. Your compiler should have warned you about this. The value of that char is then interpreted as a pointer value and dereferenced. Dereferencing an invalid pointer invokes undefined behavior, which in this case results in the program crashing.
The function is ultimately trying to work with a char, so change it to accept a char:
int charToint(char c) {
return c - '0';
}
Alternately, you can leave the function as it and pass it a pointer:
printf("%d\n", charToint(&c));
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
double *p;
p = malloc(sizeof(p));
if (p != NULL)
{
*p = 5.15;
}
For some reason, p = malloc(sizeof(p));doesn't work. I try to allocate as much memory as p needs. What is wrong with that?
I try to allocate as much memory as p needs.
p itself (as a variable) has got the (own) memory allocated, what you're trying is basically allocate the memory for which p will be pointing to.
Here, p points to a double, so it needs to have a memory area to be able to store a double value. So, the allocation should be equal to the size of a double, i.e,
p = malloc(sizeof*p);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am writing this in C:
char *IP = malloc(12 * sizeof(char));
But I get "Initializer is not a constant
Any ideas?
You are trying to assign a value to a variable outside of any function. In this case, you can only assign constant values, which are not the result of function calls or operations. For example, you can do
int i = 3;
but not
int i = pow(2, 2);
For what you want to do, you can declare the variable in the global scope, but then assign a value in the main.
try this
char *IP;
...
IP = malloc(12 * sizeof(char));//in main
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have this code that gives a pointer an address and print it, but why that does not work??
void main()
{
int *b = (int*) 32;
printf("%d\n",b[0]);
}
b[0] dereferences an array which points to memory you haven't allocated. The effects of doing this are undefined. You might get a value returned or your program may crash if address 32 isn't readable from your process.
int *b = (int*) 32;
above code assigns memory address 32 to this pointer, i don't think that is you want, you will get access denied error when you call printf,
hope the following codes is useful toyou
int a = 32;
int *b = &a;
printf("%d\n",b[0]);
//output 32
printf( "%d\n", &b);
// output b pointer address.