I'm attempting to pass a pointer to a pointer (char**) into a function that will initialize it, and then pass it into another function that will free the memory, however I'm getting seg faults on the freeing which leads me to believe my allocation is going wrong.
Valgrind is reporting use of uninitalized value at this line. tmp[i] is pointing to 0x0.
if(tmp[i]) free((char*)tmp[i]);
Here is the code (this is only test code)
void
alloc_strings(char ***test, int count)
{
char **tmp = *test;
tmp = malloc(count * sizeof(char*));
int i;
for(i = 0; i < count; i++) {
tmp[i] = malloc(6);
strcpy(tmp[i],"Hello");
}
}
void
free_strings(char ***test, int count)
{
char **tmp = *test;
int i;
for(i = 0; i < count; i++) {
if(tmp[i]) free((char*)tmp[i]);
}
if(tmp)
free(tmp);
}
And the invocation:
int
main(int argc, char **argv)
{
char **test;
alloc_strings(&test, 10);
free_strings(&test, 10);
return 0;
}
I have been playing around with this for a while, reading up on pointers etc however can't get my head around the issue. Any thoughts greatly appreciated!
You need to assign to *test, not to assign from it. How about:
void
alloc_strings(char ***test, int count)
{
char **tmp = malloc(count * sizeof *tmp);
/*...*/
*test = tmp;
}
In the code example,
alloc_strings(char ***test, int count)
{
char **tmp = *test;
*test should have some space to store a pointer to char ** which currently is not allocated. Hence, if the example is as this
char** array[1];
alloc_strings(&array[0], 7);
I feel that the code will work.
Related
I want to initialize array of char arrays in a function:
void myFunction(char*** words)
{
int size = 3;
char** words_ = (char**) malloc(size * sizeof(char*));
for (int i=0; i< size; ++i)
{
// init each word words[i] with some value
}
*words = words_;
}
which I use in that way:
char** multiple_words;
myFunction(&multiple_words);
Is there any other way to write this code better/simpler?
(This code works BTW).
char** myFunction()
{
int size = 3;
char** words_ = (char**) malloc(size * sizeof(char*));
for (int i=0; i< size; ++i)
{
// init each word words[i] with some value
}
return words_;
}
char **words = myFunction();
You can simplify the malloc call itself a bit. You don't need to cast the result of malloc in C1, so that call could be simplified to
char** words_ = malloc(size * sizeof *words); // sizeof *words == sizeof (char *)
Always check the result of a malloc, calloc, or realloc call. Even though the likelihood of the request failing is small, it's not zero.
The words_ variable really serves no purpose, and at first glance looked like you were redeclaring the words function argument. It would be simpler to get rid of it entirely and just write
*words = malloc( sizeof **words * size ); // sizeof **words == sizeof (char *)
leaving us with
void myFunction(char*** words)
{
int size = 3;
*words = malloc(size * sizeof **words);
if ( *words )
{
for (int i=0; i< size; ++i)
{
// init each word words[i] with some value
}
}
}
At least as of the 1989 standard - if you're using an ancient K&R-era implementation or if you're compiling this code as C++, then a cast is required. However, if you're writing C++, then you shouldn't be using malloc anyway.
I would create abstractions depending on what I want to represent:
struct word {
char *letters;
};
struct sentence {
struct word *words;
};
// and then work with those abstractions:
int sentence_create(struct sentence *s) {
const size_t size = 3;
s->words = malloc(size * sizeof(*s->words));
if (s->words == NULL) goto ERR_WORDS;
s->words[0] = strdup("hello");
if (s->words[0] == NULL) goto ERR_0;
s->words[1] = strdup("world");
if (s->words[1] == NULL) goto ERR_1;
s->words[2] = NULL;
// success
return 0;
// goto error handling
free(s->words[1]);
ERR_1:
free(s->words[0]);
ERR_0:
free(s->words);
s->words = NULL;
ERR_WORDS:
return -ENOMEM;
}
int main() {
struct sentence sentence;
if (sentence_create(&sentence) != 0) {
fprintf(stderr, "Oh no!");
abort();
}
// TODO: sentence_destroy(&sentence) to free memory
}
Also see wiki.c2 Three Star Programmer
Not sure 100% whether it is easier or not:
typedef struct {
char **words;
} WordList;
void myFunc(WordList *wordList) {
...
wordList->words = malloc(numberOfWords * sizeof(char *));
// strSource : this string comes from somewhere in your code
...
for (int i = 0; i < numberOfWords; i++) {
int numberOfChars = strlen(strSource);
(wordList->words)[i] = malloc((numberOfChars + 1) * sizeof(char));
(wordList->words)[i][numberOfChars] = '\0';
...
strcpy((wordList->words)[i], strSource)
...
}
...
}
int main() {
WordList wordList;
...
myFunc(&wordList);
...
}
This is gonna get icky no matter how you write it.
A char** is fine to use for pointing at the first item in an array of char*, each pointing at a string of individual length.
It is best if the function can return a char**, but if that isn't possible, then...
We'd have to return a char** through parameters, means we have to write char***. Three levels of indirection is always questionable, but this specific case is about the only valid use for it. Or the lesser evil at least, since...
Some makeshift struct wrapper that does nothing but hiding away the *** isn't making the code any better or more readable. That's very similar to hiding pointers behind typedef, not recommended.
The least messy way to write that might be something like this:
void heard (char*** word, size_t size)
{
char** the_word = malloc( sizeof(char*[size]) );
for(size_t i=0; i<size; i++)
{
const char* str = "bird"; // some random data from somewhere
the_word[i] = malloc (sizeof str); // allocate room for individual strings
strcpy(the_word[i], str);
}
*word = the_word;
}
Then call it as:
char** word;
heard(&word, 3);
for(size_t i=0; i<3; i++)
{
puts(word[i]);
}
The most proper solution is probably to use a complete string container class which handles all of this for you. Then you'd just declare arrays of strings and don't worry about all the details of allocating things manually.
So, here's my logic:
This is some text:
char *text;
Then this is array of texts:
char **arr;
Then array of these arrays is:
char ***arr2d;
And if I want a function to modify it, it needs to accept it as:
char ****arr2d;
And within the function use it as:
*arr2d = (e.g. allocate);
So if I want to create 2D array like this and make the first row, first column contain just a letter 'a', then why does this not work?
#define COLUMNS 7
void loadTable(char ****table)
{
*table = (char ***) malloc(sizeof(char**));
if (!*table) {
printf("Allocation error for table rows.");
return;
}
*table[0] = (char**) malloc(COLUMNS * sizeof(char*));
if (!*table[0]) {
printf("Allocation error for table columns.");
return;
}
*table[0][0] = (char*) malloc(2 * sizeof(char));
*table[0][0][0] = (char)(97);
*table[0][0][1] = '\0';
}
int main()
{
char ***table;
loadTable(&table);
return 0;
}
You would need only 3 *** to do what you describe, not 4 ****. Be aware, there are methods to allow you to avoid excessive depth in terms of arrays of arrays of strings. And there are also good reasons to avoid excessively deep orders of arrays, not the least is the need to free(.) everything you allocate. That means exactly one call to free(.) for each and every call to [m][c][re]alloc(.).
But to address your question...
In general, to create new memory for a single array of a previously allocated memory, you could use a function prototyped as:
char * ReSizeBuffer(char **str, size_t origSize);
Where if say the previously allocated buffer were created as:
char *buf = calloc(origSize, 1);
...the usage could look like:
char *tmp = {0};
tmp = ReSizeBuffer(&buf, newSize); //see implementation below
if(!tmp)
{
free(buf);
return NULL;
}
buf = tmp;
///use new buf
...
Then if this works for a single array of char, then the prototype for allocating new memory for a previously allocated array of strings might look like this:
char ** ReSizeBuffer(char ***str, size_t numArrays, size_t strLens);
Where if say the previously allocated 2D buffer were created as:
char **buf = Create2DStr(size_t numStrings, size_t maxStrLen); //see implementation below
...the usage could look like:
char **tmp = {0};
tmp = ReSizeBuffer(&buf, numStrings, maxStrLen);
if(!tmp)
{
free(buf);
return NULL;
}
buf = tmp;
///use new buf
...
Implementations:
Implementation of ReSizeBuffer. This must be expanded if you desire to implement the second prototype:
char * ReSizeBuffer(char **str, size_t size)
{
char *tmp={0};
if(!(*str)) return NULL;
if(size == 0)
{
free(*str);
return NULL;
}
tmp = (char *)realloc((char *)(*str), size);
if(!tmp)
{
free(*str);
return NULL;
}
*str = tmp;
return *str;
}
Implementation of Create2DStr might look like this:
char ** Create2DStr(size_t numStrings, size_t maxStrLen)
{
int i;
char **a = {0};
a = calloc(numStrings, sizeof(char *));
for(i=0;i<numStrings; i++)
{
a[i] = calloc(maxStrLen + 1, 1);
}
return a;
}
I just stumbled upon this old question of mine and spotted the problem immediately. Here is the answer:
The number of asterisks is actually correct, the problem is operator evaluation. Specifically, all lines of code of this form:
*table[0]
should have been written as:
(*table)[0]
Here is my problem: I have to make this program for school and I spent the last hour debugging and googling and haven't found an answer.
I have an array of structures in my main and I want to give that array to my function seteverythingup (by call by reference) because in this function a string I read from a file is split up, and I want to write it into the structure but I always get a SIGSEV error when strcpy with the struct array.
This is my main:
int main(int argc, char *argv[])
{
FILE* datei;
int size = 10;
int used = 0;
char line[1000];
struct raeume *arr = (raeume *) malloc(size * sizeof(raeume*));
if(arr == NULL){
return 0;
}
if(argc < 2){
return 0;
}
datei = fopen(argv[1], "rt");
if(datei == NULL){
return 0;
}
fgets(line,sizeof(line),datei);
while(fgets(line,sizeof(line),datei)){
int l = strlen(line);
if(line[l-1] == '\n'){
line[l-1] = '\0';
}
seteverythingup(&line,arr,size,&used);
}
ausgabeunsortiert(arr,size);
fclose(datei);
return 0;
}
and this is my function:
void seteverythingup(char line[],struct raeume *arr[], int size,int used)
{
char *token,raumnummer[5],klasse[6];
int tische = 0;
const char c[2] = ";";
int i=0;
token = strtok(line, c);
strcpy(raumnummer,token);
while(token != NULL )
{
token = strtok(NULL, c);
if(i==0){
strcpy(klasse,token);
}else if(i==1){
sscanf(token,"%d",&tische);
}
i++;
}
managesize(&arr[size],&size,used);
strcpy(arr[used]->number,raumnummer);
strcpy(arr[used]->klasse,klasse);
arr[used]->tische = tische;
used++;
}
Edit: Since there is more confusion I wrote a short program that works out the part you are having trouble with.
#include <cstdlib>
struct raeume {
int foo;
int bar;
};
void seteverythingup(struct raeume *arr, size_t len) {
for (size_t i = 0; i < len; ++i) {
arr[i].foo = 42;
arr[i].bar = 53;
}
}
int main() {
const size_t size = 10;
struct raeume *arr = (struct raeume*) malloc(size * sizeof(struct raeume));
seteverythingup(arr, size);
return 0;
}
So basically the signature of your functions is somewhat odd. Malloc returns you a pointer to a memory location. So you really dont need a pointer to an array. Just pass the function the pointer you got from malloc and the function will be able to manipulate that region.
Original Answer:
malloc(size * sizeof(raeume*));
This is probably the part of the code that gives you a hard time. sizeof returns the size of a type. You ask sizeof how many bytes a pointer to you raeume struct requires. what you probably wanted to do is ask for the size of the struct itself and allocate size times space for that. So the correct call to malloc would be:
malloc(size * sizeof(struct raeume));
I have declared the following struct:
typedef struct _RECOGNITIONRESULT {
int begin_time_ms, end_time_ms;
char* word;
} RECOGNITIONRESULT;
There is a method that creates an array of RECOGNITIONRESULT and fills it (for test purposes only):
void test_realloc(RECOGNITIONRESULT** p, int count){
int i;
*p = (RECOGNITIONRESULT *)realloc(*p, count * sizeof(RECOGNITIONRESULT));
for (i = 0; i < count; i++){
(*p)[i].begin_time_ms = 2*i;
(*p)[i].end_time_ms = 2*i+1;
(*p)[i].word=(char *) malloc ( (strlen("hello"+1) * sizeof(char ) ));
strcpy((*p)[i].word,"hello");
}
}
The method to free memory is:
void free_realloc(RECOGNITIONRESULT* p, int count){
int i = 0;
if(p != NULL){
if (count > 0){
for (i = 0; i < count; i++){
free(p[i].word); //THE PROBLEM IS HERE.
}
}
free(p);
}
}
The main method calls those methods like this:
int main(int argc, char** argv)
{
int count = 10;
RECOGNITIONRESULT *p = NULL;
test_realloc(&p,count);
free_realloc(p,count);
return 0;
}
Then if I try to free the memory allocated for "word", I get the following error:
HEAP CORRUPTION DETECTED: after normal block (#63) at 0x003D31D8.
CRT detected that the application wrote to memory after end of heap buffer.
Using the debugger I've discovered that the crash occurs when calling free(p[i].word);
What am I doing wrong? How can I free he memory for the strings?
The problem is in your allocation of memory for word. strlen("hello"+1) should be strlen("hello")+1.
Since you appear to allocate a whole array of structures in one strike
RECOGNITIONRESULT **p;
*p = (RECOGNITIONRESULT *)realloc(*p, count * sizeof(RECOGNITIONRESULT));
you can free them in one call to free() as well :
void free_realloc(RECOGNITIONRESULT *p, int count){
free(p);
}
And the strlen("hello"+1) is also wrong, as detected by Chowlett.
I am having issues with filling an array of strings that was created dynamically. The array basically contains two strings and I would like to print them both with their lengths. I am getting a very weird result whereby the memory locations are getting mixed up. Please see the code below.
Any suggestions would greatly be appreciated. Thanks.
void printWords(char **words, int numberOfWords)
{
int index;
for (index = 0; index < numberOfWords; index++)
{
printf("%s, %d\n", &(*words)[index], (int)strlen(&(*words)[index]));
}
}
void fillWords(char **words)
{
*words = malloc(2 * sizeof(char *));
char hello[] = {"Hello"};
(*words)[0] = (char)malloc(strlen(hello) * sizeof(char));
strcpy(&(*words)[0], hello); //Copy word into array
char world[] = {"Worldz"};
(*words)[1] = (char)malloc(strlen(world) * sizeof(char));
strcpy(&(*words)[1], world); //Copy word into array
}
int main (int argc, const char * argv[])
{
char *words;
fillWords(&words);
printWords(&words, 2);
return 0;
}
The expected output should be
Hello, 5
Worldz, 6
however I am getting
HWorldz, 7
Worldz, 6
I think you are getting confused between char * and char **.
Also, be sure to allocate enough memory for the null-termination character at the end of the string.
Here is my solution:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
void printWords(char **words, int numberOfWords)
{
int index;
for (index = 0; index < numberOfWords; index++)
{
printf("%s, %d\n", words[index], (int)strlen(words[index]));
}
}
char ** createWords()
{
char ** words;
// Allocate memory for an array of pointers, length 2.
words = malloc(2 * sizeof(char *));
char hello[] = {"Hello"};
words[0] = malloc((strlen(hello)+1) * sizeof(char));
strcpy(words[0], hello); //Copy word into array
char world[] = {"Worldz"};
words[1] = malloc((strlen(world)+1) * sizeof(char));
strcpy(words[1], world); //Copy word into array
return words;
}
int main (int argc, const char * argv[])
{
char **words;
words = createWords();
printWords(words, 2);
return 0;
}
I renamed fillWords to createWords and made it return a pointer instead of taking a pointer as an argument. If you really want fillWords to take a pointer as an argument, you can do it, but the argument will have to be of type char ***.
First problem: you can't put 2 words in a char * (well ... you can, but not the way you're doing; and the way to do it is nothing like your code).
You need an "array of strings", or, more C-like, an array of char *:
char *words[2]; /* words[0] and words[1] are pointers to char */
char *wrong; /* wrong[0] is a char; wrong[1] is a char */
So, change the definition of words in your main and check/edit all the other functions for correctness.
int main (int argc, const char * argv[])
{
char *words[2];
fillWords(words);
printWords(words, 2);
return 0;
}
Here is the solution. I was getting confused between char* and char. What I want was an array of chars.
void printWords(char **words, int numberOfWords)
{
int index;
for (index = 0; index < numberOfWords; index++)
{
printf("%s, %d\n", words[index], (int)strlen(words[index]));
}
}
void fillWords(char **words)
{
*words = malloc(2 * sizeof(char *));
char hello[] = {"Hello"};
words[0] = malloc(strlen(hello) * sizeof(char));
strcpy(words[0], hello); //Copy word into array
char world[] = {"Worldz"};
words[1] = malloc(strlen(world) * sizeof(char));
strcpy(words[1], world); //Copy word into array
}
int main (int argc, const char * argv[])
{
char *words;
fillWords(&words);
printWords(&words, 2);
return 0;
}