Valgrind complains with “Invalid write of size 8”

This looks wrong: Should probably be: (spaces added for convenience). EDIT: Some additional explanation: When you say return_data[i]=… you are trying to write something into return_data[i]. Now, return_data is char**, so return_data[i] is char*. So you are writing a pointer into some location in memory. It looks like your pointers are 8 bytes long (which is fine), but you’ve only allocated 6 bytes: MAX_SOURCE_STRING+1. So … Read more

Incorrect checksum for freed object on malloc

In read_response, you are probably overwriting the end of the buffer pointed to by buf. The problem is that buf is a pointer, so sizeof(buf) will return the size of a pointer (probably 4 or 8 depending on your CPU). You are using sizeof as if buf were an array, which is not really the same thing as a pointer in C although … Read more

How to use EOF to run through a text file in C?

How you detect EOF depends on what you’re using to read the stream: Check the result of the input call for the appropriate condition above, then call feof() to determine if the result was due to hitting EOF or some other error. Using fgets(): Using fscanf(): Using fgetc(): Using fread(): Note that the form is the same for all of them: … Read more

too many arguments for format [-Wformat-extra-args]

Your fprintf call has 8 format specifiers but passes 9 further arguments to fill these. The 8th format specifier is %d; the argument corresponding to this is Item[i]->Name. The warning is telling you that Item[i]->Name is a string so can’t (shouldn’t) be converted to a signed integer. I presume Item[i]->Price has type int; you then either need to add an extra %s to your format string … Read more

Categories c Tags

Reading string by char till end of line C/C++

You want to use single quotes: Double quotes (“) are for strings, which are sequences of characters. Single quotes (‘) are for individual characters. However, the end-of-line is represented by the newline character, which is ‘\n’. Note that in both cases, the backslash is not part of the character, but just a way you represent … Read more

How to simply convert a float to a string in c?

The second parameter is the format string after which the format arguments follow: %f tells fprintf to write this argument (amount) as string representation of the float value. A list of possible format specifiers can (for example) be found here.

Categories C Tags

What’s the difference between nexti and stepi in gdb?

stepi is more detailed than nexti. if you call sum() from main() function then doing stepi reaches you inside the sum() function, but nexti doesn’t. Below is the screenshot when you call stepi when you were at call of sum() instruction (i.e., => 0x08048403 <+40>: call 0x8048419 <sum>). The stepi instuction routes you inside the … Read more

Allocating string with malloc

malloc() returns a void* pointer to a block of memory stored in the heap. Allocating with malloc() does not initialize any string, only space waiting to be occupied.To add a null-terminating character, you either have to do this yourself, or use a function like scanf(), which adds this character for you. Having said this, you … Read more