Passing multi-dimensional arrays in C - c

I am currently trying to learn C and I have come to a problem that I've been unable to solve.
Consider:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ELEMENTS 5
void make(char **array, int *array_size) {
int i;
char *t = "Hello, World!";
array = malloc(ELEMENTS * sizeof(char *));
for (i = 0; i < ELEMENTS; ++i) {
array[i] = malloc(strlen(t) + 1 * sizeof(char));
array[i] = strdup(t);
}
}
int main(int argc, char **argv) {
char **array;
int size;
int i;
make(array, &size);
for (i = 0; i < size; ++i) {
printf("%s\n", array[i]);
}
return 0;
}
I have no idea why the above fails to read back the contents of the array after creating it. I have literally spent an hour trying to understand why it fails but have come up empty handed. No doubt it's something trivial.
Cheers,

You need to pass the address of "array" into the function. That is, you need char ***. This is because you need to change the value of array, by allocating memory to it.
EDIT: Just to make it more complete, in the function declaration you need to have something like
void make(char ***array, int *array_size)
Then you need to call it using
make(&array, &size);
Inside the function make, allocate memory with
*array = malloc(ELEMENTS * sizeof(char *));
And change other places accordingly.
Also, as kauppi has pointed out, strdup will allocate memory for you, so you don't need to do malloc on each string.

Here is the working code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ELEMENTS 5
void make(char ***array) {
char *t = "Hello, World!";
*array = malloc(ELEMENTS * sizeof(char *));
int i;
for (i = 0; i < ELEMENTS; ++i) {
(*array)[i] = strdup(t);
}
}
int main(int argc, char **argv) {
char **array;
make(&array);
int i;
for (i = 0; i < ELEMENTS; ++i) {
printf("%s\n", array[i]);
free(array[i]);
}
free(array);
return 0;
}
As the other have posted - there was unused size, and strdup allocates memory by itself, and it is nice to free the memory afterwards...

See PolyThinker's comment which is absolutely spot on.
In addition to the way you pass the array, you should check a few other issues:
Perhaps you should assign something to array_size in make(...)?
strdup(char*) allocates memory, the malloc for array[i] is not necessary.
You should free all the memory you allocate after you don't need it anymore.

You are passing the current value of array to make as a copy (on the stack). when you change array in make(), you're only changing the copy, not the actual variable. Try passing by reference with &, or make it a char *** and work with *array = ...

size is declared but gets no value assigned (that should happen in function make, I suppose).

Related

Use realloc() after malloc() to change the size of unsigned char array

In the main function, I use malloc() to create an unsigned char array:
int main()
{
int length = 64;
unsigned char *array = (unsigned char *)malloc(length * sizeof(unsigned char));
...
change_size(array, length);
}
change_size() defined in .h:
void change_size(unsigned char* arr, int len);
In the change_size function, I will use realloc() to increase the array size:
void change size(unsigned char* arr, int len)
{
printf("%d\n", len);
len = len + 16;
printf("%d\n", len);
arr = (unsigned char *)realloc(arr, len * sizeof(unsigned char));
int new_len = sizeof(arr)/sizeof(arr[0]);
printf("%d\n", new_len);
}
The printf() show me:
64
80
8
The array size in the main() also needs to be updated.
Then how to change this array size correctly?
You need to pass your parameters as pointers if you want to change their value back in the caller. That also means you pass your array pointer as a pointer, because realloc might change it:
int change_size(unsigned char **arr, int *len)
{
int new_len = *len + 16;
unsigned char *new_arr = realloc(*arr, new_len);
if (new_arr) {
*len = new_len;
*arr = new_arr;
}
return new_arr != NULL;
}
Here I've modified change_size to suit, and also added a return value to indicate success, since realloc can fail to resize the memory. For clarity, I removed the printf calls. Oh, and I also removed the cast, since that is not valid in C.
Example usage:
if (!change_size(&array, &len))
{
perror("change_size failed");
}
One final note is that you can use your change_size function for the first allocation too, rather than calling malloc. If the first argument to realloc is NULL, it does the same thing as malloc.
First C is not babysitter language,
You only need basic things then you can do everything,
Just try hard to totally understand basic.
#include <stdio.h>
#include <stdlib.h>
int main(){
int G1_Len=20;
int G2_Len=40;
char* G1=(char*)malloc(sizeof(char)*G1_Len);
char* G2=(char*)malloc(sizeof(char)*G2_Len);
printf("This is G1's Size:%d,Becuz G1 is Pointer\n",sizeof(G1));
printf("%d\n",sizeof(G2));
printf("This is what you need just add a variable remainber your size\n%d\n",G1_Len);
printf("%d\n",G2_Len);
/*alloc and free is a pair of memory control you need,remember least function thinking more is tip of C*/
/*if you need alot of function but you cant control all try c++*/
/*and if in c++ using new and delete dont use malloc free*/
free(G1);
free(G2);
G1=NULL;
G2=NULL;
G1_Len=22;
G1=(char*)malloc(sizeof(char)*G1_Len);
//Now you have 22 bytes of char array
free(G1);
return 0;
}
Okay I answer it. #Chipster
#include <stdio.h>
#include <stdlib.h>
int change_size(char** arr, int len)
{
char* nar=(char*)malloc(sizeof(char)*(len+16));
if(nar){
free(* arr);
*arr=nar;
nar[10]='K';//this will let you know its right
return len+16;
}
return len;
}
int main(){
int G1_Len=20;
int G2_Len=40;
char* G1=(char*)malloc(sizeof(char)*G1_Len);
char* G2=(char*)malloc(sizeof(char)*G2_Len);
printf("This is G1's Size:%d,Becuz G1 is Pointer\n",sizeof(G1));
printf("%d\n",sizeof(G2));
printf("This is what you need just add a variable remainber your size\n%d\n",G1_Len);
printf("%d\n",G2_Len);
/*alloc and free is a pair of memory control you need,remember least function thinking more is tip of C*/
/*if you need alot of function but you cant control all try c++*/
/*and if in c++ using new and delete dont use malloc free*/
free(G1);
free(G2);
G1=NULL;
G2=NULL;
G1_Len=22;
G1=(char*)malloc(sizeof(char)*G1_Len);
//Now you have 22 bytes of char array
printf("%d\n",G1);
G1_Len=change_size(&G1,G1_Len);
printf("%c\n",G1[10]);
printf("%d\n",G1);
printf("%d\n",G1_Len);
free(G1);
return 0;
}

