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
I have the following code:
void func(uint8 *var) {
uint8 tempvar;
if (var)
var = &tempvar;
*var = 0;
}
I call the function using:
func(NULL);
The code gives a segmentation fault at the line "*var = 0;" because var still points to the memory address 0x0. I don't understand why my assignment to a temporary variable did not work!
Because you omitted the !. You are testing whether the variable exists, but you should test whether it doesn't exist: if (!var) ...
To expand a bit further... var is of type uint8 *, thus var itself is a pointer. By writing if (var) you are testing if that pointer is not NULL. In pseudo-code, your code says:
if (the var pointer already exists)
assign a new pointer to it (make it point to somewhere else)
But if it doesn't exist (if it points to NULL), you leave it alone. You can verify this using a debugger or a simple print statement. Thus you end up at the assignment with a null-pointer, and crash your program.
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 15 days ago.
Improve this question
I've been trying to learn my way through the winapi in C, and I'm trying to use the VirtualAlloc() function to allocate memory. The function clearly takes 4 arguments and I'm supplying 4 arguments as per the windows API documentation.
Function body:
VirtualAlloc(
lpAddress,
dwSize,
flAllocationType,
flProtect
);
How I'm supplying the function:
//values I inserted here have the correct types, again the problem is that it complains about few arguments
void* memory = VirtualAlloc(
NULL,
DRAWING_AREA_MEMORY_SIZE,
MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE
);
I tried using only one allocation type instead of piping, just in case that caused the problem for some reason, but to no avail
Error Copied from the compiler:
Error (active) E0165 too few arguments in function call
I can't tell what could be the problem. Funny enough, no matter how many other arguments I throw in, it stays too few and never changes to too many. What is going on here?
Problem was an extra parenthesis in the DRAWING_AREA_MEMORY_SIZE macro. Thanks to #ChrisKushnir and #IInspectable for leading me to the solution
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 months ago.
Improve this question
my code is not identifying type string. I am using c to program
In your calls to count_letter(), count_words() and count_scenteces() you include a unnecessary string type in the function argument.
You should only include string when declaring a variable of some sort for example:
string x = "hi";
So to fix this problem replace each instance of string text in your function calls with text.
This will reference the text variable instead of inappropriately introducing a new variable of type string into the program.
Parameter types are only put in function declarations, not function calls.
int letters = count_letters(string text);
should be
int letters = count_letters(text);
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
Given the following function:
image_ret* minify_1(image_src img_src, CLIENT* cl) {
image_ret* img_ret;
magickminify_init();
magickminify(img_src.image_src_val, img_src.image_src_len, (ssize_t*)&img_ret->image_ret_len);
return image_ret;
}
The compiler is telling me "expected expression before ‘image_ret’" with regard to the last line. I'm sure I'm missing some fundamental aspect of syntax here, but I don't know what. Lil' help?
You need to return a value, not a type. image_ret is a type, img_ret is a poitner to a value of that type and probably what you want to return, except I see nowhere in your code where you allocate any storage to it, or initialising any of the fields except image_ret_len
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
I'm not quite sure what is wrong with my current program and I have reach a bit of a road block:
(*ptr).Name = (char*)malloc(strlen(record+1));
strcpy((*ptr).Name, record);
free((*ptr).Name); //problem area
*ptr is a pointer that points to a structure that has various fields. After I copy some data into the Name field I want to free my allocated memory. When I step through my program I get no errors rather just a hanging program that will not continue after I try and free the memory. Any ideas? Thank you.
(*ptr).Name = (char*)malloc(strlen(record+1)); //This is the problem!
strcpy((*ptr).Name, record);
free((*ptr).Name); //problem area //Better practice to use free(ptr->Name
Fix:
ptr->Name = (char*)malloc(strlen(record)+1); //(record+1) in previouse code was doing
//the opposite of what it was intended to do
strcpy(ptr->Name, record);
free(ptr->Name);
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
I saw this if statement and I am not sure exactly how that works, what is being compared with what? Is there a lack of && or ||? The parens are confusing me.
if ((list->func)((list->head)->dataPointer, newOb) < 0) {
what is being compared with what? The parens are confusing me.
The result of the part between if( and ) is being compared with 0, like in every if statement.
Is there a lack of && or ||?
I don't see any && or ||
The parens are confusing me.
What (list->func)((list->head)->dataPointer, newOb) does is (not necessarily in this order):
Evaluate list->func
Evaluate (list->head)->dataPointer
Call list->func passing the two arguments (list->head)->dataPointer and newOb.
The result of this is then compared to 0 because it's what's between the if( and ).
Okay, so the first set of parentheses are indicating that list is a pointer to an object which has a member called func. func is a function. So (list->func) is a function call. The function apparently takes two arguments. The first argument that is being passed in is (list->head)->dataPointer. (list->head) indicates that the object that the pointer called list is pointing at has a member called head. head is also a pointer, and it points at an object with a member called dataPointer. The second argument that is being passed to the function (list->func) is newOb. The function (list->func) is apparently returning some sort of number value, probably an int.
The code could be rewritten as:
if( list->func( list->head->dataPointer, newOb ) < 0 )
if that helps you see what is going on.