Filling out vectors passed as pointers (C) - c

Suppose I have this logical array with, which I want to split into the 1 part and the 0 part, creating two separate vectors.
So I came up with the following method:
void cut_and_uncut(long* input, int length, long* cut_part, int* cut_length, long* uncut_part, int* uncut_length){
int i;
int n_cut=0;
for(i=0;i<length;i++) n_cut+=input[i];
cut_part = vecallocl(n_cut);
uncut_part = vecallocl(length-n_cut);
*cut_length = n_cut;
*uncut_length = length-n_cut;
int index_cut = 0;
int index_uncut = 0;
for(i=0;i<length;i++){
if(input[i]==1){
cut_part[index_cut] = i;
index_cut++;
} else {
uncut_part[index_uncut] = i;
index_uncut++;
}
}
}
input is the input vector of length length (so imaginative!)
cut_part is the vector with the indices of the 1s, of length cut_length
uncut_part is the vector with the indices of the 0s, of length uncut_length
(Note: vecallocl(k) is just a shortcut for malloc(k*sizeof(long)) )
I call this function with
int len,len2;
long* cut_vec;
long* uncut_vec;
cut_and_uncut(split,matrix.m+matrix.n,cut_vec,&len,uncut_vec,&len2);
The two ints (len and len2) are correctly filled, but when I try to look into the cut_vec and uncut_vec vectors, segfault happens.
I have the hunch that I am doing something wrong with the memory, because I initialize the two pointers without them actually pointing to anything.. but then in the function, with the actual vecallocl, they should be initialized correctly.
When I print the vectors from inside this cut_and_uncut function, everything works.. when doing it outside (i.e. at the same level this function is called) it does not.
What's wrong?

Try this:
void cut_and_uncut(long* input, int length, long** cut_part,
int* cut_length, long** uncut_part, int* uncut_length)
....
// inside the function you now use `*cut_part` instead of `cut_part`, etc.
if(input[i]==1){
*cut_part[index_cut] = i;
index_cut++;
} else {
*uncut_part[index_uncut] = i;
index_uncut++;
}
and pass not the (still unallocated) pointers but the address where they reside:
long *cut_part;
long *uncut_part;
cut_and_uncut(... &cut_part, &uncut_part, ...)
This way, modifications will be retained after cut_and_uncut() returns.

Related

(C) Can't read string array values between functions

I have one char strings pointers array, lets call it "str_array".
I also have 3 strings:
1."Hello"
2."World"
3."Today"
(Every string ends with \0)
And I have this function, that receives our str_array, the size of it, and another pointers arr that is not releveant for my question.
The problem that I encounter, is that function "scanString" receives NULL, or garbage values, instead of the strings inside str_array.
unsigned int RemoveFromStrArray(char*** str_array, unsigned int str_array_size, char** ptr_to_chars_array)
{
int k = 0;
while (k != str_array_size)
{
// This is the part where im trying to scan str_array strings.
scanString(*str_array[k]);
str_array++;
k++;
}
}
int scanString(char* string)
{
int c = 0;
int counter = 0;
while (string[c] != '\0')
{
if (string[c] == 1)
{
moveOneBack(string, c);
c--;
counter++;
}
c++;
}
return c;
}
I've been trying multiple alternative ways to scan str_array string arrays.
But all of my times I just had to deal with garbage values or NULL strings.
How do I reach str_array strings, that would be passed by reference to scanString?
Picture of what I'm talking about:
BIG THANKS IN ADVANCE!
unsigned int RemoveFromStrArray(char*** str_array
You've got at least one too many indirections there. From your drawing, str_array refers to an array, so it's effectively a pointer, and the values in the array are pointers to char. You don't seem to be changing the array itself, e.g. you're not making str_array refer to some other array of strings, so there's no need to pass the address of the array. So char ** is closer to the type you want.
str_array++;
I'm not sure why you're incrementing str_array and using k as an index into the array. Although it's safe to modify parameters locally like that, it's nice to avoid it so that you can refer to them while debugging. Consider this:
unsigned int RemoveFromStrArray(char** str_array,
unsigned int str_array_size)
{
int k = 0;
while (k != str_array_size)
{
char *string = str_array[k];
scanString(string);
k++;
}
}
That is, the values in str_array are of type char *, so str_array[k] has that type. Copying it into a temporary variable string makes it a little easier to see what's going on. (I removed the last parameter, ptr_to_chars_array, to keep things simple and because it's not used. You'll add it back if you have plans for it, I'm sure.)
A for loop would be a little more compact but otherwise equivalent:
unsigned int RemoveFromStrArray(char** str_array,
unsigned int str_array_size)
{
for (int k = 0; k < str_array_size; k++)
{
char *string = str_array[k];
scanString(string);
}
}

