error looping through a integer array in c - c

Alright so i have my array:
unsiged int *arr = (unsigned int*)malloc(200 * sizeof(unsigned int));
Declared like this, and i have wrote a loop that loops through each element of the array and calls a function with the element as its argument, which looks like this:
for (size_t i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {
sprintf(buffer, "/proc/%zu/cmdline", arr[i]);
printf("/proc/%x/cmdline", arr[0]);
printf("/proc/%zu/cmdline", arr[i]);
checkIfRunning(buffer);
}
the checkIfRunning function contains:
void checkIfRunning(char *filepath) {
FILE *fh;
char buf[500];
fh = fopen(filepath, "r");
if (!fh)
exit(1);
My code always exits at exit(1), i get the following (wrong) outputs from both the printf statements:
/proc/bbb07a63/cmdline
/proc/3148905059/cmdline
What am i doing wrong
Please note that i am new to c and i'm sorry if newbie question, also note that the output of printf is different each time, which i assume means its garbage.

For an unsigned int *arr, expression sizeof(arr) is the size of a pointer value (e.g. 4 bytes on a 32 bit system), not the "length" of something this pointer points to. This is different from a declaration like unsigned int arr[100], where sizeof(arr) would give you 100*sizeof(unsigned int). You probably took over the pattern sizeof(arr) / sizeof(arr[0]) from an array declaration like unsigned int[100], yet this cannot be used for dynamically allocated memory. So you'd rather write for (size_t i = 0; i < 200; i++), or write #define ARRSIZE 200 and use ARRSIZE then for the malloc and for the loop.
Second, as mentioned by GrahamS, you do not show any code initialising the memory. If there isn't such initialisation, the behaviour of your program is undefined.

You allocated the memory for the array, but you haven't set the contents of that memory to anything. So your array is just filled with random garbage.

Related

realloc not copying first index

I'm having some trouble with using the malloc/realloc command with arrays. I've created a small array with some integers in it, and tried to add one value to it by expanding the size with realloc and adding the value, but when I do that the 0 index's value is not preserved and is seen as garbage.
#include <stdio.h>
#include <stdlib.h>
int main(){
int n;
printf("Enter size of array\n");
scanf("%d",&n);
int *A = malloc(n*sizeof(int));
for(int i = 0; i < n; i++){
A[i] = i + 1;
}
*A = realloc(A, sizeof(A)+ sizeof(int));
A[n] = 1234;
for(int i = 0; i < n + 1; i++){
printf("%d\n",A[i]);
}
return 0;
}
and when i run the program this happens:
Enter size of array
5
14643216
2
3
4
5
1234
Does anyone know why the 0 index of the array is getting this value and not 1?
$ gcc a.c
a.c: In function ‘main’:
a.c:12:12: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
*A = realloc(A, sizeof(A)+ sizeof(int));
^
Make sure to configure your compiler to emit warnings on code that looks suspicious. Any remotely decent compiler would raise at least a warning, if not an error, on this line. realloc returns a pointer, which you're trying to assign to an int object.
You need to assign the resulting pointer to A, not *A. Furthermore, there's another error, which compilers can't warn you about. sizeof(A)+ sizeof(int) is too small, and does not make much sense in context. Note that sizeof(A) is the size of the pointer A. There's no way to use sizeof to find the number of items in the array that A points to, because sizeof relies on compile-time information. To extend the array by one element, you need to add sizeof(int) to the current allocated size, which is n*sizeof(int), i.e. the new size should be (n+1) * sizeof(int).
In addition, it would be better to use sizeof(*A) than sizeof(int). The two are equivalent, but sizeof(*A) has the advantage that it'll still be correct if you decide to change the array elements, e.g. to make them long.
A = realloc(A, (n+1) * sizeof(*A));
Write A = realloc(A, (n + 1) * sizeof(int)); instead of *A = realloc(A, sizeof(A)+ sizeof(int));
*A = ... will overwrite the value of the first index with a "address value" if A is not moved to another place in memory (undefined behaviour otherwise).
Note that sizeof(A) is a constant value (probably 8, and not the amount of memory allocated previously), such that you had a good chance that realloc did not move the memory.

Segmentation fault in C while declaring large pointer array

I run in a problem with a program and I'm not sure how to solve it. I'm processing a file and to do so I get the size with ftell and store it in M_size. After that I declare a unsigned char pointer array with N. The array is then used in two functions a() and b().
...
unsigned long N = (M_size/ x);
int LstElemSize = M_size % x;
if(LstElemSize != 0){
N += 1;
}
unsigned char *ptr_M[N]
a(ptr_M)
b(ptr_M)
...
Function a() actually initializes each element of ptr_M in a for loop:
a(){
int i;
for(i = 0; i < N-1; i ++){
ptr_M[i] = malloc(sizeof(unsigned char) * x);
}
}
Function b() iterates then over each element and calculates stuff and at the end each element is freed.
My problem is now that when I try to process a file e.g. 1 GB the array size will be around 4 000 000 and a Segmentation error occurs (In the line i declare my array). If I calculated it correctly that is 8 byte (char pointer) times 4 000 000 = 32MB. The server running the program has enough memory to hold the file, but i guess as mentioned in Response 1 the stack space is not enough.
What can I do to solve my problem? Increase my stack space? Thanks!
The problem is that you're defining ptr_M in the stack, which has a small size limit. The heap does not have such a small size limit and is able to use more of your system's memory. You need to use malloc() to allocate ptr_M just like you allocate the subarrays. (Make sure to free it at some point too along with all those subarrays!)
unsigned char **ptr_M = malloc(sizeof(unsigned char*) * N);
Also, your a() has an off-by-one error. It ignores the last item of the array. Use this:
for(i = 0; i < N; i ++){
unsigned char *ptr_M[N] is a variable-length array declaring N number of unsigned char on the stack in your case. You should dynamically allocate the space for the array as well.
unsigned char **ptr_M = malloc(sizeof(unsigned char*) * N);
a(ptr_M);
b(ptr_M);
...
//After you free each element in ptr_M
free(ptr_M);
malloc allocates space from heap, not from stack. You may try increasing your heapsize looking at the compiler option. Check the upper limit of heapsize that is supported there.

Am I correctly allocating memory for my pointer arrays in C?

I am trying to track down a bug a big program. I think it is due to how I am passing arrays to my functions. Am I doing this correctly?
main(){
int *x = declarArray(x, 100);
int *y = declarArray(x, 100);
// lines of code....
x = arrayManip(x, 100);
// more code...
int i;
for(i=0; i<100; i++)
y[i] = x[i];
//more code...
free(x);
free(y);
}
This is how I manipulate arrays:
int *arrayManip(int *myarray, int length){
int i;
for(i=0; i<length; i++)
myarray[i] = i;
return array;
}
This is how I initialize the arrays:
int* declareArray(int *myarray, int length){
myarray = (int*) malloc(length*sizeof(int*));
if (myarray==NULL)
printf("Error allocating memory!\n");
int i;
for(i=0; i<length; i++)
myarray[i] = -888;
return myarray;
}
This code seems to work fine on a small scale, but maybe there is a problem once I have many more arrays of larger size that are often getting passed back and forth and copied in my program?
declarArray :
Name is not gramatically correct
The name of the function is not what it does
malloc with sizeof(int*), not sizeof(int). Guarantuee to be a bug in 64 bit machine
malloc fails, you print, but still write to null
passing myarray as argument is a noop as is
-888 is a magic number
There is no error check whatsoever
My advice. Throw it away and start fresh
No, as per my understanding.
You allocating one dim array => elements in that array should be integers and not pointers to integers so instead of this :
myarray = (int*) malloc(length*sizeof(int*));
it should be :
myarray = (int*) malloc(length*sizeof(int));
In function arrayManip you pass param named array, and than you trying to access it as myarray
This:
myarray = (int*) malloc(length*sizeof(int*));
allocates an array of length pointers to an integer, but then puts it into a pointer to an integer (i.e. an array of integers, not pointers to integers). If you want an array of integers, you want:
myarray = (int*) malloc(length*sizeof(int));
or (if you want to zero it):
myarray = (int*) calloc(length, sizeof(int));
which does the size x length calculation itself.
To allocate a list of pointers to integers, you want:
myarray = (int**) malloc(length*sizeof(int*));
or
myarray = (int**) calloc(length, sizeof(int*));
Unless you are fantastically concerned about speed, I find using calloc() results in fewer bugs from uninitialized arrays, and makes the reason for the allocated size more obvious.
The pointer is of word size [2 or 4 ,... depending on machine architecture]. whatever it may point to int,double,float,...
for integer pointer it works if it takes 4 bytes for int in machine. when u go for other data type it 'll lead you to error.
you should allot memory as
pointer = (DataType*) malloc (length * sizeof(DataType));
use malloc and to make your code clear.
void* malloc (size_t size);
malloc reference
use memset to allot default value [-888] for your array.
void *memset(void *str, int c, size_t n)

Allocating a 3D array in C using malloc()

I'm trying to allocate a 3D array in C. As you can tell, it's to store image data, in the form Array[height][width][channel]; This is the code I have.
BYTE*** allocateImageArray(INT_32 width, INT_32 height, INT_32 channels) {
BYTE*** array;
INT_32 i, j;
array = malloc(height * sizeof(BYTE**));
for(i = 0; i < height; i++) {
array[i] = malloc(width * sizeof(BYTE*));
for(j = 0; j < channels; j++) {
array[i][j] = malloc(channels * sizeof(BYTE));
}
}
printf("Pixel Array Size: %d\n", sizeof(array));
return array;
}
It compiles fine. However, the array is always 8 at the end (Indicated by the sizeof). And when I try to access the array later in the code the program just crashes.
All answers are thanked in advanced.
EDIT: Forgot to include. I've worked out where the program crash occurs. It happens when I try to assign a value to one of the BYTES at the end of the array.
The sizeof operator doesn't work like that. Think if it more as a special #define macro that figures out the size of something based on its type.
So sizeof(array) is the same as sizeof(BYTE***) which is the same as sizeof(void*) which is apparently 8 for your computer.
BYTE** is just a pointer, so in the end sizeof(BYTE **) always returns 8.
You may try the following code:
#define value_at_index(a,i,j,k,m,n) (a)[(i)*m*n + (j)*n + (k)]
static BYTE*** allocateImageArray(int width, int height, int channels) {
BYTE (*array)[width][height];
array = malloc(width * height * channels * sizeof(BYTE));
array[1][2][3] = 1;
printf("array[1][2][3] = %d", array[1][2][3]);
return (BYTE***)array;
}
Doing so, you have a contiguous memory space for the 3-dimension array, then in the main() you can call like this:
BYTE ***array = allocateImageArray(10, 20, 30);
printf("array[1][2][3] = %d", value_at_index(array, 1, 2, 3, 10, 20));
The sizeof operator does not tell you the size of dynamically allocated arrays.
sizeof returns the size in bytes of the underlying datatype, which in your case is a pointer to a pointer to a pointer to a BYTE.
As array is still a pointer (regardless of what it points to) the returned size of 8 bytes makes sense.
Please refer to the sizeof operator explanation.
Aside:
I would also recommend having a look at Why does sizeof(x++) not increment x? for an interesting error that can prop up with the sizeof operator and assumptions that new programmers make about it.
sizeof(array) == the number of bytes for a pointer, which is 8 bytes in your case.

Is this code doing what I want it to do?

I want to create an integer pointer p, allocate memory for a 10-element array, and then fill each element with the value of 5. Here's my code:
//Allocate memory for a 10-element integer array.
int array[10];
int *p = (int *)malloc( sizeof(array) );
//Fill each element with the value of 5.
int i = 0;
printf("Size of array: %d\n", sizeof(array));
while (i < sizeof(array)){
*p = 5;
printf("Current value of array: %p\n", *p);
*p += sizeof(int);
i += sizeof(int);
}
I've added some print statements around this code, but I'm not sure if it's actually filling each element with the value of 5.
So, is my code working correctly? Thanks for your time.
First:
*p += sizeof(int);
This takes the contents of what p points to and adds the size of an integer to it. That doesn't make much sense. What you probably want is just:
p++;
This makes p point to the next object.
But the problem is that p contains your only copy of the pointer to the first object. So if you change its value, you won't be able to access the memory anymore because you won't have a pointer to it. (So you should save a copy of the original value returned from malloc somewhere. If nothing else, you'll eventually need it to pass to free.)
while (i < sizeof(array)){
This doesn't make sense. You don't want to loop a number of times equal to the number of bytes the array occupies.
Lastly, you don't need the array for anything. Just remove it and use:
int *p = malloc(10 * sizeof(int));
For C, don't cast the return value of malloc. It's not needed and can mask other problems such as failing to include the correct headers. For the while loop, just keep track of the number of elements in a separate variable.
Here's a more idiomatic way of doing things:
/* Just allocate the array into your pointer */
int arraySize = 10;
int *p = malloc(sizeof(int) * arraySize);
printf("Size of array: %d\n", arraySize);
/* Use a for loop to iterate over the array */
int i;
for (i = 0; i < arraySize; ++i)
{
p[i] = 5;
printf("Value of index %d in the array: %d\n", i, p[i]);
}
Note that you need to keep track of your array size separately, either in a variable (as I have done) or a macro (#define statement) or just with the integer literal. Using the integer literal is error-prone, however, because if you need to change the array size later, you need to change more lines of code.
sizeof of an array returns the number of bytes the array occupies, in bytes.
int *p = (int *)malloc( sizeof(array) );
If you call malloc, you must #include <stdlib.h>. Also, the cast is unnecessary and can introduce dangerous bugs, especially when paired with the missing malloc definition.
If you increment a pointer by one, you reach the next element of the pointer's type. Therefore, you should write the bottom part as:
for (int i = 0;i < sizeof(array) / sizeof(array[0]);i++){
*p = 5;
p++;
}
*p += sizeof(int);
should be
p += 1;
since the pointer is of type int *
also the array size should be calculated like this:
sizeof (array) / sizeof (array[0]);
and indeed, the array is not needed for your code.
Nope it isn't. The following code will however. You should read up on pointer arithmetic. p + 1 is the next integer (this is one of the reasons why pointers have types). Also remember if you change the value of p it will no longer point to the beginning of your memory.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define LEN 10
int main(void)
{
/* Allocate memory for a 10-element integer array. */
int array[LEN];
int i;
int *p;
int *tmp;
p = malloc(sizeof(array));
assert(p != NULL);
/* Fill each element with the value of 5. */
printf("Size of array: %d bytes\n", (int)sizeof(array));
for(i = 0, tmp = p; i < LEN; tmp++, i++) *tmp = 5;
for(i = 0, tmp = p; i < LEN; i++) printf("%d\n", tmp[i]);
free(p);
return EXIT_SUCCESS;
}
//Allocate memory for a 10-element integer array.
int array[10];
int *p = (int *)malloc( sizeof(array) );
At this point you have allocated twice as much memory -- space for ten integers in the array allocated on the stack, and space for ten integers allocated on the heap. In a "real" program that needed to allocate space for ten integers and stack allocation wasn't the right thing to do, the allocation would be done like this:
int *p = malloc(10 * sizeof(int));
Note that there is no need to cast the return value from malloc(3). I expect you forgot to include the <stdlib> header, which would have properly prototyped the function, and given you the correct output. (Without the prototype in the header, the C compiler assumes the function would return an int, and the cast makes it treat it as a pointer instead. The cast hasn't been necessary for twenty years.)
Furthermore, be vary wary of learning the habit sizeof(array). This will work in code where the array is allocated in the same block as the sizeof() keyword, but it will fail when used like this:
int foo(char bar[]) {
int length = sizeof(bar); /* BUG */
}
It'll look correct, but sizeof() will in fact see an char * instead of the full array. C's new Variable Length Array support is keen, but not to be mistaken with the arrays that know their size available in many other langauges.
//Fill each element with the value of 5.
int i = 0;
printf("Size of array: %d\n", sizeof(array));
while (i < sizeof(array)){
*p = 5;
*p += sizeof(int);
Aha! Someone else who has the same trouble with C pointers that I did! I presume you used to write mostly assembly code and had to increment your pointers yourself? :) The compiler knows the type of objects that p points to (int *p), so it'll properly move the pointer by the correct number of bytes if you just write p++. If you swap your code to using long or long long or float or double or long double or struct very_long_integers, the compiler will always do the right thing with p++.
i += sizeof(int);
}
While that's not wrong, it would certainly be more idiomatic to re-write the last loop a little:
for (i=0; i<array_length; i++)
p[i] = 5;
Of course, you'll have to store the array length into a variable or #define it, but it's easier to do this than rely on a sometimes-finicky calculation of the array length.
Update
After reading the other (excellent) answers, I realize I forgot to mention that since p is your only reference to the array, it'd be best to not update p without storing a copy of its value somewhere. My little 'idiomatic' rewrite side-steps the issue but doesn't point out why using subscription is more idiomatic than incrementing the pointer -- and this is one reason why the subscription is preferred. I also prefer the subscription because it is often far easier to reason about code where the base of an array doesn't change. (It Depends.)
//allocate an array of 10 elements on the stack
int array[10];
//allocate an array of 10 elements on the heap. p points at them
int *p = (int *)malloc( sizeof(array) );
// i equals 0
int i = 0;
//while i is less than 40
while (i < sizeof(array)){
//the first element of the dynamic array is five
*p = 5;
// the first element of the dynamic array is nine!
*p += sizeof(int);
// incrememnt i by 4
i += sizeof(int);
}
This sets the first element of the array to nine, 10 times. It looks like you want something more like:
//when you get something from malloc,
// make sure it's type is "____ * const" so
// you don't accidentally lose it
int * const p = (int *)malloc( 10*sizeof(int) );
for (int i=0; i<10; ++i)
p[i] = 5;
A ___ * const prevents you from changing p, so that it will always point to the data that was allocated. This means free(p); will always work. If you change p, you can't release the memory, and you get a memory leak.

Resources