array of strings printing wrong values - c

I trying to make a program that calculates proper prefixes and proper suffixes , and then compare the sets , and then return the array containing values representing number of matched
pairs , This could be used later in KMP algorithm .
But the problem is prefixes and suffixes array give wrong values .
Even after appending a new element at new index, it replaces the all value in the array with the new element .
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int* lps(char *,int );
int main()
{
char *pat = "abababca";
int *ptr ;
ptr = lps(pat,strlen(pat));
printf("\n***LPS***\n");
}
int * lps (char *p,int s)
{
char *prefixes[s] ;
char *suffixes[s] ;
char tmp1[s] , tmp2[s];
int i , j , k , c1 , c2 , c;
for (i = 0 ; i < s ; i ++)
{
printf("\n\n*** --- Creating Prefixes and Suffixes for i = %d --- ***",i);
c1 = 0 ;
//create prefixes
for (j = 0 ; j < i; j++)
{
for (k =0 ; k <= j; k++)
{
tmp1[k]=*(p+k);
printf("\n *(p+%d)= %c , tmp1[%d]=%c",k,*(p+k),k,tmp1[k]);
}
tmp1[k]='\0';
printf("\nprefixes[0]:%s",prefixes[0]);
prefixes[c1] = tmp1;
//strcpy(prefixes[c1], tmp1);
printf("\ncurrently added %s to prefixes at %d and prefixes[%d]= %s\n ",tmp1,c1,c1,prefixes[c1]);
c1++;
}
//print prefixes
for (k = 0; k<c1; k++)
{
printf("\tprefixes[%d] = %s",k,prefixes[k]);
}
printf("\n");
//create suffixes
c2 = 0;
for (j = 1 ; j <= i; j++)
{
for (k = j ; k <= i; k++)
{
tmp2[k-j] = *((p+k));
printf("\n *(p+%d)= %c , tmp2[%d]=%c",k,*(p+k),k-j,tmp2[k-j]);
}
tmp2[k-j]='\0';
suffixes[c2] = tmp2 ;
// strcpy(suffixes[c2], tmp2);
printf("\ncurrently added %s to suffixes at %d and suffixes[%d]= %s\n",tmp2,c2,c2,suffixes[c2]);
c2++;
}
//prinf suffixes
for (k = 0; k<c2; k++)
{
printf("\tsuffixes[%d] = %s",k,suffixes[k]);
}
printf("\n");
//compare the prefixes and suffixes
c = 0 ;
for (j = 0; j < c1; j++)
{
for(k=0 ; k < c2 ; k++)
{
printf("\nprefixes[%d] = %s , suffixes[%d] = %s\n ",j,prefixes[j],k,suffixes[k]);
if (strcmp(prefixes[j], suffixes[k])==0)
{
c = c + 1 ;
}
}
}
}
}
OUTPUT (Some part of OUTPUT) :-
prefixes[0] = ab prefixes[1] = ab //it should be prefixes[0] = a prefixes[1] = ab

The problem is that you aren't allocating any strings. The only strings that you have in lps are tmp1 and tmp2. You then make assignments like this:
prefixes[c1] = tmp1;
That assigns a pointer but does not copy the contents of the string. You'll end up with every entry in prefixes pointing to the same string, tmp1. And likewise for suffixes.
You'll need to use malloc and strcpy to make new string instances.
In the code you have commented out calls to strcpy. I suspect you tried these and encountered runtime errors. Those runtime errors were because you did not allocate any memory. The corrected code would look like:
prefixes[c1] = malloc(strlen(tmp1)+1);
strcpy(prefixes[c1], tmp1);
And similarly for suffixes.
In production quality code you'd include error checking. And you'd want to make sure that you call free() on any pointer returned by the calls malloc(), once you had finished with it.
I'd also question the use of C variable length arrays, VLAs. In your code, prefixes, suffixes, tmp1 and tmp2 are all VLAs. Using VLAs can readily lead to stack overflow if you have large array dimensions. My instincts say that heap allocation is what you need here.

Related

Inconsistent output given by same code on different C compilers