Trying to free an array of character array in C result in double free or corruption

I am working on a project in which I have to create an array of string (char *), however, when I try to free the array of array, "double free or corruption" is given and I couldn't figure out the problem.
The code extraction here is a simplified version, it may seem meaningless but it illustrates the problem I am facing:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char **cptr;
int i;
cptr = malloc(256*sizeof(char));
for (i=0; i<256; i++) {
cptr[i] = calloc(2, sizeof(char));
cptr[i][0] = (char)i;
}
for (i=0; i<256; i++) {
free(cptr[i]);
}
free(cptr);
return 0;
}
Thank you.
cptr = malloc(256 * sizeof(char));
Above line is wrong. Even if you compute number of bytes allocated above, it is 256 bytes. If size of pointer is 4 bytes on your machine, it would be enough for 64 pointers. Instead you need 256 pointers. Use
cptr = malloc(256 * sizeof(char *));

memory leakage on asprintf using inside a loop in C

I have a piece of code that looks like this
#include <stdio.h>
int main()
{
int i;
int number_of_chunks = 12;
char *final_string = NULL;
for(i = 0; i < number_of_chunks; i++)
{
char *chunk = some_hash_table.pop(i);
asprintf(&final_string, "%s%s", (final_string==NULL?"":final_string), chunk);
}
free(final_string);
return 0;
}
Here I am concatinating string chunks dynamically, meaning I don't know the size of each chunk in advance. For this I am using asprintf. The code works fine, however rise some serious memory issue. My doubt is asprintf allocates memory in each iteration and the code loses pointer in each iteration. If there is any other way I can concate string inside loop please guide me
To put your question in the simplest possible way, what you are essentially trying to do with the above code is
1. Allocate memory to a pointer continuously(in your case 12 times in the for loop) and
2. free it at the end only once, which is causing memory leak.
Like in the below code
#include <stdio.h>
int main()
{
int i;
int number_of_chunks = 12;
char *final_string = NULL;
for(i = 0; i < number_of_chunks; i++)
{
/*For example: similar to what asprintf does, allocate memory to the pointer*/
final_string = malloc(1);
}
free(final_string);
return 0;
}
From the above example it is easily visible that you have allocated the memory 12 times but freed only once.
code snippet:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i;
int number_of_chunks = 12;
char *final_string = NULL;
char *tmp = NULL;
for(i = 0; i < number_of_chunks; i++)
{
char *chunk = some_hash_table.pop(i);
asprintf(&final_string, "%s%s", (tmp==NULL?"":tmp), chunk);
if (tmp)
free(tmp);
tmp = final_string;
}
printf("%s\n", final_string);
free(final_string);
return 0;
}
Others have already pointed out that you lose the reference to all but the last allocation and that having the same string that is written to as printf argument is probably undefined behaviour, even more so as re-allocations might occur and invalidate the format argument.
You don't use asprintf's formatting capabilities, you use it only to concatenate strings, so you might want to take another approach. You could either collect the strings in an array, determine the needed length, allocate as appropriate and fill the allocated buffer with memcpy.
Or you could write a self-allocating string buffer similar to C++'s std::stringstream, for example:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct append_t {
char *str; /* string */
size_t len; /* length of string */
size_t size; /* allocated size */
};
void append(struct append_t *app, const char *str)
{
size_t len = strlen(str);
while (app->len + len + 1 >= app->size) {
app->size = app->size ? app->size * 2 : 0x100;
app->str = realloc(app->str, app->size);
/* error handling on NULL re-allocation */
}
strcpy(app->str + app->len, str);
app->len += len;
}
int main(int argc, char **argv)
{
struct append_t app = {NULL};
for (int i = 1; i < argc; i++) {
append(&app, argv[i]);
}
if (app.str) puts(app.str);
free(app.str);
return 0;
}

