C - malloc allocating too much memory - c

running int a strange scenario where malloc is allocating more memory than I ask for:
void function (int array [], int numberOfElements) {
int *secondArray = malloc(sizeof(int) * numberOfElements/2);
for (int i = 0; i < numberOfElements / 2; i++) {
secondArray[i] = array[i];
}
}
Let's say array is a some 10 numbers. When I print out secondArray after the above code, I get:
so first of all, the array should be 5 elements. But second, why the 0's in the end? I'm mallocing only space for 10/2 = 5 ints.
EDIT:
printing code:
for (int d = 0; d < numberOfElements; d++) {
printf("%i ", secondArray[d]);
}
hmm I might have just answered my own question here, I'm guessing it's the printing beyond secondArray that shows 0, not the array itself.
-
Actually, the problem is that I was also not doing this:
secondArray[numberOfElements] = '\0';
That is why it was printing beyond.

malloc is actually allocating exactly the right amount.
However, you're accessing memory beyond the allocation.
What exists there is completely undefined and could really be anything.
In your case, it was one "junk" number and four zeroes.

You are just lucky. Malloc can and sometimes does ask from more memory off the OS - Taking into account paging. Sometimes it does not even need to ask the OS for memory as it has asked for extra earlier. Therefore the malloc could ask for a page of memory - more that enough to satisfy your request and the extra memory happens to be filled with zeros.
You are in the land of undefined behaviour. So all bets are off.

/** its print 0 0 0 0 because in C no array bound if you define your array
* size is 4 but
* you want to store data more than array size you can store so you print your
* array.
* for(i = 0; i < numberOfElements; i++) its give data and 0 also because you
* store the data
* only 5 position but you print it max size so it give you 0 0 0
*/
int *secondArray = malloc(sizeof(int) * numberOfElements/2); // no matter either use it or
int *secondArray = malloc(sizeof(int));
// ^^^ this will take same memory

Related

why do I have a runtime #2 failure in C when I have enough space and there isn't many data in the array

