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);
Related
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;
}
*/
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 5 years ago.
Improve this question
Hello I am new to C and I am trying to print a string that i set by my self but it prints junk.
I know id[4] is '\0' so i did not set it.
int main(){
char id[5];
printf("Enter a string\n");
id[0]=1;id[1]=2;id[2]=3;id[3]=4;
printf("You entered the string %s\n",id);
}
I know id[4] is '\0'
Well, you're wrong.
id being an automatic local variable, unless initialized explicitly, it contains indeterminate value. So, you cannot be sure of any value, let alone '\0'.
Quoting C11, §6.7.9
If an object that has automatic storage duration is not initialized explicitly, its value is
indeterminate. [....]
However, if you initialize it like
char id[5] = {0};
then, by rule of initialization, all the elements are 0-initialized and you can then rely on the null-termination.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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
I have declared a char pointer in the following manner:
School *student[10];
for(i=0;i<10;i++){
*student[i] = malloc(sizeof(Student)); <--- Error points here
}
The error I get is:
incompatible types when assigning to type 'struct Student' from type 'void*'
Does anyone know why I am getting this error?
But how come if I were to allocate memory in the same line it would be with the star. For example: Student *name = malloc(sizeof(Student)); Why does this work? Im a bit confused
*student[i] = malloc(sizeof(School)); should be student[i] = malloc(sizeof(School));
students is an array of pointer to struct of type School. So you need to allocate for each pointer in that array. When you write *student[i] - you are dereferencing pointer i instead of allocating memory for it.
And as NicolasMiari pointed out, the sizeof operator must apply to School instead of student.
But how come if I were to allocate memory in the same line it would be with the star. For example: Student *name = malloc(sizeof(Student)); Why does this work? Im a bit confused
That's different. When you write Student *name = malloc(sizeof(Student)); you are both declaring a pointer and initialize it with malloc. You can do both steps in a single line like that. Alternatively, you declare it first, then assign it with malloc in a different line - in that case you must remove the asterisk.
You may want to refer to this question pointer initialization and pointer assignment.
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 8 years ago.
Improve this question
node *rever(node *root)
{
node *prev = NULL;
node *xnew = NULL;
while (root != NULL) {
xnew = malloc(sizeof(root));
xnew->value = root->value;
xnew->next = prev;
prev = xnew;
root = root->next;
}
return xnew;
}
Hello I wrote this linked list reverse function. However it doesn't work(empty response): I suspect it's because of prev index getting overwritten. Could someone explain me whats going on? I know I could find working code on the internet but I wanna know what am I doing wrong.
Thanks
This looks close to correct. One problem I see: sizeof(root) is the same as sizeof(node*). That is, it's enough space for a pointer to a node.
You need enough space for an actual node struct. So malloc sizeof(*root) bytes (or sizeof(node), which is the same).
xnew = malloc(sizeof(root));
That should really be sizeof(*root) — you want a memory block the size of the structure pointed to, not the size of the pointer. As a result, you're likely overwriting whatever happens to be sitting in memory just past the pointer-sized block you allocated, resulting in undefined behavior, which could include anything from working as expected to causing demons to fly out of your nose.
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 8 years ago.
Improve this question
struct t{
char days[20];
int date;
char x;
struct t *next;
}*head
printf("%ld\n", sizeof(head));
where sizeof(*void)=8, sizeof(int)=4, sizeof(char)=1
Why does it print 8?
head is a pointer to the struct t, which is 8 bytes since I'm assuming you're running an x64 program. If you want the size of the underlying type, do this:
sizeof(*head)
Notice that head is a pointer to the struct rather than an actual instance of the struct. This means that sizeof(head) is the size of the pointer, which on your system happens to be 8 (notice that sizeof(void*) is also 8).
Hope this helps!