Fill dynamically sized array in C++ and use the values - c

I'd like to fill a char-array dynamically and check whether the contained values are valid integers, here's what I got so far:
for(int i = 0; i < 50000; i++)
{
if(input[i] == ',')
{
commaIndex = i;
}
}
commaIndex is the index of a comma inside a file, numerical values should have been entered before a comma, file looks like this: -44,5,19,-3,13,(etc), it's important for this part:
char *tempNumber = new char[commaIndex];
Fill tempNumber (which should presumably be just as big as the number due to my dynamic allocation) so I don't have a number in a size 50000 char-array (named input).
for(int i = 0; i < commaIndex; i++)
{
cout << i << "\n";
tempNumber[i] = input[i];
}
And now I want to use it:
if(!isValidInteger(tempNumber))
{
cout << "ERROR!\n";
}
Unfortunately, tempNumber always seems to be of size 4 irregardless of the value of "commaIndex", i.e. I get the following output:
(Inputdata: 50000,3,-4)
commaIndex: 5
content of tempNumber: 5000 (one 0 missing)
commaIndex: 1
content of tempNumber: 3²²² (notice the 3 ^2s)
commaIndex: 2
content of tempNumber: -4²²
Any ideas?
One more thing: This is for a homework assignment and I am not allowed to use any object-oriented element of C++ (this includes strings and vectors, I've been there and I know it would be SO easy.)
Thanks,
Dennis

You might be interested by the strtol function.

You may also consider using strtok() with sscanf(). Notice, that strtol() does not allow you to check for errors since it simply returns (perfectly valid) value 0 on parse error. On the other hand, sscanf() returns number of successfully read items, so you may easily check if there was an error while reading a number.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i = 0;
char str[] = "1,2,-3,+4,a6,6";
/* calculate result table size and alloc */
int max = 1;
char* tmp = str;
while (*tmp)
if (*tmp++ == ',')
++max;
int* nums = malloc(sizeof(int) * max);
/* tokenize string by , and extract numbers */
char* pch = strtok(str, ",");
while (pch != NULL) {
if (sscanf(pch, "%d", &nums[i++]) == 0)
printf("Not a number: %s\n", pch);
pch = strtok(NULL, ",");
}
/* print read numbers */
for (i = 0; i < max; ++i)
printf("%d\n", nums[i]);
free(nums);
return 0;
}

Related

Segmentation Fault when My Code Executes the printf() in c

below I have posted my code. When I compile I receive no errors, and only one warning about variables I haven't used yet. the code works all the way to the line in code where it starts to print. I have tested all the sections and I believe that one is at fault. please let me know what I am doing wrong so I can fix it.
#include <stdio.h>
#include <string.h>
#define NUM_LINES 37
#define LINE_LENGTH 60
void select_sort_str(char list[NUM_LINES][LINE_LENGTH], int n);
int alpha_first(char list[NUM_LINES][LINE_LENGTH], int min_sub, int max_sub);
int main (void){
//store each line in an array of strings
FILE *inp;
FILE *outp;
char hurr[NUM_LINES][LINE_LENGTH];
;
inp = fopen("hurricanes.csv","r");
outp = fopen("out.txt","w");
//read in lines from file
for (int i = 0; i<NUM_LINES; i++){
fgets(hurr[i], LINE_LENGTH, inp);
}
inp = fopen("hurricanes.cvs","r");
//printf("%s", hurr[0]);
//define function
select_sort_str(hurr, NUM_LINES);
return(0);
}
int
alpha_first(char list[NUM_LINES][LINE_LENGTH], // input - array of pointers to strings
int min_sub, // input - min and max subscripts of
int max_sub) // portion of list to consider
{
int first, i;
first = min_sub;
for (i = min_sub + 1; i <= max_sub; ++i) {
if (strcmp(list[i], list[first]) < 0) {
first = i;
}
}
return (first);
}
/*
* Orders the pointers in an array list so they access strings in
* alphabetical order
* Pre: first n elements of list reference string of uniform case;
* n >= 0
*/
void
select_sort_str(char list[NUM_LINES][LINE_LENGTH], // input/output - array of pointers being
// ordered to acces strings alphabetically
int n) // input - number of elements to sort
{
int fill, // index of element to contain next string in order
index_of_min; // index of next string in order
char *temp;
char temp1[NUM_LINES][LINE_LENGTH];
for (fill = 0; fill < n - 1; ++fill) {
index_of_min = alpha_first(list, fill, n - 1);
if (index_of_min != fill) {
temp = list[index_of_min];
list[index_of_min][LINE_LENGTH] = list[fill][LINE_LENGTH];
strncpy(temp1[index_of_min], list[index_of_min], LINE_LENGTH);
temp1[fill][LINE_LENGTH] = *temp;
}
}
char *name;
char *cat = 0;
char *date;
for (int i = 0; i<NUM_LINES; i++){
name = strtok(NULL, ",");
cat = strtok(NULL, "h");
date = strtok(NULL, " ");
printf("%s %s %s\n", name, cat, date);
}
// for( int i =0; i<NUM_LINES; i++){
// printf("%s", list[i]);
// }
}
The only first parameter you ever pass to strtok is NULL. You never actually give it anything to parse. Did you perhaps mean strtok(temp1[i], ",");?
Also, why no error checking? It's much easier to find bugs in code with error checking.