I'm having trouble with allocating memory with strings

I am having trouble with the allocating memory part of my program. I am supposed to read in a file that contains a list of names then allocate memory for them and store them in the allocate memory. This is what I have so far, but I keep getting a segmentation fault when I run it.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_STRING_LEN 25
void allocate(char ***strings, int size);
int main(int argc, char* argv[]){
char **pointer;
int size = atoi(argv[1]);
allocate(&pointer, size);
}
/*Will allocate memory for an array of char
pointers and then allocate those char pointers with the size of MAX_STRING_LEN.*/
void allocate(char ***strings, int size){
**strings = malloc( sizeof (char) * MAX_STRING_LEN);
}
This is currently not working because I am given a seg fault. Thanks a lot for the help in advance.
void allocate(char ***strings, int size)
{
int i;
// Here you allocate "size" string pointers...
*strings = malloc( sizeof (char*) * size);
// for each of those "size" pointers allocated before, here you allocate
//space for a string of at most MAX_STRING_LEN chars...
for(i = 0; i < size; i++)
(*strings)[i] = malloc( sizeof(char) * MAX_STRING_LEN);
}
So, if you pass size as 10...
In your main you will have space for 10 strings (pointer[0] to pointer[9]).
And each of those strings can have up to 24 characters (don't forget the null terminator)...
Pointers are a little tricky but here is a trick to deal with them:
Lets say you have your main like this:
int main()
{
int ***my_variable;
}
and you know how to operate in my_variable inside main...
To use it in a function you do as following:
add an extra * in the parameter
void f(int ****my_param)
and whenever you want to use it inside the function, use the same way as you would use in main with this little change:
(*my_param) = //some code
by using (*my_param) is the same as if you were using to my_variable in main
You need
*strings = malloc(sizeof(char *) * 10); // Here is the array
(*strings)[0] = malloc(MAX_STRING_LEN);
strcpy((*strings)[0], "The first person");
printf("First person is %s\n", (*strings)[0]);
Dunno where size comes in

Passing a string array to a function in C

I am just learning C, Could someone explain why the following code produces a Segmentation fault after printing the first element of an array?
what would working code look like?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ELEMENTS 8
void make(char ***array) {
*array = malloc(ELEMENTS * sizeof(char *));
(*array)[0] = "test0";
(*array)[1] = "test1";
(*array)[2] = "test2";
(*array)[3] = "test3";
(*array)[4] = "test4";
(*array)[5] = "test5";
(*array)[6] = "test6";
(*array)[7] = "test7";
(*array)[8] = "test8";
}
int main(int argc, char **argv)
{
char **array;
make(&array);
int i;
for (i = 0; i < ELEMENTS; ++i) {
printf("%s\n", array[i]);
free(array[i]);
}
free(array);
return 0;
}
Your array size is 8, but you access index 8, which is one past the end of your array. Count the number of elements if you don't understand...
You call free on the assigned string constants. Don't do this. Only free what you malloc, which is just array, not array[0] to array[8].
When you put a literal string in C++ like "test0", it is actually stored in a special memory location where it cannot be modified. In the line
(*array)[0] = "test0";
you're pointing your char* to that memory location, which is alright to do. However, later, when you call free(array[i]);, you are attempting to free the same memory, which is a no-no. In general, only use free() if you have previously used malloc() on the same variable.
Also, as others have said, you need to allocate an array of size 9, since you're using 9 elements.

Resources