Why wouldn't this change the values in the array?

I know in C, things are passed by value however I thought that arrays, if modified in functions (without making a copy) would have the original modified but when I run this code that does not happen. I am assuming range doesn't change len because of the scope?
Can someone explain?
static void task(int *b, int range){
b[range-1] = 200;
range = 0;
b = NULL;
}
int main (){
int a[]= {2,4,6};
int len = 3, i;
printf("len1: %d\n", len);
task(a,len);
printf("len %d\n", len);
for(i=0; i < len; i++){
printf("%d\n", a[i]);
}
return 0;
}
When using pointers, you can modify to what the pointer is pointing to, but not the pointer itself.
So, your function will modify a, effectively putting 200 in the last position, however, it will not become NULL.
Len wont be modified as well.
To modify both of them, you should do
void task(int **b, int *range) {
*(b)[*range - 1] = 200;
*range = 0;
*b = NULL;
}
And call it as
task(&b, &len);
But why would you want to modify the array if you are gonna set it to NULL later?
What happens when you declare the array int a[]= {2,4,6}; is that the values 2,4,6 will be stored in memory in three consecutive memory fields. In variable a will be stored the address to the first memory field (where the first array element was saved).
When calling task(a,len); you pass a copy of this address (call by value).
Because of this you can enter the array in the called function but can not override the actual variable a.
What happens there is that b[range-1] = 200; overrides the third element with 200. range = 0; and b = NULL; do only override the local variables, that only exist in the function. The least two instructions don't affect the variables in your main().
When returning to main() a and len have still the same values as before calling task(a,len);. But the values stored in the array are now a={2,4,200};.

Cannot return int array

