I wrote the following function which will break in the lines marked with // Breakpoint:
char *parseNextWord(char *str)
{
static char *lastStr = "";
static int lastPosition = 0;
if (strcmp(lastStr, str) != 0)
{
lastStr = str;
lastPosition = 0;
}
if (lastPosition > 0 && str[lastPosition - 1] == 0)
{
return 0;
}
char *word = "";
int wLength = 0;
while (str[lastPosition] != ' ' && str[lastPosition] != '\n' && str[lastPosition] != '\0')
{
char *tmp = (char*)malloc(++wLength * sizeof(char));
for (int i = 0; i < sizeof(word); i++)
{
tmp[i] = word[i];
}
tmp[sizeof(*tmp) - 1] = str[lastPosition];
free(word); // Breakpoint
word = (char*)malloc(sizeof(*tmp));
for (int i = 0; i < sizeof(tmp); i++)
{
word[i] = tmp[i];
}
free(tmp); // Breakpoint
lastPosition++;
}
while (str[lastPosition - 1] != '\0' && (str[lastPosition] == ' ' || str[lastPosition] == '\n' || str[lastPosition] == '\0'))
{
lastPosition++;
}
return word;
}
The function can be called like this:
char* string = "Name1 Name2\nName3 Name4\nName1";
int totalCount = 0;
char *nextWord = parseNextWord(string);
while (nextWord != 0)
{
for (int c = 1; c < argc; c++)
{
if (strcmp((const char*)argv[c], nextWord) == 0)
{
totalCount++;
}
}
nextWord = parseNextWord(string);
}
Why is my code breaking on free? How can I improve it?
The relevant code I see is:
char* word = "";
free(word);
You did not allocate the empty string (""), so you cannot free it.
You can only free what you malloc
If you didn't allocate it, don't try to free it.
P.S. Here is my best list of functions which allocate memory that can be freed:
malloc
realloc
calloc
strdup
asprintf
vasprintf
(notably: _not_ alloca)
Maybe there are others as well?
because you didn't allocate word the first time you entered the loop.
you just make it point to "" which is not allocated dynamically.
my suggestion to make it work is to add integer variable before the while with initial value 0 :
if (flag != 0) {
free(word);
} else {
word = 1;
}
Related
I recently started learning basic C and I am still noobie with it, I started doing some projects for learning and I am using library functions but I am interested in other methods...
So I have an email validation, it works fine, but I want to do it without strlen, any suggestion what can I do instead of strlen?
void mail(char e[]) {
int count = 0;
int countb = 0;
int i, j;
int t, t2;
int p = 0;
for (i = 0; i < strlen(e); i++) {
if (e[i] == '#') {
count++;
t = i;
}
}
if (count == 1) {
for (j = 0; j < t; j++) {
if (!(e[j] == '_' || e[j] == '.' || isalpha(e[j]) || isdigit(e[j]))) {
p = -1;
printf("\nwrong\n");
break;
}
}
if (p == 0) {
for (i = t; i < strlen(e); i++) {
if (e[i] == '.') {
t2 = i;
countb++;
}
}
if (countb == 1) {
for (i = 0; i < t2 && i > t2; i++) {
if (!(isalpha(e[i]))) {
p = -1;
printf("\nwrong\n");
break;
} else {
p = 1;
}
}
if (p == 1) {
if (e[t2 + 3] != '\0') {
p = -1;
printf("\nwrong\n");
}
}
} else {
p =- 1;
printf("\nwrong\n");
}
}
} else {
p = -1;
printf("\nwrong\n");
}
return;
}
Instead of comparing i < strlen(e) you can test if the byte at offset i is not the null terminator: e[i] != '\0'.
You can also compute the length of the string just once at the beginning of the function and store it into a len variable.
The only thing you can do is to implement your own strlen function.
In C, strings are null-terminated char arrays, that means, the last character of an array is '\0' (zero). The strlen function iterates over the character array until a null-character is found, it returns the length of that array, the null-character not included.
To duplicate (create) a c-string, you have to do the following:
char *str = malloc(strlen(another_string) + 1);
str[strlen(another_string)] = '\0';
Example for strlen:
size_t my_strlen(const char *str)
{
if (!str) return 0;
size_t n = 0;
for (; *str; ++n, ++str);
return n;
}
You can create you own my_strlen :)
unsigned int my_strlen(char *str)
OR
you can loop while your char is different of '\0' (null byte). So just replace strlen(e) with e[i] != '\0'
It should work just fine
I am supposed to save every sequence of digits from a string in an array of chars , this is what i tried:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int check_number(char *s) {
for (; *s; ++s) {
if (!isdigit(*s))
return 0;
}
return 1;
}
void int_in_string(char *s, char **ar, int MaxCap) {
char temp[100];
int index = 0;
int i = 0;
for (; *s; s++) {
if (index == MaxCap) {
break;
}
if (isdigit(*s)) {
temp[i++] = *s;
}
if (*s == ' ' && check_number(temp)) {
ar[index++] = temp;
memset(temp, '\0', i);
i = 0;
}
}
if (index == 0) {
printf("no numbers in string");
}
for (int i = 0; i < index; i++)
printf(" %s \n", ar[i]);
}
but this code only prints several newlines , can someone explain me what i do wrong?
Some issues:
ar[index++]=temp;
This is just storing the same value (the address of temp) over and over. What you need to do is copy the string into the array.
Also, you need to terminate the string temp with '\0'. You handle this in all but the first string with memset(temp, '\0', i); However, since local variables are not initialized, you need to do it:
char temp[100] = {0}
Or, you can remove the initialization and the memset by just adding the EOS:
temp[i] = '\0';
Lastly, since you declare the original array as
char * ar[10];
You are not allocating any space for the strings. The simplest way to handle that is with strdup.
void int_in_string(char *s, char **ar, int MaxCap)
{
char temp[100];
int index = 0;
int i = 0;
for (; *s; s++) {
if (isdigit(*s)) {
temp[i++] = *s;
// Need to avoid buffer overflow
if (i == sizeof(temp)) {
i = 0;
}
}
if (isspace(*s)) {
temp[i] = '\0';
// strdup will allocate memory for the string, then copy it
ar[index++] = strdup(temp);
// if (NULL == ar[index-1]) TODO: Handle no memory error
i = 0;
if (index == MaxCap) {
break;
}
}
}
if (index == 0) {
printf("no numbers in string");
}
for (int i = 0; i < index; i++) {
printf(" %s \n", ar[i]);
// free the mem from strdup
free(ar[i]);
}
}
I believe some systems may not have strdup(). If not, it can be easily replicated:
char * my_strdup(const char *src)
{
if (src == NULL) return NULL;
char *dest = malloc(strlen(src) + 1);
if (dest == NULL) return NULL;
strcpy(dest, src);
return dest;
}
I want to count the number of words that are in a file. I store each line of the text in the file using a double pointer and then manipulate it do other things.
char **create2DArray()
{
int i = 0;
char **str = malloc(sizeof(char *) * 100);
for (i = 0; i < 100; i++)
{
str[i] = malloc(sizeof(char) * 1000);
}
return str;
}
char **readFile(char **str)
{
int i = 0;
FILE *pFile;
char *filename = "C:\\Users\\muham\\OneDrive\\Documents\\A2\\A2 Samples\\sample1.txt";
pFile = fopen(filename, "r");
if (pFile == NULL)
{
printf("Could not open file");
exit(1);
}
while (fgets(str[i], 1000, pFile) != NULL)
{
RemoveReturn(str[i]);
lineCount++;
printf("%s\n", str[i]);
i++;
}
fclose(pFile);
return str;
}
int wordCount(char **str)
{
int wordCounting = 0;
int i = 0;
int q = 0;
for (i = 0; i < lineCount; i++)
{
for (q = 0; q <= strlen(str[i]); q++)
{
if (*str[q] == ' ' || *str[q] == '\0')
{
wordCounting++;
}
if (*str[q] == ' ' && *str[q + 1] == ' ' && *str[0] != ' ')
{
wordCounting--;
}
if (*str[0] == ' ')
{
wordCounting--;
}
if (*str[q] == ' ' && *str[q + 1] == '\0')
{
wordCounting--;
}
if (strlen(str[q]) == 0)
{
wordCounting--;
}
}
}
printf("%d\n", wordCounting);
return wordCounting;
}
As of right now, when I run the program, wordCount prints 0. Why is this happening? Is it because I am iterating through the number of pointers with str[i] and not the strings stored in str[i]? How do I fix this?
There are several issues in your code; The most obvious one is probably your loop for (i = 0; i <= strlen(str[i]); i++), in which you compare the length of the ith string with the value of i, and you use the same i then to access the characters of the ith string. This all rarely makes sense.
I'd start with two things:
First, make sure that you do not access uninitialized rows, i.e. consider lineCount. A simple way would be to make it either a global variable or to return it in readFile; signature would change to int readFile(char **str) { ....; return lineCount; }
Second, use two nested loops:
for (int line=0; line<lineCount; line++) {
for (int column=0; column < strlen(str[line]); column++) {
// your code for detecting lines goes here...
}
}
If I have a a data structure like a matrix or a tree, and I want to factor out a for loop from a very large function where the above variable is included, how should the call look like? I tried the following but I get a segmentation fault.
void write_command(int w, char *argv[], char *string[]) {
char *dest;
for (int r = 0; argv[r] != NULL; r++) {
dest = malloc(sizeof(char *) * strlen(argv[r]) + 1);
*dest = '0';
strcpy(dest, argv[r]);
string[w][r] = *dest;
free(dest);
}
}
I think you see what I'm trying to do but how should I declare the variables? I get segfault at string[w][r] = *dest;.
I don't think you want to see what I'm refactoring but it is the biggest and most unreadable function ever.
static int runCmd(const char *cmd) {
const char *cp;
pid_t pid;
int status;
struct command structcommand[15];
char **argv = 0;
int argc = 1;
bool pipe = false;
char *string[z][z];
char *pString3[40];
char *pString2[40];
int n = 0;
char **ptr1;
char string1[z];
bool keep = false;
char *pString1[z];
char *pString[z];
*pString1 = "\0";
*pString = "\0";
char *temp = {'\0'};
int w = 0;
bool b = false;
int j = 0;
int i;
int p = 0;
char **ptr;
char *tmpchar;
char *cmdtmp;
bool b1 = false;
char *dest;
int y = 0;
i = 0;
int h = 0;
nullterminate(string);
if (cmd) {
for (cp = cmd; *cp; cp++) {
if ((*cp >= 'a') && (*cp <= 'z')) {
continue;
}
if ((*cp >= 'A') && (*cp <= 'Z')) {
continue;
}
if (isDecimal(*cp)) {
continue;
}
if (isBlank(*cp)) {
continue;
}
if ((*cp == '.') || (*cp == '/') || (*cp == '-') ||
(*cp == '+') || (*cp == '=') || (*cp == '_') ||
(*cp == ':') || (*cp == ',') || (*cp == '\'') ||
(*cp == '"')) {
continue;
}
}
}
if (cmd) {
cmdtmp = malloc(sizeof(char *) * strlen(cmd) + 1);
strcpy(cmdtmp, cmd);
tmpchar = malloc(sizeof(char *) * strlen(cmd) + 1);
if (tmpchar == NULL) {
printf("Error allocating memory!\n"); /* print an error message */
return 1; /* return with failure */
}
strcpy(tmpchar, cmd);
ptr1 = str_split(pString3, cmdtmp, '|');
if (strstr(cmd, "|") == NULL) { /* not a pipeline */
makeArgs(cmd, &argc, (const char ***) &argv, pipe, 0, 0);
for (j = 0; j < argc; j++) {
string[0][j] = argv[j];
structcommand[i].argv = string[0]; /*process;*/
}
n++;
}
else {
for (i = 0; *(ptr1 + i); i++) { /* tokenize the input string for each pipeline*/
n++; /* save number of pipelines */
int e = 0; /* a counter */
*pString = "\0"; /* should malloc and free this? */
strcpy(string1, *(ptr1 + i));
if ((string1[0] != '\0') && !isspace(string1[0])) { /* this is neither the end nor a new argument */
ptr = str_split(pString2, *(&string1), ' '); /* split the string at the arguments */
h = 0;
for (j = 0; *(ptr + j); j++) { /* step through the arguments */
/* the pipeline is in cmdtmp and the argument/program is in ptr[i] */
if (ptr + j && !b && strstr(*(ptr + j), "'")) {
b = true;
strcpy(temp, *(ptr + j));
if (y < 1) {
y++;
}
}
while (b) {
if (*(ptr + j) && strstr(*(ptr + j), "'")) { /* end of quote */
b = false;
if (y < 1) {
string[i][j] = strcpy(temp, *(ptr + j));
}
y = 0;
}
else if (*(ptr + j)) { /* read until end of quote */
string[i][j] = temp;
continue;
} else {
b = false;
break;
}
}
if (ptr + j) {
if (*(ptr + j)[0] == '{') {
keep = true;
}
if (testFn(*(ptr + j))) { /* test for last char */
string[i][j - p] = concat(*pString1, *(ptr + j));
keep = false;
free(*pString1);
goto mylabel;
}
if (keep) {
*pString1 = concat(*pString1, *(ptr + j));
*pString1 = concat(*pString1, " ");
p++;
} else {
// strcpy(temp, *(ptr + j));
b1 = false;
int q = j;
for (e = 0; *(ptr + q + e); e++) { /* step through the string */
b1 = true;
if (*(ptr + e + q)) {
*pString = concat(*pString, *(ptr + e + q));
*pString = concat(*pString, " ");
}
j = e;
}
if (makeArgs(*pString, &argc, (const char ***) &argv, pipe, i, h)) {
write_command(&w, argv, string[w]);
/*for (int r = 0; argv[r] != NULL; r++) {
dest = malloc(sizeof(char *) * strlen(argv[r]) + 1);
*dest = '0';
strcpy(dest, argv[r]);
string[w][r] = dest;
}*/
w++;
} else {
if (!b1) { /* no args (?) */
for (int r = 0; argv[r] != NULL; r++) {
string[i][r] = argv[r];
}
}
}
}
}
}
mylabel:
free(ptr);
dump_argv((const char *) "d", argc, argv);
}
}
free(ptr1);
free(cmdtmp);
free(tmpchar);
}
for (i = 0; i < n; i++) {
for (j = 0; DEBUG && string[i][j] != NULL; j++) {
if (i == 0 && j == 0) printf("\n");
printf("p[%d][%d] %s\n", i, j, string[i][j]);
}
structcommand[i].argv = string[i];
}
fflush(NULL);
pid = fork();
if (pid < 0) {
perror("fork failed");
return -1;
}
/* If we are the child process, then go execute the string.*/
if (pid == 0) {
/* spawn(cmd);*/
fork_pipes(n, structcommand);
}
/*
* We are the parent process.
* Wait for the child to complete.
*/
status = 0;
while (((pid = waitpid(pid, &status, 0)) < 0) && (errno == EINTR));
if (pid < 0) {
fprintf(stderr, "Error from waitpid: %s", strerror(errno));
return -1;
}
if (WIFSIGNALED(status)) {
fprintf(stderr, "pid %ld: killed by signal %d\n",
(long) pid, WTERMSIG(status));
return -1;
}
}
return WEXITSTATUS(status);
}
I'm going to suppose that you are trying to make a deep copy of the argv array, with that being a NULL-terminated array of strings such as the second parameter of a C program's main() function. The function you present seems to assume that you have already allocated space for the destination array itself; its job seems limited to copying the argument strings.
First things first, then: let's look at the caller. If you're making a deep copy of a standard argument vector, then the type of the destination variable should be compatible with the type of argv itself (in the colloquial sense of "compatible"). If the lifetime of the copy does not need to extend past the host function's return, then a variable-length array would be a fine choice:
char *copy[argc + 1];
That relieves you of manually managing the memory of the array itself, but not of managing any memory uniquely allocated to its elements. On the other hand, if you need the copy to survive return from the function in which it is declared, then you'll have to use manual allocation:
char **copy = malloc((argc + 1) * sizeof(*copy));
if (!copy) /* handle allocation failure */ ;
Either way, you can pass the resulting array or pointer itself to your write_command() function, and the required parameter type is the same. It is pointless to pass a pointer to copy, because that function will not modify the pointer it receives as its argument; rather, it will modify the memory to which it points.
Here is the signature of the function you seem to want:
void write_command(char *argv[], char *string[]) {
Given such a signature, you would call it as ...
write_command(argv, copy);
....
The key step you seem to want to perform in the loop inside is
string[r] = strdup(argv[r]);
You can accomplish the same thing with a malloc(), initialize, strcpy() sequence, but that's a bit silly when stdrup() is ready-made for the same task. Do not forget to check its return value, however, (or in your original code, the return value of malloc()) in case memory allocation fails. Any way around, you must not free the allocated memory within write_command(), because that leaves you with invalid pointers inside your copied array.
Furthermore, even if you really do have a 2D array of char * in the caller, such as ...
char *copies[n][argc + 1];
... nothing changes with function write_command(). It doesn't need to know or care whether the array it's copying into is an element of a 2D array. You simply have to call it appropriately, something like:
write_command(argv, copies[w]);
No matter what, you must be sure to free the copied argument strings, but only after you no longer need them. Again, you cannot do that inside the write_command() function.
I use the following code to load a large hashtable on the heap.
However i dont know the right syntax to search the whole array after loading.
I suppose i can add a strcmp in the last J loop??
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int lines_allocated = 128;
int max_line_len = 100;
/* Allocate lines of text */
char **words = (char **)malloc(sizeof(char*)*lines_allocated);
if (words==NULL)
{
fprintf(stderr,"Out of memory (1).\n");
exit(1);
}
FILE *fp = fopen("hashtable.txt", "r");
if (fp == NULL)
{
fprintf(stderr,"Error opening file.\n");
exit(2);
}
int i;
for (i = 0; 1; i++)
{
int j;
/* Have we gone over our line allocation? */
if (i >= lines_allocated)
{
int new_size;
/* Double our allocation and re-allocate */
new_size = lines_allocated*2;
words = (char **)realloc(words,sizeof(char*)*new_size);
if (words == NULL)
{
fprintf(stderr,"Out of memory.\n");
exit(3);
}
lines_allocated = new_size;
}
/* Allocate space for the next line */
words[i] = malloc(max_line_len);
if (words[i] == NULL)
{
fprintf(stderr,"Out of memory (3).\n");
exit(4);
}
if (fgets(words[i], max_line_len-1,fp) == NULL)
break;
/* Get rid of CR or LF at end of line */
for (j = strlen(words[i]) - 1; j >= 0 && (words[i][j] == '\n' || words[i][j] == '\r')j--);
words[i][j] = '\0';
}
int j;
for(j = 0; j < i; j++)
printf("%s\n", words[j]);
// Search for a string e.g "ffffffffff999999999922222222227777777777" in words[]
//
//strcmp ( string, words[j])????
//
//
//
/* Good practice to free memory */
for (;i>=0;i--)
free(words[i]);
free(words);
return 0;
}
i have tried to implement strcmp in the loop but then the program segfaults.
Used this example :
/* what is i? the number of items used in the array? */
for(x = 0; x < i; x++) {
if ( strcmp( new_name, names[x] ) == 0 ){
/* match, x is the index */
return x;
}
}
/* here with no match */
return -1;
As i was indenting your code i saw:
for (j = strlen(words[i]) - 1; j >= 0 && (words[i][j] == '\n' || words[i][j] == '\r')j--);
I think you meant:
for (j = strlen(words[i]) - 1; j >= 0 && (words[i][j] == '\n' || words[i][j] == '\r'); j--)
----^^^^^^^
That while would never execute what it had between braces.
You have a problem here:
for (j=strlen(words[i]) - 1; j>=0 && (words[i][j]=='\n' || words[i][j]=='\r'); j--);
words[i][j]='\0';
j is off by one. You shoud increment jjust after the loop:
for (j=strlen(words[i])-1; j>=0 && (words[i][j]=='\n' || words[i][j]=='\r'); j--)
{
}
words[i][j + 1] = '\0';
The extra { } is there only for readibility purposes. Otherwise if you forget the ; after the for, your code will compile correctly but with words[i][j +1 ]='\0'; being part of the loop.
Other off by one problem:
You must decrement i here:
/* Good practice to free memory */
i-- ; // <<<< decrement i here
for (;i>=0;i--)
free(words[i]);
Problem with strcmp:
Concerning your strcmp problem you probably want this:
int j;
for(j = 0; j < i; j++)
{
printf("%s\n", words[j]);
if (strcmp (words[j], "word we are lookong for") == 0)
{
// found it
}
}