HEAP CORRUPTION DETECTED: after normal block(#87)

I'm trying to do a program that get number of names from the user, then it get the names from the user and save them in array in strings. After it, it sort the names in the array by abc and then print the names ordered. The program work good, but the problem is when I try to free the dynamic memory I defined.
Here is the code:
#include <stdio.h>
#include <string.h>
#define STR_LEN 51
void myFgets(char str[], int n);
void sortString(char** arr, int numberOfStrings);
int main(void)
{
int i = 0, numberOfFriends = 0, sizeOfMemory = 0;
char name[STR_LEN] = { 0 };
char** arrOfNames = (char*)malloc(sizeof(int) * sizeOfMemory);
printf("Enter number of friends: ");
scanf("%d", &numberOfFriends);
getchar();
for (i = 0; i < numberOfFriends; i++) // In this loop we save the names into the array.
{
printf("Enter name of friend %d: ", i + 1);
myFgets(name, STR_LEN); // Get the name from the user.
sizeOfMemory += 1;
arrOfNames = (char*)realloc(arrOfNames, sizeof(int) * sizeOfMemory); // Change the size of the memory to more place to pointer from the last time.
arrOfNames[i] = (char*)malloc(sizeof(char) * strlen(name) + 1); // Set dynamic size to the name.
*(arrOfNames[i]) = '\0'; // We remove the string in the currnet name.
strncat(arrOfNames[i], name, strlen(name) + 1); // Then, we save the name of the user into the string.
}
sortString(arrOfNames, numberOfFriends); // We use this function to sort the array.
for (i = 0; i < numberOfFriends; i++)
{
printf("Friend %d: %s\n", i + 1, arrOfNames[i]);
}
for (i = 0; i < numberOfFriends; i++)
{
free(arrOfNames[i]);
}
free(arrOfNames);
getchar();
return 0;
}
/*
Function will perform the fgets command and also remove the newline
that might be at the end of the string - a known issue with fgets.
input: the buffer to read into, the number of chars to read
*/
void myFgets(char str[], int n)
{
fgets(str, n, stdin);
str[strcspn(str, "\n")] = 0;
}
/*In this function we get array of strings and sort the array by abc.
Input: The array and the long.
Output: None*/
void sortString(char** arr, int numberOfStrings)
{
int i = 0, x = 0;
char tmp[STR_LEN] = { 0 };
for (i = 0; i < numberOfStrings; i++) // In this loop we run on all the indexes of the array. From the first string to the last.
{
for (x = i + 1; x < numberOfStrings; x++) // In this loop we run on the next indexes and check if is there smaller string than the currnet.
{
if (strcmp(arr[i], arr[x]) > 0) // If the original string is bigger than the currnet string.
{
strncat(tmp, arr[i], strlen(arr[i])); // Save the original string to temp string.
// Switch between the orginal to the smaller string.
arr[i][0] = '\0';
strncat(arr[i], arr[x], strlen(arr[x]));
arr[x][0] = '\0';
strncat(arr[x], tmp, strlen(tmp));
tmp[0] = '\0';
}
}
}
}
After the print of the names, when I want to free the names and the array, in the first try to free, I get an error of: "HEAP CORRUPTION DETECTED: after normal block(#87)". By the way, I get this error only when I enter 4 or more players. If I enter 3 or less players, the program work properly.
Why does that happen and what I should do to fix it?
First of all remove the unnecessary (and partly wrong) casts of the return value of malloc and realloc. In other words: replace (char*)malloc(... with malloc(..., and the same for realloc.
Then there is a big problem here: realloc(arrOfNames, sizeof(int) * sizeOfMemory) : you want to allocate an array of pointers not an array of int and the size of a pointer may or may not be the same as the size of an int. You need sizeof(char**) or rather the less error prone sizeof(*arrOfNames) here.
Furthermore this in too convoluted (but not actually wrong):
*(arrOfNames[i]) = '\0';
strncat(arrOfNames[i], name, strlen(name) + 1);
instead you can simply use this:
strcpy(arrOfNames[i], name);
Same thing in the sort function.
Keep your code simple.
But actually there are more problems in your sort function. You naively swap the contents of the strings (which by the way is inefficient), but the real problem is that if you copy a longer string, say "Walter" into a shorter one, say "Joe", you'll write beyond the end of the allocated memory for "Joe".
Instead of swapping the content of the strings just swap the pointers.
I suggest you take a pencil and a piece of paper and draw the pointers and the memory they point to.

Writing to char* array in C throws segfault

I'm an infosec student, who has just begun learning C. I've been given an assignment to write a simple translation program using this prototype: char* dictionary[number_of_words][2];.
The output should look something like this:
Enter # of words to add: 3
dictionary[0][0]= plane
dictionary[0][1]= Flugzeug
dictionary[1][0]= house
dictionary[1][1]= Haus
dictionary[2][0]= cat
dictionary[2][1]= Katze
Enter english word to translate: house
---> Haus
My current (work in progress) code looks like this:
void clean_stdin(void);
int main(void)
{
unsigned int i = 0,j = 0,size;
printf("Enter # of words to add: ");
scanf("%u",&size);
clean_stdin(); //clears input buffer
char* dictionary[size][2];
while(i <= size)
{
printf("dictionary[%u][%u]= ",i,j);
fgets(dictionary[i][j],100,stdin);
if(j >= 1)
{
j = 0;
++i;
}
else
j = 1;
}
return 0;
}
The code throws no errors when compiled with gcc -Wall main.c, but this is the behaviour I get:
Enter # of words to add: 3
dictionary[0][0]= plane
dictionary[0][1]= Flugzeug
dictionary[1][0]= house
dictionary[1][1]= Haus
fish: “./a.out” terminated by signal SIGSEGV (Adressbereichsfehler)
*****
Enter # of words to add: 4
dictionary[0][0]= plane
fish: “./a.out” terminated by signal SIGSEGV (Adressbereichsfehler)
There's a fundamental flaw somewhere in my thought process. Any help/heads up is very appreciated. Cheers!
Let's look at this part of your code:
char* dictionary[size][2];
while(i <= size)
{
printf("dictionary[%u][%u]= ",i,j);
fgets(dictionary[i][j],100,stdin);
Each dictionary[i][j] is a pointer to char, but you haven't set them to point anywhere meaningful yet - each array element contains some random bit pattern that may or may not correspond to a writable address. Your first few entries get written somewhere, but eventually you try to write to a memory location you don't own or don't have access to.
You will need to set aside additional memory to store each individual string. You either need create a 3D array of char (not char *):
#define MAX_STR_LEN 100
...
char dictionary[size][2][MAX_STR_LEN+1];
or you will need to dynamically allocate memory for each array entry:
while ( i < size ) // <, not <=
{
dictionary[i][j] = malloc( sizeof *dictionary[i][j] * (MAX_STR_LEN + 1));
if ( !dictionary[i][j] )
{
// memory allocation failed, handle error
}
printf("dictionary[%u][%u]= ",i,j);
fgets(dictionary[i][j],MAX_STR_LEN,stdin);
If you allocate memory dynamically, you will need to explicitly free it when you're done:
for ( i = 0; i < size; i++ )
{
free( dictionary[i][0] );
free( dictionary[i][1] );
}
instead of messing around with arrays of multiple dimensions, I suggest you use structures:
#include <stdio.h>
#include <string.h>
int main()
{
unsigned int size;
printf("Enter # of words to add: ");
if (scanf("%u", &size) != 1)
return 1; // cannot convert input to integer
#define WORD_MAX_SIZE 100
struct {
char word[WORD_MAX_SIZE];
char translation[WORD_MAX_SIZE];
} dictionary[size];
memset(dictionary, 0, sizeof(dictionary));
for (int i = 0; i < size; i++) {
printf("dictionary[%u].word= ", i);
fgets(dictionary[i].word, WORD_MAX_SIZE, stdin);
printf("dictionary[%u].translation= ", i);
fgets(dictionary[i].translation, WORD_MAX_SIZE, stdin);
}
// do something with your stuff
}
fgets(dictionary[i][j],100,stdin); reads from an uninitialized variable (dictionary[i][j] was never given a value), which has undefined behavior.
Also, i is going out of bounds later.

Load numbers from input into array

I have given input which contains data that I am going to process and saved into an array. The input looks like this :
{ [1, 10], [2,1] , [-10, 20] }
it can have more elements in it. I need to process it that I can load all numbers from [ number , number ] into 2d array , first number should be at 0th and second number should be at 1st index so this array should look like
[[1,10],[2,1],[-10,20]]
But I've failed to find the solution, how to process this input into my desired array. What is the right way to do it?
I tried to do as following:
int main()
{
long long int cisla[10][2];
int x;
int y;
int i;
int index=0;
int counter=0;
char c;
char zatvorka_one;
char zatvorka_three;
char ciarka;
char ciarka_two;
printf("Pozicia nepriatela\n");
c=getchar();
if(c!='{'){
return 0;
}
scanf(" %c%d,%d%c",&ciarka,&x,&y,&zatvorka_one);
cisla[index][0]=x;
cisla[index][1]=y;
index++;
while(1){
scanf("%c",&ciarka);
if(ciarka=='}'){
break;
}
scanf(" %c%d%,%d%c",&ciarka,&x,&y,&zatvorka_one);
cisla[index][0]=x;
cisla[index][1]=y;
index++;
}
for ( i = 0; i < index; i++){
printf("%d %d\n",cisla[i][0],cisla[i][1]);
}
}
But somehow it returns unexpected result, how can i fix it?
You should use gets instead of scanf. gets will return the entire string wich will be easear. Then you ahould read about strtok wich can be used to separate a string. For example: strtok(s,",") will separate your string into smaller strings. For the input {[12,4], [8,9]} will divide into: first string: {[12 second string: 4] third string [8 and fourth string 9]}. Now you will just have to remove the characters that are not numbers like { } and []. After that you will have strings only with the numbers so you can use another predefined fuction you should read abput called atoi. It recieves a string and turns it into an int (ascci to int). There is also an atof (ascci to float) if you need it. Tuturialpoints is a good place to look for examples on how to use these functions i mentioned.
I'm relatively new to C and SO. Maybe I shouldn't give you the solution, but I did. It follows the advice of sharp c student.
You could try to do it like this:
#include "stdafx.h"
#include "string.h"
#include "stdlib.h"
#define MAXNBRELM 10 // maximum number of elements; adjust as needed
int main()
{
int IntArr[MAXNBRELM][2]; // integer array to hold results
int s = 0; // subscript
int NbrElm; // number of elements found
char Buf[81]; // buffer to hold input
char * StrPtr; // pointer to string for fgets
char * TknChr; // each individual token
char * NxtTkn; // next token position (only needed for Visual C++)
StrPtr = fgets(Buf, 80, stdin);
TknChr = strtok_s(Buf, " {[,]}", &NxtTkn);
while (s <= MAXNBRELM && TknChr != NULL) {
IntArr[s][0] = atoi(TknChr);
TknChr = strtok_s(NULL, " {[,]}", &NxtTkn);
if (TknChr != NULL) {
IntArr[s][1] = atoi(TknChr);
TknChr = strtok_s(NULL, " {[,]}", &NxtTkn);
s++;
}
}
NbrElm = s;
for (s = 0; s < NbrElm; s++)
printf("%d %d\n", IntArr[s][0], IntArr[s][1]);
return 0;
}
This is for Visual Studio, is why I needed to use strtok_s and &NxtTkn.

Concat LPSTR in C

Trying to use as basic C as I can to build a list of numbers from 1-52 in a random order (deck of cards). Everything works, but all of my attempts to concat the strings and get a result end in failure. Any suggestions? NOTE: This is not homework it's something I'm using to create a game.
// Locals
char result[200] = ""; // Result
int card[52]; // Array of cards
srand(time(0)); // Initialize seed "randomly"
// Build
for (int i=0; i<52; i++) {
card[i] = i; // fill the array in order
}
// Shuffle cards
for (int i=0; i<(52-1); i++) {
int r = i + (rand() % (52-i));
int temp = card[i]; card[i] = card[r]; card[r] = temp;
}
// Build result
for (int c=0; c<52; c++) {
// Build
sprintf(result, "%s%d", result, card[c]);
// Comma?
if ( c < 51 )
{
sprintf(result, "%s%s", result, ",");
}
}
My end result is always garbled text. Thanks for the help.
You keep writing to the same position of "result".
sprintf is not going to do the appending for you.
You may consider, after each sprintf, get the return value (which is the number of char written), and increment the pointer to result buffer. i.e. something like:
(psuedo code):
char result[200];
char * outputPtr = result;
for (int c=0; c<52; c++) {
// Build
int n = sprintf(outputPtr, "%d%s", card[c], (c<51 ? "," : ""));
outputPtr += n;
}
Are we writing C++ or C? In C++, concat-ing a string is just:
string_out = string_a + string_b
…since you'd be using std::string.
Furthermore, if this is C++, the STL has a std::shuffle function.
If this is C, note that all your sprintfs aren't concatenating strings, they're just overwriting the old value.
I think, if memory serves, that sprintf will always write into the buffer starting at byte 0. This means that you would be writing the first couple of bytes over and over again with a number, then a comma, then a number. Check if your first bytes are ",[0-9]" - if so, that's your issue.
This would add a comma between each number in the result string:
// Get a pointer to the result string
char* ptr = &result[0];
for (int c = 0; c < 52; c++) {
// Add each cards number and increment the pointer to next position
ptr += sprintf(ptr, "%d", card[c]);
// Add a separator between each number
if (c < 51) {
*ptr++ = ',';
}
}
// Make sure the result string is null-terminated
*ptr = 0;

Resources