Different compilers are giving different outputs for the same logic in my algorithm.
I wrote the following code for a C code exercise.
The code checks for the longest string in a string vector.
But the same logic gives two different outputs.
Here's what is happening. I have no idea what I did wrong.
First version - without a printf() inside the if condition
Here the if (j > longest) just attributes new values for int longest and int index.
#include <stdio.h>
int main(void) {
char *vs[] = {"jfd", "kj", "usjkfhcs", "nbxh", "yt", "muoi", "x", "rexhd"};
int longest, index = 0;
/* i is the index for elements in *vs[].
* "jfd" is 0, "kj" is 1... */
for (int i = 0; i < sizeof(*vs); i++) {
/* j if the index for string lengths in vs[].
* for "jfd", 'j' is 0, 'f' is 1... */
for (int j = 0; vs[i][j] != '\0'; j++) {
/* if j is longer than the previous longest value */
if (j > longest) {
longest = j;
index = i;
}
}
}
printf("Longest string = %s\n", vs[index]);
return 0;
}
I ran it on https://replit.com/. It gave the unexpected output for longest string of "jfd". https://replit.com/#Pedro-Augusto33/Whatafuck-without-printf?v=1
Second version - with a printf() inside the if condition
Now I just inserted a printf() inside the if (jf > longest) condition, as seen in the code block bellow.
It changed the output of my algorithm. I have no idea how or why.
#include <stdio.h>
int main(void) {
char *vs[] = {"jfd", "kj", "usjkfhcs", "nbxh", "yt", "muoi", "x", "rexhd"};
int longest, index = 0;
/* i is the index for elements in *vs[].
* "jfd" is 0, "kj" is 1... */
for (int i = 0; i < sizeof(*vs); i++) {
/* j if the index for string lengths in vs[].
* for "jfd", 'j' is 0, 'f' is 1... */
for (int j = 0; vs[i][j] != '\0'; j++) {
/* if j is longer than the previous longest value */
if (j > longest) {
printf("Whatafuck\n");
longest = j;
index = i;
}
}
}
printf("Longest string = %s\n", vs[index]);
return 0;
}
I also ran it on https://replit.com/. It gave the expected output for longest string of "usjkfhcs". https://replit.com/#Pedro-Augusto33/Whatafuck-with-printf?v=1
Trying new compilers
After replit.com giving two different outputs, I tried another compiler to check if it also behaved strangely. https://www.onlinegdb.com/online_c_compiler gives random outputs. Sometimes it's "jfd", sometimes it's "usjkfhcs". https://onlinegdb.com/iXoCDDena
Then I went to https://www.programiz.com/c-programming/online-compiler/ . It always gives the expected output of "usjkfhcs".
So, my question is: why are different compilers behaving so strangely with my algorithm? Where is the flaw of my algorithm that makes the compilers interpret it different?
The code does not make sense.
For starters the variable longest was not initialized
int longest, index = 0;
So using it for example in this statement
if (j > longest) {
invokes undefined behavior.
In this for loop
for (int i = 0; i < sizeof(*vs); i++) {
the expression sizeof( *vs ) is equivalent to expression sizeof( char * ) and yields either 4 or 8 depending on the used system. It just occurred such a way that the array was initialized with 8 initializers. But in any case the expression sizeof( *vs ) does not provide the number of elements in an array and its value does not depend on the actual number of elements.
Using the if statement within the for loop in each iteration of the loop
for (int j = 0; vs[i][j] != '\0'; j++) {
/* if j is longer than the previous longest value */
if (j > longest) {
longest = j;
index = i;
}
}
Also does not make sense. It does not calculate the exact length of a string that is equal to j after the last iteration of the loop. So in general such a loop shall not be used for calculating length of a string.
Consider a string for example like "A". Using this for loop you will get that its length is equal to 0 while its length is equal to 1..
It seems you are trying to find the longest string a pointer to which stored in the array.
You could just use standard C string function strlen declared in header <string.h>. If to use your approach with for loops then the code can look the following way
#include <stdio.h>
int main(void)
{
const char *vs[] = { "jfd", "kj", "usjkfhcs", "nbxh", "yt", "muoi", "x", "rexhd" };
const size_t N = sizeof( vs ) / sizeof( *vs );
size_t longest = 0, index = 0;
for ( size_t i = 0; i < N; i++ )
{
size_t j = 0;
while ( vs[i][j] != '\0' ) ++j;
if ( longest < j )
{
longest = j;
index = i;
}
}
printf( "Longest string = %s\n", vs[index] );
printf( "Its length = %zu\n", longest );
return 0;
}

Segmentation fault with concatenation of two strings

I'm trying to build alphanumeric strings consisting of 3 initial letters and 3 final numbers and save them in a .txt file. I wrote this:
int i = 0,
j = 0;
char name_cpu[8],
array_numbers_final[8],
array_letters[27] = "ABCDEFGHIJKLMNOPQRSTUWXYVZ",
array_numbers[10] = "123456789";
/* Generator data */
for(i = 0; i < number_cpu; i++)
{
for(j = 0; j < 3; j++){
name_cpu[j] = array_letters[rand() % (sizeof(array_letters)-1)];
array_numbers_final[j] = array_numbers[rand() % (sizeof(array_numbers)-1)];
}
strcat(name_cpu, array_numbers_final);
fprintf(list_cpu, "%s \n", name_cpu);
}
The problem is that at the first external for loop it correctly prints a string of the form "AAA000". At the second for loop it goes in segmentation fault. Can anyone tell me what and where I am doing wrong?
EDIT:
A minimal reproducible example is:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void){
FILE *list_cpu = NULL;
int i = 0,
number_cpu = 3,
j = 0;
char name_cpu[8] = {0},
array_numbers_final[8] = {0},
array_letters[27] = "ABCDEFGHIJKLMNOPQRSTUWXYVZ",
array_numbers[10] = "123456789";
list_cpu = fopen("list_cpu.txt", "w");
/* Generator data */
for(i = 0; i < number_cpu; i++)
{
for(j = 0; j < 3; j++){
name_cpu[j] = array_letters[rand() % (sizeof(array_letters)-1)];
array_numbers_final[j] = array_numbers[rand() % (sizeof(array_numbers)-1)];
}
strcat(name_cpu, array_numbers_final);
fprintf(list_cpu, "%s \n", name_cpu);
}
fclose(list_cpu);
return(0);
}
If number_cpu is equal to 1 it works. But if it is higher then 1, the program goes into segmentation fault.
answer to new edits to OP
Edits to OP include initialization of variables addressing some instances of undefined behavior in original code, however buffer overflow problem remains in following section:
/* Generator data */
for(i = 0; i < number_cpu; i++)
{
for(j = 0; j < 3; j++){
name_cpu[j] = array_letters[rand() % (sizeof(array_letters)-1)];
array_numbers_final[j] = array_numbers[rand() % (sizeof(array_numbers)-1)];
}
strcat(name_cpu, array_numbers_final);
fprintf(list_cpu, "%s \n", name_cpu);
}
fclose(list_cpu);
Where name_cpu is defined with 8 char: char name_cpu[8] = {0}
"If number_cpu is equal to 1 it works. But if it is higher then 1, the program goes into segmentation fault."...
After first iteration of outer loop, including one strcat of name_cpu and list_cpu, the char name_cpu is populated with 6 new characters and \0 in remaining locations. Eg:
//example
|S|J|P|Y|E|P|\0|\0| //
The error occurs at end of 2nd iteration of outer loop at the strcat() statement. The inner loop re-populated the first three positions of name_cpu, eg:
//first three positions changed, the rest remains
|C|B|A|Y|E|P|\0|\0| //
^ ^ ^ updated values
Error occurs when the new value for array_numbers_final :
|G|H|O|\0|\0|\0|\0|\0|
is attempted to be concatenated to name_cpu, resulting in:
|C|B|A|Y|E|P|G|H|O| //attempt to write to memory not owned
^end of buffer
Resulting in buffer overflow error, and likely the segmentation fault error condition.
Again, as before, design variables to meet the potential needs of of the program. In this instance the following will work:
char name_cpu[100] = {0};
char array_numbers_final[100] = {0};
answer to original post:
The problem is here:
strcat(name_cpu, array_numbers_final);//undefined behavior.
First, name_cpu[8] is uninitialized at time of declaration, nor is it initialized at any time before used. Because it is not guaranteed what the contents are, it is not necessarily a string. Using this variable as is in any string function can invoke undefined behavior
Furthermore,(even if name_cpu is a valid string) when array_numbers_final is concatenated to name_cpu, the buffer will overflow. During my test run I saw this:
The fix is to start off with initialized buffers of sufficient size for intended purpose. eg:
char name_cpu[100] = {0}
array_numbers_final[100] = {0},
array_letters[27] = "ABCDEFGHIJKLMNOPQRSTUWXYVZ",
array_numbers[10] = "123456789";
Code example below is adaptation using your provided example, with suggested edits. Read in-line comments:
int main(void)
{
int number_cpu = 3;//added - previously not defined in OP
int i = 0,
j = 0;
char name_cpu[100] = {0},//resized and initialize
array_numbers_final[100] = {0},//resized and initialize
array_letters[27] = "ABCDEFGHIJKLMNOPQRSTUWXYVZ",
array_numbers[10] = "123456789";
/* Generator data */
for(i = 0; i < number_cpu; i++)
{
for(j = 0; j < 3; j++){
name_cpu[j] = array_letters[rand() % (sizeof(array_letters)-1)];
array_numbers_final[j] = array_numbers[rand() % (sizeof(array_numbers)-1)];
}
strcat(name_cpu, array_numbers_final);
fprintf(stdout, "%s \n", name_cpu);//to stdout
// ^ changed here
}
return 0;
}

Exception thrown at 0x7C131F4C (ucrtbased.dll) in ICP LAB ASSIGNMENT PROJECT.exe: 0xC0000005

I was trying to print some array but it won't print no matter what.
Which part did I do wrong?
Is it the array?
int main()
{
int i;
char id[3]; ///sample data wanted to print
id[0] = 'id1';
id[1] = 'id2';
id[2] = 'id3';
for (i = 1; i <= 3; ++i)
{
printf("%s", id[i]); ///The error appeared here////
}
}
i starts at 1, and goes to 3:
for (i = 1; i <= 3; ++i)
But you set up your array so that valid indicies are 0, 1, and 2.
3 is not a valid index.
Convention C-loops always look like this:
for(i = 0; i < 3; ++i)
That is, they start at 0 and go while less than the size of the array.
Not less than or equal to. That is your mistake.
Next, each element of the array is a single character.
But you are trying to initialize them with 3-letters, such as: id1.
A single character can hold ONE LETTER ONLY, not a set of 3 letters.
You are trying to print them out using %s; but %s is for strings, not single characters.
Here is a corrected version of your program.
int main()
{
int i;
char* id[3]; // Declare strings, not characters.
id[0] = "id1"; // Initialize each with a string
id[1] = "id2";
id[2] = "id3";
for (i = 0; i < 3; ++i) // Set loop limit correctly.
{
printf("%s\n", id[i]);
}
}
You invoked undefined behavior by passing data having wrong type: %s expects an pointer to a null-terminated string while you passed id[i], whose type is char (expanded to int here).
You can use %c to display the values of implementation-defined values of multi-character character literals.
Also The loop range is wrong as #abelenky says.
#include <stdio.h>
int main()
{
int i;
char id[3]; ///sample data wanted to print
id[0] = 'id1';
id[1] = 'id2';
id[2] = 'id3';
for (i = 0; i < 3; ++i)
{
printf("%c", id[i]);
}
}
Or do you mean this?
#include <stdio.h>
int main()
{
int i;
const char* id[3]; ///sample data wanted to print
id[0] = "id1";
id[1] = "id2";
id[2] = "id3";
for (i = 0; i < 3; ++i)
{
printf("%s\n", id[i]);
}
}

realloc() seems to affect already allocated memory

I am experiencing an issue where the invocation of realloc seems to modify the contents of another string, keyfile.
It's supposed to run through a null-terminated char* (keyfile), which contains just above 500 characters. The problem, however, is that the reallocation I perform in the while-loop seems to modify the contents of the keyfile.
I tried removing the dynamic reallocation with realloc and instead initialize the pointers in the for-loop with a size of 200*sizeof(int) instead. The problem remains, the keyfile string is modified during the (re)allocation of memory, and I have no idea why. I have confirmed this by printing the keyfile-string before and after both the malloc and realloc statements.
Note: The keyfile only contains the characters a-z, no digits, spaces, linebreaks or uppercase. Only a text of 26, lowercase letters.
int **getCharMap(const char *keyfile) {
char *alphabet = "abcdefghijklmnopqrstuvwxyz";
int **charmap = malloc(26*sizeof(int));
for (int i = 0; i < 26; i++) {
charmap[(int) alphabet[i]] = malloc(sizeof(int));
charmap[(int) alphabet[i]][0] = 0; // place a counter at index 0
}
int letter;
int count = 0;
unsigned char c = keyfile[count];
while (c != '\0') {
int arr_count = charmap[c][0];
arr_count++;
charmap[c] = realloc(charmap[c], (arr_count+1)*sizeof(int));
charmap[c][0] = arr_count;
charmap[c][arr_count] = count;
c = keyfile[++count];
}
// Just inspecting the results for debugging
printf("\nCHARMAP\n");
for (int i = 0; i < 26; i++) {
letter = (int) alphabet[i];
printf("%c: ", (char) letter);
int count = charmap[letter][0];
printf("%d", charmap[letter][0]);
if (count > 0) {
for (int j = 1; j < count+1; j++) {
printf(",%d", charmap[letter][j]);
}
}
printf("\n");
}
exit(0);
return charmap;
}
charmap[(int) alphabet[i]] = malloc(sizeof(int));
charmap[(int) alphabet[i]][0] = 0; // place a counter at index 0
You are writing beyond the end of your charmap array. So, you are invoking undefined behaviour and it's not surprising that you are seeing weird effects.
You are using the character codes as an index into the array, but they do not start at 0! They start at whatever the ASCII code for a is.
You should use alphabet[i] - 'a' as your array index.
The following piece of code is a source of troubles:
int **charmap = malloc(26*sizeof(int));
for (int i = 0; i < 26; i++)
charmap[...] = ...;
If sizeof(int) < sizeof(int*), then it will be performing illegal memory access operations.
For example, on 64-bit platforms, the case is usually sizeof(int) == 4 < 8 == sizeof(int*).
Under that scenario, by writing into charmap[13...25], you will be accessing unallocated memory.
Change this:
int **charmap = malloc(26*sizeof(int));
To this:
int **charmap = malloc(26*sizeof(int*));

What's wrong in this C program..?

struct bucket
{
int nStrings; //No. of Strings in a Bucket.
char strings[MAXSTRINGS][MAXWORDLENGTH]; // A bucket row can contain maximum 9 strings of max string length 10.
};//buck[TOTBUCKETS];
void lexSorting(char array[][10], int lenArray, int symb) //symb - symbol, sorting based on character symbols.
{
int i, j;
int bucketNo;
int tBuckNStrings;
bucket buck[TOTBUCKETS];
for(i=0; i<lenArray; i++)
{
bucketNo = array[i][symb] - 'a'; // Find Bucket No. in which the string is to be placed.
tBuckNStrings = buck[bucketNo].nStrings; // temp variable for storing nStrings var in bucket structure.
strcpy(buck[bucketNo].strings[tBuckNStrings],array[i]); // Store the string in its bucket.
buck[bucketNo].nStrings = ++tBuckNStrings; //Increment the nStrings value of the bucket.
}
// lexSorting(array, lenArray, ++symb);
printf("****** %d ******\n", symb);
for(i=0; i<TOTBUCKETS; i++)
{
printf("%c = ", i+'a');
for(j=0; j<buck[i].nStrings; j++)
printf("%s ",buck[i].strings[j]);
printf("\n");
}
}
int main()
{
char array[][10] = {"able","aback","a","abet","acid","yawn","yard","yarn","year","yoke"};
int lenArray = 10;
int i;
printf("Strings: ");
for(i=0; i<lenArray; i++)
printf("%s ",array[i]);
printf("\n");
lexSorting(array, lenArray, 0);
}
Well here is the complete code, that I am trying. since its been a long time since i have touched upon C programming, so somewhere i am making mistake in structure declaration.
The problem goes here:-
1) I have declared a structure above and its object as array(buck[]).
2) Now when I declare this object array along with the structure, it works fine.. I have commented this thing right now.
3) But when I declare this object array inside the function.. because ultimately i have to declare inside function( as i need to build a recursive program, where objects will be created in very recursive call) then the program is throwing segmentation fault.
Expected Output
> [others#centos htdocs]$ ./a.out
> Strings: able aback a abet acid yawn
> yard yarn year yoke
> ****** 0 ******
> a = able aback a abet acid
> b =
> c
> .
> .
> y = yawn yard yarnyear yoke
> z =
Actual Output
[others#centos htdocs]$ ./a.out
Strings: able aback a abet acid yawn yard yarn year yoke
Segmentation fault
I have no idea, what difference I made in this. Kindly help.
Thanks.
What's wrong with your program is that it doesn't contain a main() function hence it won't link.
Beyond that, you should always do the following when asking questions here:
Provide a complete, minimal code sample that demonstrates the problem.
Detail the expected behaviour.
Detail the actual behaviour.
In fact, when I add the line:
int main (void) { return 0; }
it compiles and links fine.
That means it's almost certainly a run-time error you're experiencing hence we need the main() to figure out what you're doing wrong.
Using my psychic debugging skills, an important difference between declaring it at file scope and block scope is that the file-scope version will be initialised to zeros.
That means all the structure fields will be effectively zero (for the count) and empty strings (for the strings). With block scope, those counts and strings will be uninitialised.
The fact that you're using TOBUCKETS to print the structure out probably means you're trying to print out one of those uninitialised strings.
I think what's probably happening is that the nStrings field contains a garbage value when you start the processing. You should probably initialise it to zero manually (with a loop) and see if that fixes your problem. Put this after the declaration of buck in your sort function:
for (i = 0; i < TOTBUCKETS; i++)
buck[i].nStrings = 0;
Right. It turns out that was the problem. When I fix up the errors in your latest code, I get the segmentation violation as well but, when I add that section above, it works fine:
#include <stdio.h>
#include <string.h>
#define MAXSTRINGS 9
#define MAXWORDLENGTH 10
#define TOTBUCKETS 26
struct bucket
{
int nStrings;
char strings[MAXSTRINGS][MAXWORDLENGTH];
};
void lexSorting(char array[][10], int lenArray, int symb)
{
int i, j;
int bucketNo;
int tBuckNStrings;
struct bucket buck[TOTBUCKETS];
for(i=0; i<TOTBUCKETS; i++) buck[i].nStrings = 0;
for(i=0; i<lenArray; i++)
{
bucketNo = array[i][symb] - 'a';
tBuckNStrings = buck[bucketNo].nStrings;
strcpy(buck[bucketNo].strings[tBuckNStrings],array[i]);
buck[bucketNo].nStrings = ++tBuckNStrings;
}
printf("****** %d ******\n", symb);
for(i=0; i<TOTBUCKETS; i++)
{
printf("%c = ", i+'a');
for(j=0; j<buck[i].nStrings; j++)
printf("%s ",buck[i].strings[j]);
printf("\n");
}
}
int main()
{
char array[][10] = {"able","aback","a","abet","acid",
"yawn","yard","yarn","year","yoke"};
int lenArray = 10;
int i;
printf("Strings: ");
for(i=0; i<lenArray; i++)
printf("%s ",array[i]);
printf("\n");
lexSorting(array, lenArray, 0);
}
The output of that was:
Strings: able aback a abet acid yawn yard yarn year yoke
****** 0 ******
a = able aback a abet acid
b =
c =
d =
e =
f =
g =
h =
i =
j =
k =
l =
m =
n =
o =
p =
q =
r =
s =
t =
u =
v =
w =
x =
y = yawn yard yarn year yoke
z =
keyword struct is not required when you create objects for it.

Resources