I am trying to remove an object from an array of characters that i have dynamically allocated for. But when i check my output for this code I am segfaulting and i dont know why. I am quite new to memory allocation in C. This is just some test code I am writing before I put it into a larger project. Can someone help me debug this?
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main(){
int count = 5;
char* test = malloc(count * sizeof(char));
for (int i = 0; i < count; ++i) {
(test[i]) = 'a';
}
int indexToRemove = 2;
for (int i = 0; i < count; ++i) {
printf("%s ", &(test)[i]);
}
printf("\n");
char* temp = malloc((count - 1) * sizeof(char)); // allocate an array with a size 1 less han the current one
memmove(temp,test,(indexToRemove+1)*sizeof(char)); // copy everything BEFORE the index
memmove(temp+indexToRemove,(test)+(indexToRemove+1),(count - indexToRemove)*sizeof(char)); \\copy everything AFTER the index
for (int i = 0; i < count-1; ++i) {
printf("%s ", &(temp)[i]);
}
printf("\n");
count--;
return 0;
}
You've made two major mistakes. The first is using this:
char** test = malloc(count * sizeof(char*));
instead of this:
char* test = malloc(count * sizeof(char));
There's no reason to use double-indirection here, and it leads to a lot of loose ends and bugs.
The second is here:
free(test);
*test = temp;
You free the space-- and then you write something into it. This is an illegal move which results in undefined behavior which, like any undefined behavior, might work perfectly a thousand times before crashing spectacularly.
EDIT: Here's a version that seems to work:
int count = 5;
char *test = malloc(count * sizeof(char));
test[0] = 'a';
test[1] = 'b';
test[2] = 'c';
test[3] = 'd';
test[4] = 'e';
int indexToRemove = 2;
char* temp = malloc((count - 1) * sizeof(char));
memmove(temp,test,(indexToRemove+1)*sizeof(char));
memmove(temp+indexToRemove,(test)+(indexToRemove+1),(count - indexToRemove)*sizeof(char));
for (int i = 0; i < count-1; ++i) {
printf("%c ", temp[i]);
}
printf("\n");
free(test);
return 0;
Related
Im just curious if there is anyway of directly converting a char array to a char* array such as:
char charArray[5]={'h','e','l','l','o'};
'Converted' to:
char *pointerArray[5]={"h","e","l","l","o"};
How about this?
#include <stdio.h>
#include <stdlib.h>
char **convert_array(char *arr_in, size_t arr_in_sz)
{
char **arr_out = (char **)malloc(arr_in_sz * sizeof(char *));
for (size_t i = 0; i < arr_in_sz; ++i) {
arr_out[i] = (char *)malloc(2);
arr_out[i][0] = arr_in[i];
arr_out[i][1] = '\0';
}
return arr_out;
}
void free_array(char **arr, size_t arr_sz)
{
for (size_t i = 0; i < arr_sz; ++i)
free(arr[i]);
free(arr);
}
int main()
{
char array_char[] = {'h', 'i'};
char **array_str = convert_array(array_char, sizeof(array_char));
for (size_t i = 0; i < sizeof(array_char); ++i)
printf("%s\n", array_str[i]);
free_array(array_str, sizeof(array_char));
}
You only need a place to store the string literals. You need twice as much memory as for the charArray - to store letters and zero terminating characters, one zero for each letter - twice as much memory. Then initialize pointerarray to point to every second character inside memory. Like so:
#include <stdio.h>
int main() {
char chararray[5]={'h','e','l','l','o'};
char memory[sizeof(chararray) * 2];
for (unsigned i = 0; i < sizeof(chararray); ++i) {
memory[i * 2] = chararray[i];
memory[i * 2 + 1] = '\0';
}
char *pointerarray[5];
for (unsigned i = 0; i < sizeof(chararray); ++i) {
// this loop could me merged with above loop
pointerarray[i] = &memory[i * 2];
}
for (unsigned i = 0; i < 5; ++i) {
printf("%s\n", pointerarray[i]);
}
}
The first one is one dimensional array and secon one is two dimensional. So if you just use pointer like that :
char *pointerArray it means you have an array but you can not assign like in the first example.
I have been trying to figure out how to modify an array of char pointers but no matter what I do there appears to be no change below are the three arrays I'm trying to change including the call to the function I'm using.
char*cm1[5];
char*cm2[5];
char*cm3[5];
setupCommands(&cm1,commands,file,0);
setupCommands(&cm2,commands,file,1);
setupCommands(&cm3,commands,file,2);
The code below is the function itself.I was thinking that maybe it involves a double pointer but if I try *cmd to change the array I get a segmentation fault.
void setupCommands(char **cmd[], char* commands[],char file[],int index){
char str1[255];
strcpy(str1,commands[index]);
char newString [5][255];
int j = 0;
int ctr = 0;
int i;
//printf("str1 %s\n" ,str1);
for(i = 0; i <= strlen(str1); i++){
if(str1[i] == ' '|| str1[i] =='\0'){
newString[ctr][j] = '\0';
ctr++;//next row
j=0;// for next word, init index to 0
}else{
newString[ctr][j]=str1[i];
j++;
}
}
for(i = 0; i < ctr; i++){
//printf(" test2 %s \n", newString[i]);
cmd[i] = newString[i];
//printf(" test2 %d %s \n", i,cmd[i]);
}
//printf("index %d", i);
cmd[i]= file;
cmd[i + 1] = NULL;
//execvp(cmd[0],cmd);
//cmd
}
There are a few issues with your code:
you are trying to return references to the local 'char newString [5][255]' when the function exits. In simple worlds - never return anything locally allocated on the stack. This is the reason you are getting the segmentation fault.
char **cmd[] must be declared char *cmd[] - even though you will get a warning from the compiler assignment from incompatible pointer type, the code would run and execute correctly(essentially **cmd[] would do the same work as *cmd[], even though it's not of correct type) if you didn't return references to the local object;
Easy and simple optimization is just to remove the array str1 and directly operate on the array commands.
Apart from this simple optimization I have changed your code to overcome the segmentation fault, by allocating on the heap, instead on stack(will live until the program terminates) the multidimensional array, and I also calculate it's size so I will know how much memory to allocate. Now it's safe to return references to it.
Note that more optimizations could be made, but for the sake of the simplicity this is the bare minimal for this code to work.
int setupCommands(char *cmd[], char *commands[], char file[], int index)
{
int j = 0;
int ctr = 0;
int i = 0;
int rows = 0;
int cols = 0;
char **newString = NULL;
while(commands[index][i])
{
if (commands[index][i] == ' ')
{
++rows;
}
++i;
}
++rows;
cols = strlen(commands[index]) + 1;
newString = malloc(rows * sizeof(*newString));
if (newString == NULL)
{
return -1;
}
for (i = 0; i < rows; ++i)
{
newString[i] = malloc(cols * sizeof(*newString));
if (newString[i] == NULL)
{
return -1;
}
}
for(i = 0; i <= strlen(commands[index]); i++){
if(commands[index][i] == ' '|| commands[index][i] =='\0'){
newString[ctr][j] = '\0';
ctr++;//next row
j=0;// for next word, init index to 0
}else{
newString[ctr][j]=commands[index][i];
j++;
}
}
for(i = 0; i < ctr; i++){
cmd[i] = newString[i];
}
cmd[i]= file;
cmd[i + 1] = NULL;
return 0;
}
First of all - being the three stars pointer programmer is not good :)
You assign it with pointer to the local variable which is not longer available after the function return
But if you still want the three stars pointers:
char **cm1;
char **cm2;
char **cm3;
setupCommands(&cm1,commands,file,0);
setupCommands(&cm2,commands,file,1);
setupCommands(&cm3,commands,file,2);
#define MAXWORD 256
int setupCommands(char ***cmd, const char *commands,const char *file,int index){
char str1[255];
strcpy(str1,commands[index]);
int j = 0;
int ctr = 0;
int i;
//printf("str1 %s\n" ,str1);
*cmd = malloc(sizeof(char *));
**cmd = malloc(MAXWORD);
if(!*cmd || !**cmd)
{
/* do spmething if mallocs failed*/
return -1;
}
for(i = 0; i <= strlen(str1); i++){
if(str1[i] == ' '|| str1[i] =='\0'){
(*cmd)[ctr][j] = '\0';
ctr++;//next row
*cmd = realloc((ctr + 1) * sizeof(int));
(*cmd)[ctr] = malloc(MAXWORD);
if(!*cmd || !*cmd[ctr])
{
/* do spmething if mallocs failed*/
return -1;
}
j=0;// for next word, init index to 0
}else{
(*cmd)[ctr][j]=str1[i];
j++;
}
}
*cmd = realloc(sizeof(char *) * ctr + 2)
(*cmd)[ctr - 2] = malloc(MAX);
if(!*cmd || !*cmd[ctr - 2])
{
/* do spmething if mallocs failed*/
return -1;
}
strcpy((*cmd)[ctr - 2], file);
(*cmd)[ctr - 1] = NULL;
return 0;
//execvp(cmd[0],cmd);
//cmd
}
you can improve many things (for example do not realloc every time but in the larger chunks) and I did not change anything in your code logic.
I am getting a segmentation fault 11 error on the print statement in the while(splitting) loop. If I comment out the assignment statement the printf statement executes perfectly, But if I don't, the print statement won't work and will tell me the index is out of bounds. With the assignment statement under it, the print statement won't even execute on the 4th iteration, unless I comment out the assignment statement. Any insight would be greatly appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char **argv){
char* filename = argv[1];
FILE* file = fopen(filename, "r");
int features = atoi(argv[2]);
int examples = atoi(argv[3]);
printf("Filename: %s\n", filename);
printf("Features: %d\n", features);
printf("Training examples: %d\n", examples);
int **X = malloc(examples * sizeof(int));
for(int i = 0; i < examples; i++){
X[i] = malloc(features * sizeof(int));
}
int lineLength = (2 * features) + 1;
char *example = malloc(lineLength * sizeof(char));
char *splitting = malloc(lineLength * sizeof(char));
int exampleNumber = 0;
int featureNumber;
while(fgets(example, lineLength, file)){
printf("%d: ", exampleNumber);
printf("%s\n", example);
splitting = strtok(example, ",");
featureNumber = 0;
while(splitting){
printf("%d,%d ", exampleNumber, featureNumber);
X[exampleNumber][featureNumber] = atoi(splitting);
splitting = strtok(NULL, ",");
featureNumber++;
}
printf("\n");
exampleNumber++;
}
for(int i = 0; i < examples; i++){
for(int j = 0; j < features; j++){
printf("%d,", X[i][j]);
}
printf("\n");
}
fclose(file);
}
One thing I noticed is that you are using the wrong size in first call to malloc.
int **X = malloc(examples * sizeof(int));
^^^^^
It needs to be sizeof(int*), or sizeof(*X).
PS That might not solve everything.
I am trying to pass a array of pointers to string to a function where I need to set the values. In the passing function I do not know the number of strings I will get, the called function is calling some other function which returns list of strings.
Sample code below:
int main() {
char** list;
create(list);
}
int create(char **array) {
char* str[] = { "hello", "dear" };
int len;
int i = 0;
for (i = 0; i < 2; i++) {
len = strlen(str[i]);
printf("%d\n", len);
*(array + i) = (char*) malloc(len * sizeof(char*));
strcpy(*(array + i), str[i]);
i++;
}
return 1;
}
This gives me segmentation fault.
What wrong am I doing here. Please help.
Thanks
EDIT
Updated code from below comments:
int main() {
char** list;
create(list);
int i = 0;
for (i = 0; i < 2; i++) {
printf("%s\n", list[i]); // segmentation fault
}
}
int create(char **array) {
char* str[] = { "hello", "dear" };
int len;
int i = 0;
array = malloc(2 * sizeof(char*));
for (i = 0; i < 2; i++) {
len = strlen(str[i]);
printf("%d\n", len);
*(array + i) = (char*) malloc(len * sizeof(char));
strcpy(*(array + i), str[i]);
printf("%s\n", array[i]); // this prints
}
return 1;
}
Now getting segmentation fault in main while printing the list.
Actual code where I am reading the strings
int i;
for ( i=0; i<reply->elements; i++ )
{
printf( "Result: %d---%s\n", i,reply->element[i]->str );
*array[i] = (char*)malloc(strlen(reply->element[i]->str));
printf("***");
strcpy(array[i],reply->element[i]->str);
printf( "Array[%d]: %s\n", i,array[i] );
}
You correctly alloc memory for the individual strings, but fail to alloc some for the array itself.
You should use:
int main() {
char* list[8] = {0}; /* initialize pointers to NULL */
create(list);
/* free allocated memory - free(NULL) is legal and is a noop */
for (i=0; i<sizeof(list)/sizeof(list[0]); i++) free(list[i]);
return 0; /* never return random value from main */
}
And you should remove the i++ at the end of the loop in function create because it leads to a double increment.
Alternatively you could alloc the array itself in the function create:
int create(char ***array) {
char* str[] = { "hello", "dear" };
int len;
int i = 0;
*array = malloc(1 + sizeof(str)/sizeof(str[0]));
for (i = 0; i < 2; i++) {
len = strlen(str[i]) + 1;
printf("%d\n", len);
(*array)[i] = malloc(len * sizeof(char*));
strcpy((*array)[i], str[i]);
}
(*array)[i] = NULL;
return i;
}
int main() {
char** list;
create(&list);
}
In above code, the length of the array is the return value from create, and the last element of list is a NULL (in the same logic as argc/argv).
You need to allocate some space for list or undefined behavior occurs:
char* list[2];
You increment i twice; therefore, remove the i++ from the bottom of the for loop.
Minor notes:
refer to string literals as const char*
use array[i] instead of *(array + i)
don't cast the result of malloc
malloc allocates too much space as you allocate len char*s, even though you need just chars. Also, as #CoolGuy noted, you need one extra byte for the null byte. Replace the allocation with
array[i] = malloc(len * sizeof(char) + sizeof(char));
or
array[i] = malloc(len + 1);
call free after malloc
you assign 0 twice to i; remove the initialization
You allocate two arrays (char*) to store the strings "hello" and "dear" but does not allocate the array (char**) containing those two string array.
I would suggest you to change declaration of function create to this -
int create(char ***array);
And call it like this -
create(&list);
In function create allocate memory like this -
*array = malloc(2 * sizeof(char*));
for (i = 0; i < 2; i++)
{
len = strlen(str[i]);
printf("%d\n", len);
(*array)[i] =malloc(len * sizeof(char*)+1);
strcpy((*array)[i], str[i]);
}
And do the printing as you do in main.
Note - free memory that you allocate.
And you should declare len as type size_t -> size_t len;
and print it with %zu specifier in printf .
See working code here -https://ideone.com/GX2k9T
I'm using a dynamic array of strings in C:
char** strings;
I initialize it:
int max = 10;
strings = malloc(sizeof(char*) * max);
And copy a couple of dummy strings:
char* str = "dummy";
for (int i = 0; i < max; i++) {
strings[i] = malloc(strlen(str) + 1);
strncpy(strings[i], str, strlen(str) + 1);
}
Yet when I try to print this:
for (int i = 0; i < max; i++)
printf("array = %s", strings[i])
I get this error from Splint:
Value strings[] used before definition
An rvalue is used that may not be initialized to a value on some execution
path. (Use -usedef to inhibit warning)
Checking for NULL like this will not help:
for (int i = 0; i < max; i++)
if (strings[i] != NULL)
printf("array = %s", strings[i])
since strings[i] is still used "before definition".
Any ideas on how to solve this?
Edit: Will try this with a linked list instead, I think.
Also, complete code listing:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char** strings;
int i;
int max = 10;
char* str = "hello";
// Dynamic array with size max
strings = malloc(sizeof(char*) * max);
// Abort if NULL
if (strings == NULL)
return (-1);
// Define strings
for (i = 0; i < max; i++)
{
strings[i] = malloc(strlen(str) + 1);
// Abort if NULL
if (strings[i] == NULL)
{
// Undetected memory leak here!
free(strings);
return (-1);
}
strncpy(strings[i], str, strlen(str) + 1);
}
// Print strings
for (i = 0; i < max; i++)
{
if (strings[i] != NULL)
printf("string[%d] = %s\n", i, strings[i]);
}
// Free strings
for (i = 0; i < max; i++)
{
if (strings[i] != NULL)
free(strings[i]);
}
free(strings);
return 0;
}
I do not have Splint on my machine, so i cannot test with it, just an another way to your task:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int i, len, max;
char* str = "hello";
len = strlen(str) + 1;
max = 10;
char strings[max][len];
for (i = 0; i < max; i++) {
strcpy(strings[i], str);
}
for (i = 0; i < max; i++) {
printf("string[%d] = %s\n", i, strings[i]);
}
return 0;
}
Avoid creating non-continuous memory it would be better approach if you allocate memory in single malloc call.
Memory can be freed in single free call instead of multiple free call
max_rows * sizeof(char) will allocate 2 * 1
((strlen(str) * N) + 1) will allocate memory for every N element.
Here is my approch
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
size_t max_rows = 2;
char* str = "dummpy";
char* vec_s = (char *) malloc( max_rows * sizeof(char) * ((strlen(str) * max_rows) + 1));
for (int i = 0; i < max_rows; i++){
strcpy((vec_s + i), str);
printf("vec_s[%d]=%s\n", i, (vec_s + i));
}
free(vec_s);
return 0;
}