I want to be clear about all the advantages/disadvantages of the following code:
{
char *str1 = strdup("some string");
char *str2 = "some string";
free(str1);
}
str1:
You can modify the contents of the string
str2:
You don't have to…
I am getting an invalid read error when the src string ends with \n, the error disappear when i remove \n:
#include
#include
#include
int main (void)
{
char *txt = strdup ("this is a not socket terminated…
For example I have the following struct:
struct Student{
char *studentName;
int studentAge;
struct Student *next;
};
I have many instances of the Student struct (for many different students) in a linked list. Each variable within…
I'm writing code and a good portion of it requires returning wchar arrays. Returning wstrings aren't really an option (although I can use them) and I know I can pass a pointer as an argument and populate that, but I'm looking specifically to return…
What's the difference between the following code :
char* str = "this is a string"
From this one :
char* str = strdup("this is a string")
The usage scenarios ?
I am doing a small Project. I am checking about memory leaks using the tool Valgrind. When I use this tool, I got the bellow information.
> 584 bytes in 74 blocks are definitely lost in loss record 103 of 104
> ==4628== at 0x402BE68: malloc (in…
In the below code snippet can i replace char * to const char * and remove the strdup() function call and directly take the optarg value set by getopt()? I am advised to use const char * to skip the strdup function usage. Appreciate the help in…
I'm trying to free g_strdup but I'm not sure what I'm doing wrong.
Using valgrind --tool=memcheck --leak-check=yes ./a.out I keep getting:
==4506== 40 bytes in 10 blocks are definitely lost in loss record 2 of 9
==4506== at 0x4024C1C: malloc…
This is program in book “The c programming language”.
There is an error:conflicting types for 'strdup'! When encounter function 'strdup'.But if you change 'strdup' to other name, for example 'strdu', the error will disappear.
I don't know WHY? By…
With:
char *x = malloc(1024);
strcpy(x, "asdf");
x = strdup(x);
free(x); // OK
free(x); // Segfault
If I just free it once, will I still be leaking? And if so, how to avoid it?
I have seen strdup used in code samples on StackOverflow and have just tried to use it on Linux (3.0.0-21-generic x86_64).
The compiler (clang) knew it was in string.h, but still complained about not having a prototype even with string.h included.…
Throughout the programs I inherited from my predecessors, there are functions of the following format:
somefunc(some_type some_parameter, char ** msg)
In other words, the last parameter is a char **, which is used to return messages.
That is:…
I tried the following configuration with mmap:
open file (file is over 2 kB)
request statistics from file *f_file*
map file (file is smaller than a page, offset page 0, size is expected size)
verify values of *f_footer* in the map *f_fpage*
usage…
If an input const string is being modified in some way (which is resulting in C compiler warning), what is the best way to handle it - typecasting it to a new variable and then using it OR duplicating it and using it and then freeing it. Or is there…