I'm writing this code in C for some offline games but when I run this code, it says "runtime failure #2" and "stack around the variable has corrupted". I searched the internet and saw some answers but I think there's nothing wrong with this.
#include <stdio.h>
int main(void) {
int a[16];
int player = 32;
for (int i = 0; i < sizeof(a); i++) {
if (player+1 == i) {
a[i] = 254;
}
else {
a[i] = 32;
}
}
printf("%d", a[15]);
return 0;
}
Your loop runs from 0 to sizeof(a), and sizeof(a) is the size in bytes of your array.
Each int is (typically) 4-bytes, and the total size of the array is 64-bytes. So variable i goes from 0 to 63.
But the valid indices of the array are only 0-15, because the array was declared [16].
The standard way to iterate over an array like this is:
#define count_of_array(x) (sizeof(x) / sizeof(*x))
for (int i = 0; i < count_of_array(a); i++) { ... }
The count_of_array macro calculates the number of elements in the array by taking the total size of the array, and dividing by the size of one element.
In your example, it would be (64 / 4) == 16.
sizeof(a) is not the size of a, but rather how many bytes a consumes.
a has 16 ints. The size of int depends on the implementation. A lot of C implementations make int has 4 bytes, but some implementations make int has 2 bytes. So sizeof(a) == 64 or sizeof(a) == 32. Either way, that's not what you want.
You define int a[16];, so the size of a is 16.
So, change your for loop into:
for (int i = 0; i < 16; i++)
You're indexing too far off the size of the array, trying to touch parts of memory that doesn't belong to your program. sizeof(a) returns 64 (depending on C implementation, actually), which is the total amount of bytes your int array is taking up.
There are good reasons for trying not to statically declare the number of iterations in a loop when iterating over an array.
For example, you might realloc memory (if you've declared the array using malloc) in order to grow or shrink the array, thus making it harder to keep track of the size of the array at any given point. Or maybe the size of the array depends on user input. Or something else altogether.
There's no good reason to avoid saying for (int i = 0; i < 16; i++) in this particular case, though. What I would do is declare const int foo = 16; and then use foo instead of any number, both in the array declaration and the for loop, so that if you ever need to change it, you only need to change it in one place. Else, if you really want to use sizeof() (maybe because one of the reasons above) you should divide the return value of sizeof(array) by the return value of sizeof(type of array). For example:
#include <stdio.h>
const int ARRAY_SIZE = 30;
int main(void)
{
int a[ARRAY_SIZE];
for(int i = 0; i < sizeof(a) / sizeof(int); i++)
a[i] = 100;
// I'd use for(int i = 0; i < ARRAY_SIZE; i++) though
}

Why this code works when i is smaller but gives a segmentation fault when i is larger?

#include <stdio.h>
int main(void) {
int *x, *y;
x = malloc(sizeof(int));
for (int i = 0; i < 4; i++)
x[i] = i + 1;
y = x;
for (int i = 0; i < 4; i++)
printf("%d ", y[i]);
}
This works correct and outputs 1 2 3 4.
But when i < 1000000 it gives segmentation fault.
Can someone explain this?
You need to allocate a large enough buffer. You only allocate sizeof(int) which is 4 bytes typically and large enough to hold only one integer. Can't store 1000000 elements in that. It worked for 4 elements out of pure chance, probably because although you were overwriting memory, you didn't clobber anything important. Something like this is what you should use.
#include <stdio.h>
int main(void)
{
int count = 1000000;
int *x, *y;
x = malloc(sizeof(int) * count);
for (int i=0; i < count; i++)
x[i] = i+1;
y = x;
for (int i=0; i < count; i++)
printf("%d ", y[i]);
}
Undefined behaviour is undefined, you cannot justify any outcome whatsoever.
You have memory allocated for one integer, the moment you try to dereference the memory outside that range (i.e., i == 1), you're invoking UB. The only valid access is x[0] and x[0] only.
You only allocated memory for one int:
x = malloc(sizeof(int)); // malloc allocates a memory chunk to only hold one int object.
Indexing x at x[i] = i+1; or y at printf("%d ", y[i]); in the loops with anything other than a value of 0 for i (like x[0] or y[0]) invokes undefined behavior because you would attempt to write to and read from not allocated memory.
"then this means if I don't have any enough buffer, it also will give a segmentation fault for i < 4?"
Exactly. You know that is the bad thing on undefined behavior. It does not need to provide wrong results or errors. So, the i < 4 code is broken, too.
Since you written to "only" 12 bytes after the allocated memory (since sizeof(int) common is 4), it might have worked because there was no other necessary information in memory thereafter, but your code is absolutely broken nonetheless.
you defined less memory than the memory you used causing your program to write after that memory zone and alterate the stack fo the program, this is also the case of the buffer overflow vulnerability in C and C++, increment the buffer size

c - malloc not working for double type array

I'm trying to allocate memory for a double type array to use it with GNU Scientific Library.
The code in using for this is something like
double *x_i, *y_i, *x_e, *y_e, data[MAX_SIZE][2];
int n_i, n_e, n_data;
...
x_i = (double *)malloc(n_i * sizeof(double));
y_i = (double *)malloc(n_i * sizeof(double));
x_e = (double *)malloc(n_e * sizeof(double));
y_e = (double *)malloc(n_e * sizeof(double));
for (int i = 0; i < n_data; i++){
if (data[i][1] > 0){
x_e[i] = data[i][0];
y_e[i] = data[i][1];
}
else{
x_i[i] = data[i][0];
y_i[i] = data[i][1];
}
}
With n_i + n_e = n_data.
Apparently, sizeof(x/y_e/i) after malloc is 8, but should be arround 50*sizeof(double). The values attributed after the for loop to x_e/i[i] and y_e/i[i] are not consistent, they change as I change the order of attribution, sometimes returning -nan. Values of data[][], n_i, n_e and n_data are consistent with I expect, and if I print x_e/i[i] and y_e/i[i] values inside the for loop they look correct, but outside that loop they change.
Thank you, and sorry if this is a redundant or bad formulated question.
In addition to sizeof(x/y_e/i) actually returning sizeof(double*) (as other users have mentioned), your for-loop is incorrect:
If n_i + n_e == n_data, then you will always hit a case where attempting to access x/y_e/i[i] will take you out of bounds. To avoid this, you could keep track of x/y_e's and x/y_i's indices separately, perhaps like this:
int i_e = 0, i_i = 0;
for (int i = 0; i < n_data; i++){
if (data[i][1] > 0){
x_e[i_e] = data[i][0];
y_e[i_e] = data[i][1];
i_e++;
}
else{
x_i[i_i] = data[i][0];
y_i[i_i] = data[i][1];
i_i++;
}
}
By the end of the loop, you should find that i_e + i_i == n_data and that the values assigned to x_e/i and y_e/i are consistent.
First, sizeof(x_i) with x_i being of type double* is the size of a pointer (probably 8 on your system), and not the size of the memory allocated. There is no way to get the size of the memory block to which a pointer points solely from the pointer.
Second, the "changing values" of the array that are not set in the loop are caused by having not initialized this portion of the array. Actually you yield undefined behaviour, most likely printing "garbage" when accessing these values. To overcome this, use calloc instead of malloc. calloc zero-initializes the memory block allocated, and according to IEEE standards, floating point values with all bits set to 0 represent the floating point value 0.0:
x_i = calloc(n_i, sizeof(double));

int LA[] = {1,2,3,4,5} memory allocation confusion in c

I have observed that memory allocated for array seems to be dynamic.
Here is the sample code I found in this tutorial:
#include <stdio.h>
main() {
int LA[] = {1,3,5,7,8};
int item = 10, k = 3, n = 5;
int i = 0, j = n;
printf("The original array elements are :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
n = n + 1;
while( j >= k){
LA[j+1] = LA[j];
j = j - 1;
}
LA[k] = item;
printf("The array elements after insertion :\n");
for(i = 0; i<n; i++) {
printf("LA[%d] = %d \n", i, LA[i]);
}
}
and sample output:
The original array elements are :
LA[0]=1
LA[1]=3
LA[2]=5
LA[3]=7
LA[4]=8
The array elements after insertion :
LA[0]=1
LA[1]=3
LA[2]=5
LA[3]=10
LA[4]=7
LA[5]=8
How its working I did not get.
First, a general statement, for an array defined without explicit size and initialized using brace-enclosed initializer, the size will depend o the elements in the initializer list. So, for your array
int LA[] = {1,3,5,7,8};
size will be 5, as you have 5 elements.
C uses 0-based array indexing, so the valid access will be 0 to 4.
In your code
LA[j+1] = LA[j];
trying to access index 6, (5+1) which is out of bound access. This invokes undefined behavior.
Output of a code having UB cannot be justified in any way.
That said, main() is technically an invalid signature as per latest C standards. You need to use at least int main(void) to make the code conforming for a hosted environment.
The code has a buffer overflow bug! Arrays in C cannot be extended! You need to allocate enough space when you declare/define it.
You can declare additional space by supplying a size in the declaration:
int LA[10] = {1,3,5,7,8};
LA will now have room for 10 elements with index 0 through 9.
If you want more flexibility you should use a pointer and malloc/calloc/realloc to allocate memory.
Note:
There is a second bug in the copying. The loop starts one step too far out.
With j starting at 5 and assigning index j+1 the code assigns LA[6], which is the 7th element. After the insertion there are only 6 elements.
My conclusion from these 2 bugs is that the tutorial was neither written nor reviewed by an experienced C programmer.
To add on to the other answers, C/C++ do not do any bounds checking for arrays.
In this case you have a stack allocated array, so as long as your index does not leave stack space, there will be no "errors" during runtime. However, since you are leaving the bounds of your array, it is possible that you may end up changing the values of other variables that are also allocated in the stack if it's memory location happens to be immediately after the allocated array. This is one of the dangers of buffer overflows and can cause very bad things to happen in more complex programs.

C: Array not allocating more memory correctly

I'm fairly new to C and I'm working on a project. Given an integer array, I want to move all the zeros in it to the left of the array with the rest of the elements in any order to the right of all the zeros. The basic idea of my algorithm is to count the number of zeros in the array, create a new array with the number of zeros in it from the old array, and then "append" the rest of the non-zero integers onto this array. And then of course I print finished product.
int main(int argc, const char * argv[]) {
int a[10] = {3, 0, 1, 4, 0, 0, 7, 20, 1, 5};
int n = 10, count = 0;
// counts the number of 0's in the original array
for (int i = 0; i < n; ++i)
{
if (a[i] == 0)
{
++count;
}
}
// creates a new array and makes each element 0
int *array = NULL;
for (int j = 0; j < count; ++j)
{
array = realloc(array, (j + 1) * sizeof(int));
array[j] = 0;
}
// adds the nonzero elements of the array to the new array
for (int l = count; l < n; ++l)
{
array = realloc(array, l * sizeof(int)); // getting an error here
if (a[l] != 0)
{
array[l+count] = a[l];
}
}
// prints the array out in a nice format
printf("%s", "{");
for (int k = 0; k < n-1; ++k)
{
printf("%d%s", array[k], ",");
}
printf("%d", array[n-1]);
printf("%s", "}\n");
free(array);
return 0;
}
I'm getting a "Thread 1: EXC_BAD_ACCESS (code=1, address=0x40)" error when I run this code. I think it's got to do something with invalid pointers to the new array, but I'm not sure how to fix it.
array[l+count] = a[l];
This accesses the memory block that array points at beyond its allocated size. You have to do it differently, using a second index:
// adds the nonzero elements of the array to the new array
int l = count;
for (int j=0; j < n; ++j)
{
if (a[j] != 0)
{
array = realloc(array, (l+1) * sizeof(int));
array[l] = a[j];
++l;
}
}
About your algorithm:
I think u don't need to create a new array, just use a int tmp as swap area, and a int foundZeroCount as index, u swap 2 numbers at a time.
About memory allocation:
If u want to allocate memory for a fixed size array, just use malloc() to allocate array once, later when u need to extend the array, just call realloc() once.
About memory reset:
Just use memset(), and don't need a loop.
Suggestion - about c programming
Try improve your c basic, especially about array / pointer / memory, and try to know more functions from glibc.
Books like <The c programming language 2nd>, GNU c library document, and <The linux programming interface> would be useful, I guess.
The problem is with array[l+count] = a[l]; right when you are done allocating your 'zero-array' it's size is 3 and then you try to access (l + count)'th position which is 6.
And even if you have fixed those issues with memory it still wouldn't work because a[l] and further may still be zeros. (Your initial array is doesn't have zeroes in the beggining, remember?)
And there is a couple of suggestions:
use calloc() to build your initial array of zeros because as man states:
The calloc() function allocates memory for an array of nmemb elements
of size bytes each and returns a pointer to the allocated memory. The
memory is set to zero
First allocate then set because operations with memory are quite taxing for performance. It would be better for you to first allocate some memory and work with it instead of reallocating it each step. It would be much easier to keep track of as well.
Other answers address your immediate issue, that
array[l+count] = a[l];
attempts to access outside the bounds of the allocated space to which array points. I'll focus instead on your approach to the problem, which is flawed.
Dynamic memory allocation is comparatively expensive. You do not want to do any more than necessary, and it is particularly poor form to reallocate many times to increase by small increments each time, as you do.
Since you know at compile time how many elements you will need, dynamic allocation is altogether unnecessary here. You could instead do this:
int a[10] = {3, 0, 1, 4, 0, 0, 7, 20, 1, 5};
int array[10] = { 0 };
(Note also here that when an array initializer is provided, any array elements it does not explicitly initialize are initialized to 0.)
Even if you did not know at compile time how many elements you would need, it would be far better to perform the whole allocation in one chunk. Moreover, if you did that via calloc() then you would get automatic initialization of the allocated space to all-zeroes.
The count is known before defining the array. You can allocate memory using malloc as shown below.
array = malloc( count * sizeof(int)).
The error indicates you are trying to access address 0x40. This indicates one of the pointer has become NULL and you are trying to dereference ptr+0x40.
Before you start to hack away, take your time to consider the actual problem, which you have described as:
count the number of zeros in the array, create a new array with the number of zeros in it from the old array, and then "append" the rest of the non-zero integers onto this array.
Your comments say what the code should do, yet the code does something entirely different. Your algorithm to solve the problem is wrong - this, and nothing else, is the cause of the bugs.
To begin with, if the new array should contain all zeroes of the old array plus all non-zeroes, then common sense says that the new array will always have the same size as the old array. You don't even need to use dynamic memory allocation.
The code you have creates a new array and discards the old one, in the same memory location, over and over. This doesn't make any sense. On top of that, it is very ineffective to call realloc repeatedly in a loop.
You should do something like this instead:
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
int main(int argc, const char * argv[]) {
int a[10] = {3, 0, 1, 4, 0, 0, 7, 20, 1, 5};
const int n = 10;
// counts the number of 0's in the original array
int zeroes = 0;
for (int i = 0; i < n; ++i)
{
if (a[i] == 0)
{
++zeroes;
}
}
// creates a new array and makes each element 0
// there's actually no need to allocate this dynamically at all...
int *array = calloc(1, sizeof(int[n]) );
assert(array != NULL);
// skip zeroes, ie "move the zeroes from the original array"
int* not_zeroes = array + zeroes; // point at first item to contain "not zeroes"
// adds the non-zero elements of the original array to the new array
for (int i = 0; i < n; ++i)
{
if(a[i] != 0)
{
*not_zeroes = a[i];
not_zeroes++;
}
}
// prints the array out in a nice format
printf("%s", "{");
for (int i = 0; i < n; ++i)
{
printf("%d,", array[i]);
}
printf("}\n");
free(array);
return 0;
}

Resources