C Concat String Int Sprintf [duplicate] - c

This question already has answers here:
Sprintf Segmentation Fault
(5 answers)
Closed 5 years ago.
why is the the second sprintf not working?
char* jc;
char* tn;
char* result = malloc((256)*sizeof(char));
int thread=99;
int jobcounter=88;
sprintf(jc, "%d", jobcounter);
sprintf(tn, "%d", thread);
strcpy(result,"file_");
strcat(result,jc);
strcat(result,"_");
strcat(result, tn);
strcat(result,".html");
printf("%s",result);
Output:
file_88_Þ*m.html

In your case
sprintf(jc, "%d", jobcounter);
sprintf(tn, "%d", thread);
causes undefined behavior as none of those pointers (first arguments) point to any valid memory.
You need to make sure that the pointer(s) you're using to access a(ny) memory location points to a valid memory. You can either
make them point to statically/automatic allocated variable or
use a memory allocator function like malloc() or family.

Related

C. malloc() and free() in function doesn't work [duplicate]

This question already has answers here:
How do I modify a pointer that has been passed into a function in C?
(7 answers)
What happens to memory after free()?
(4 answers)
Unable to check memory allocation size with sizeof() [duplicate]
(2 answers)
Closed 2 years ago.
Can someone tell me, why I can't allocate memory to the struct array through the init() function? When done manually in main, everything is fine. When trying it through init() nothing happens (Also no error message). The adress is always 0x0, I guess the null pointer.
#define GAMES 100
typedef struct{
double *scores;
}SCORES;
void init(SCORES *arr);
int main(){
SCORES *numbers = NULL;
init(numbers);
printf("Adress is: %p\n", numbers); //Still 0x0
return 0;
}
void init(SCORES *arr){
arr = (SCORES*) malloc(GAMES * sizeof(SCORES));
}
Trying it with the code below works for malloc. I get an adress but if I use free(), memory is still allocated.
void init(SCORES **arr){
*arr = (SCORES*) malloc(GAMES * sizeof(SCORES));
}
...
init(&numbers);
...
free(numbers);
In the first code snippet you're passing the value of the numbers variable. In the function you change a local variable, and doing so has no effect on the variable is the calling function.
In the second snippet you correctly pass the address of numbers so it can be set in the function, and the call to free is also correct. Just don't attempt to use the value of numbers after you free it, otherwise it's undefined behavior.

Different output when printing a null char pointer with and without a newline appended [duplicate]

This question already has answers here:
What is the behavior of printing NULL with printf's %s specifier?
(4 answers)
Closed 5 years ago.
I tried the following code on Linux
#include<stdio.h>
int main()
{
char *p=NULL;
printf("%s",p);
return 0;
}
#include<stdio.h>
int main()
{
char *p=NULL;
printf("%s\n",p);
return 0;
}
The first one outputs: (null)
While the second one causes a segmentation fault.
Why does \n make such a difference?
Both of your examples are undefined behavior per standard. Calling printf with %s and passing a NULL pointer is UB.
Therefore it makes no sense to discuss the outcome. On one system you might get one result and on another system you get another result.
Also see https://stackoverflow.com/a/11589479/4386427

Why this code compile but not running [duplicate]

This question already has answers here:
What is the trick behind strcpy()/uninitialized char pointer this code?
(3 answers)
Closed 5 years ago.
Why this code compile but not running?
int main() {
char *s;
scanf("%15s", s);
puts(s);
}
Because s is an uninitialized pointer, you cannot store data there (since there "is no there there").
Try:
char s[32];
instead, that gives you 32 characters' worth of room into which scanf() can write.
You need to provide memory for scanf(...) The char *s is only a pointer to some memory, but not he memory itself. You can either malloc(...) the memory and have s point to it, or allocate it locally on the stack by char s[16]
For starters, provide a proper buffer to the scanf call. For example, instead of char *s which is simply an uninitialized pointer, try char s[128].

C Segmentation Fault when trying strcpy on char *pointer [duplicate]

This question already has answers here:
C segmentation fault-char pointers
(3 answers)
Closed 8 years ago.
I'm new in learning the C-Language and I have a question to pointers.
For Example if I try this:
char *pointer;
strcpy(pointer,"Hello, World!\n");
printf(pointer);
I get this Output:
Segmentation Fault
But if I try this:
char *pointer = "Hello, World!\n");
printf(pointer);
I get this:
Hello, World!
My question is why it isn't working with strcpy.
The functions do the same overall.
What is the difference between the first sourcecode and the second?
It would be good if someone could explain what is happening in the memory, so that I can get a better view at this.
char* pointer merely gives you a variable to use to access the memory location. You've not yet allocated any memory, so when you do the strcpy you're writing to whatever random/undefined value pointer has.
You need to do something like:
char* pointer = calloc(LEN);
if (pointer)
{
strcpy(pointer, "Hello World");
printf(pointer);
free(pointer);
}
The first parameter to "strcpy" should be pointing to a usable location in memory. In your first source code, you havent initialized "pointer" to anything. You should first initialize it, for example by declaring it to be an array of characters of maximum length:
char myArray[42]; // where 42 represents the maximum capacity

Difference between arrays and pointers in c? [duplicate]

This question already has answers here:
Is an array name a pointer?
(8 answers)
Closed 9 years ago.
I am really confused in arrays and pointers.
Please tell me What is difference between following two codes?
int main()
{
int i,*p;
for(i=0;i<5;i++)
{
p[i]=i;
printf("%d",p[i]);
}
return 0;
}
int main()
{
int i,p[5];
for(i=0;i<5;i++)
{
p[i]=i;
printf("%d",p[i]);
}
return 0;
}
First one results in undefined behaviour.
For not having UB you need to allocate memory using either malloc or calloc.
Allocating memory will store the data in heap. After you done with your task , you need to free the allocated memory also.
Second one do not result in UB. it stores the array data in stack and not on heap.
Memory is automatically freed from stack once the scope is over.
In first p points to garbage location (not-allocated), and I'm pretty sure that in the way you are using it will generate a segmentation fault. You should allocate memory first, before using it, like:
p = malloc(5 * sizeof(int))
Second is allocated on stack and have the lifetime of the scope it is declared in.

Resources