I want to use only studio.h library to convert from decimal number to binary number by using an array to store remainder but the result is not correct, maybe i have problem with memory allocation or return value is wrong, please help me to check it.
Thank you so much!
#include <stdio.h>
int n = 0;
int* DecimalToBinary(int number){
int a[10];
while(number!=0){
a[n++] = number%2;
number/=2;
}
return a;
}
void main(){
int *d1 = DecimalToBinary(5);
int *d2 = DecimalToBinary(10);
for(int i = n-1 ;i>=0;i--)
printf(" %d",d1[i]);
printf("\n");
for(int i = n-1 ;i>=0;i--)
printf(" %d",d2[i]);
}
You return a pointer to a local array. That local array is on the stack, and when the function returns the array goes out of scope and that stack memory will be reused when you call the next function. This means that the pointer will now point to some other data, and not the original array.
There are two solutions to this:
Declare the array in the function calling DecimalToBinary and pass it as an argument.
Create the array dynamically on the heap (e.g. with malloc) and return that pointer.
The problem with method 2 is that it might create a memory leak if you don't free the returned pointer.
As noted by Craig there is a third solution, to make the array static inside the function. However in this case it brings other and bigger problems than the two solutions I originally listed, and that's why I didn't list it.
There is also another serious problem with the code, as noted by Uchia Itachi, and that is that the array is indexed by a global variable. If the DecimalToBinary function is called with a too big number, or to many times, this global index variable will be to big for the array and will be out of bounds for the array.
Both the problem with dereferencing a pointer to an out-of-scope array and the indexing out of bounds leads to undefined behavior. Undefined behavior will, if you're lucky, just lead to the wrong result being printed. If you're unlucky it will cause the program to crash.
You are returning a pointer to a locally allocated array. It is allocated on the stack, and goes away when the function returns, leaving your pointer pointing to garbage.
You have a few options. You could pass an array in to fill:
void DecimalToBinary(int result[10],int number){
while(number!=0){
result[n++] = number%2;
number/=2;
}
return result;
}
// usage example:
int b[10];
DecimalToBinary(b, 42);
Or you could allocate an array on the heap:
int* DecimalToBinary(int number){
int *a = (int *)malloc(sizeof(int) * 10);
while(number!=0){
a[n++] = number%2;
number/=2;
}
return a;
}
// usage example
int *b = DecimalToBinary(42);
free(b); // when finished with it
Or you could wrap the array in a struct:
typedef struct {
int b[10];
} result;
result DecimalToBinary(int number){
result r;
while(number!=0){
r.b[n++] = number%2;
number/=2;
}
return r;
}
// usage example
result r = DecimalToBinary(42);
If you do the malloc() option, do not forget to free() the returned data when you're done with it, otherwise it will hang around. This is called a memory leak. In more complex programs, it can lead to serious issues.
Note: By the way, if your number is larger than 1023 (10 binary digits), you'll overrun the array. You may also wish to explicitly stop once you've stored 10 digits, or pass the size of the array in, or compute the required size first and allocate that much space. Also, you will get some odd results if your number is negative, you might want to use number&1 instead of number%2.
Note 2: As noted elsewhere, you should make n local, or at the very least reinitalize it to 0 each time the function is called, otherwise it will just accumulate and eventually you'll go past the end of the array.
int[10] is not the same as int *; not only is the former created on the stack, it is a different type alltogether. You need to create an actual int * like so:
int *a = malloc (10 * sizeof (int));
Of course, don't forget to free() it after use!
What you can also do and what is commonly done in C is creating the array where it is called and provide a pointer to that array to the function, this way when the array is on the stack of the function that calls it and not in the function self. We also have to specify the size of the array on to that function, since the function cannot know to how many elements the pointer points to
void DecimalToBinary( int number, int* output, unsigned size ) {
/*adapt this to your liking*/
int i;
for ( i = 0; i < size && number != 0; i++) {
output[i] = number%2;
number/2;
}
}
and in you main function you would call it like this:
int array[10];
DecimalToBinary( 5, array, sizeof(array)/sizeof(array[0]));
now array has the same result as a would have had in your example.
The problem in your code lies here..
int * DecimalToBinary(int number){
int a[10];
while(number!=0){
a[n++] = number%2;
number/=2;
}
return a;
}
The array a scope is only till this function. Once this function terminates, the memory allocated for this array will be released, either u need to use dynamic memory allocation or make array a global.
This is the correct program:
#include <stdio.h>
int n = 0;
int a[10] = {0};
int* DecimalToBinary(int number){
n = 0;
while(number!=0){
a[n++] = number%2;
number = number/2;
}
return a;
}
int main(){
int *d1;
int *d2;
int i;
d1 = DecimalToBinary(5);
for(i = n-1;i>=0;i--)
printf(" %d",d1[i]);
printf("\n");
d2 = DecimalToBinary(10);
for(i = n-1;i>=0;i--)
printf(" %d",d2[i]);
printf("\n");
}

mergesort of names passing arguments in C

