I've run into an issue with our code under clang involving realloc. This code works fine under gcc and visual studio, so I'm interested in understanding clang's behavior. Our code does something similar to this (error handling etc elided):
//…
I am learning C from the book by K.N.King and am going through the programming projects for chapter 17. I came across a self inflicted bug while I was completing the first project. It was undefined behaviour of the program since I was passing…
Many people recommend doing the following to reallocate memory:
int *temp=realloc(previousVar,newSize);
if(temp==NULL){
printf("error\n");
exit(-1);
}
previousVar=temp;
I understand that, if no new variable was created, the previous pointer would…
I am trying to dynamically reallocate memory for an array of structs (actually an array each of 2 structs but 1 included here for simplicity) that is being read from/to a file or inputted by the user.
typedef Struct
{
char surname[21];
char…
From c99:
The realloc function deallocates the old object pointed to by ptr and returns a pointer to a new object that has the size specified by size. The contents of the new object shall be the same as that of the old object prior to deallocation,…
The man page of realloc() says:
The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the…
I wrote this code, but inserts garbage in the start of string:
void append(char *s, char c) {
int len = strlen(s);
s[len] = c;
s[len + 1] = '\0';
}
int main(void) {
char c, *s;
int i = 0;
s = malloc(sizeof(char));
while…
Say we have:
char *a = malloc(sizeof(char*));
char *b = realloc(a,sizeof(char*));
Can we safely say that b does not alias with a? The realloc reference page says that
The original pointer ptr is invalidated and any access to it is
undefined…
I have below a simplified C program that demonstrates the problem. When I try to call realloc using a pointer, it works fine, but if I try to add an offset to the pointer(i.e. to start from a later element in an array), it fails.
# include…
I read about dynamic memory allocation in C using this reference.
That document Say's :
realloc() should only be used for dynamically allocated memory. If the
memory is not dynamically allocated, then behavior is undefined.
If we use realloc()…
Reading the Rust subreddit today I came across comments that:
jemalloc is optimized for (multithreaded) speed, not memory usage
After doing more research I found that there are even more alternatives (such as calloc).
I would like to understand…
Say I declared an array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. Later I want it to be array[8] = {2, 3, 4, 5, 6, 7, 8, 9}.
Dismiss the first 2 elements. So it would start on array[2]. Reallocing array to array[2].
I tried:
int…
I apologize for the lengthy code. I have a simple question, but I thought I include my code so it will be clear where I am coming from.
I get a realloc corruption. I think the corruption is because I am not freeing correctly. In reality I am not…
DISCLAIMER: I'm new to C.
What is the best way to convert every line in a .txt file (can be other file types too) to a dinamic calloc() array, and the other way around?
In my file I have to following:
1 Hello
2 18
3 World
4 15
etc...
I want…