How do I pass an argument of string arrays e.g *p[50]?
void sortnames(char array[],int low,int high){
int mid;
if(low<high){
mid=(low+high)/2;
sortnames(array,low,mid);
sortnames(array,mid+1,high);
mergeSort(array,low,mid,high);
}
}
the code
Method to define
char *arr_ofptr[];
Example to fill its elements.Here filling first element
arr_ofptr[0] = "John Smith";
method to pass this array as argument
func(arr_ofptr,..
Method to pass perticular element of this array
func(arr_ofptr[nth], ..
You can do:
void sortnames(char **names, int low, int high, int num_names, int *sizes)
Here you pass the array of names on the first parameter. The size of the first coordinate has to be num_names, so you don't get into segmentation fault problems. Then you can pass on the last parameter an array with the length of each string. This last parameter must also be of size num_names, and then sizes[i] would inficate the length of string names[i].
EDIT: A segmentation fault is an error that you'll get whenever you're accessing memory that you are not allowed to be touching in C. That usually arrives when you are accessing array elements out of bounds. In order to avoid this, you have to make sure to allocate enough space for your arrays using an appropriate call to malloc. So, for example, in order to call your sortnames function, you should declare before an array of strings more or less like this (I say more or less because I don't know the context in which you're executing):
int num_names // This is the number of names you want to read
// Populate the variable num_names
// ...
char **to_sort = malloc(num_names * sizeof(char *));
int i;
for(i = 0; i < num_names; ++ i)
{
to_sort[i] = malloc(max_name_size); // Here max_name_size is a static value
// with the size of the longest string
// you are willing to accept. This is to
// avoid you some troublesome reallocation
}
// Populate the array with your strings using expressions like
// to_sort[i] = string_value;
//...
int *sizes = malloc(num_names * sizeof(int));
for(i = 0; i < num_names; ++ i)
{
sizes[i] = strlen(to_sort[i]);
}
sortnames(to_sort, 0, num_names, sizes);
And remember to null-terminate your strings to avoid segmentation faults on the call to strlen.
you can do this:
void sortnames(char *array,int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
sortnames(array,low,mid);
sortnames(array,mid+1,high);
mergeSort(array,low,mid,high);
}
}
with char *array you pass the address of the fist element of the array..
i hope this help..

Dynamic Memory storage issue after realloc - C

For an assignment at school, we have to use structs to make matrices that can store a infinite amount of points for an infinite amount of matrices. (theoretical infinite)
For the assignment I decided to use calloc and realloc. How the sizes for the matrix go is: It doubles in size every time its limit is hit for its points (so it starts at 1, then goes to 2, then 4 and so on). It also doubles in size every time a matrix is added as well.
This is where my issue lies. After the initial matrix is added, and it goes to add the second matrix name and points, it gives me the following:
B???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
B is the portion of it that I want (as I use strcmp later on), but the ? marks are not supposed to be there. (obviously)
I am not sure why it is exactly doing this. Since the code is modular it isn't very easy to get portions of it to show exactly how it is going about this.
Note: I can access the points of the matrix via its method of: MyMatrix[1].points[0].x_cord; (this is just an example)
Sample code that produces problem:
STRUCTS:
struct matrice {
char M_name[256];
int num_points[128];
int set_points[128];
int hasValues[1];
struct matrice_points * points;
} * MyMatrix;
struct matrice_points {
int set[1];
double cord_x;
double cord_y;
};
Setup Matrix Function:
void setupMatrix(){
MyMatrix = calloc(1, sizeof(*MyMatrix));
numMatrix = 1;
}
Grow Matrix Function:
void growMatrix(){
MyMatrix = realloc(MyMatrix, numMatrix * 2 * sizeof(*MyMatrix));
numMatrix = numMatrix * 2;
}
Add Matrix Function which outputs this problem after growing the matrix once.
void addMatrix(char Name, int Location){
int exists = 0;
int existsLocation = 0;
for (int i = 0; i < numMatrix; i++){
if (strcmp(MyMatrix[i].M_name, &Name) == 0){
exists = 1;
existsLocation = i;
}
}
*MyMatrix[Location].M_name = Name;
printf("Stored Name: %s\n", MyMatrix[Location].M_name);
*MyMatrix[Location].num_points = 1;
*MyMatrix[Location].set_points = 0;
*MyMatrix[Location].hasValues = 1;
MyMatrix[Location].points = calloc(1, sizeof(*MyMatrix[Location].points));
}
void addMatrix(char Name, int Location)
char Name represents a single char, i.e. a integer-type quantity. char is just a number, it's not a string at all.
When you do this:
strcmp(..., &Name)
you're assuming that the location where that one character is stored represents a valid C string. This is wrong, there is no reason why this should be the case. If you want to pass a C string to this function, you will need to declare it like this:
void addMatrix(char *Name, int Location)
Then you need to copy that C string into the appropriate place in your matrix structure. It should look like:
strncpy(... .M_name, Name, max_number_of_chars_you_can_store_in_M_Name);
Also these field definitions are strange in your struct:
int num_points[128];
int set_points[128];
int hasValues[1];
This means that your struct will contain an array of 128 ints called num_points, another array of 128 ints calls set_points, and an array of one int (strange) called hasValues. If you only need to store the count of total points and set points, and a flag indicating whether values are stored, the definition should be:
int num_points;
int set_points;
int hasValues;
and correct the assignments in your addMatrix function.
If you do need those arrays, then your assignments as they are are wrong also.
Please turn on all warnings in your compiler.
Try adding '\0' to the end of your data.
*MyMatrix[Location].M_name = Name;
You're copying a single character here, not a string. If you want a string, Name should be defined as char *, and you should be using strcpy.

Resources