Skip to content

Added hw_1 - #2

Open
rmnsmnsk wants to merge 5 commits into
mainfrom
hw_1
Open

Added hw_1#2
rmnsmnsk wants to merge 5 commits into
mainfrom
hw_1

Conversation

@rmnsmnsk

Copy link
Copy Markdown
Owner

No description provided.

@ArtemNik1tin ArtemNik1tin left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вынесите логику из main, в остальном нормально

Comment thread src/hw_1/func.h
@@ -0,0 +1,7 @@
#pragma once

int cleanStr(const char* q);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нет комментариев

Comment thread src/hw_1/func.c
#include <stdlib.h>
#include <string.h>

int cleanStr(const char* q)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут что-то странное происходит, функция по сути просто считает количество элементов в строке. А ещё называть переменные одной буквой плохо)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если эта функция вообще нужна, назовите например getSizeBeforeLineBreak

Comment thread src/hw_1/func.c

char* copyStr(const char* buffer, int j)
{
char* copy = malloc(j + 1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Добавьте проверку аллокации. А ещё называть строку глаголом в повелительном наклонении странновато

Comment thread src/hw_1/func.c
char* copyStr(const char* buffer, int j)
{
char* copy = malloc(j + 1);
for (int i = 0; i < j; i++) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А что если j больше чем размер буфера?

Comment thread src/hw_1/func.c
return j;
}

char* copyStr(const char* buffer, int j)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
char* copyStr(const char* buffer, int j)
char* copyStr(const char* source, int numberOfCharsForCopying)

Comment thread src/hw_1/func.c
word[len] = '\0';
int y = cleanStr(word);
char* cleanWord = copyStr(word, y);
free(word);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Подумайте как можно обойтись без этой аллокации

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А то на одно слово 2 раза память выделяется, причём первый раз сильно больше чем нужно в действительности

Comment thread src/hw_1/func.c
}
if (commaCount == n - 1) {
i++;
while (q[i] != ',' && i < strlen(q)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вынесите в отдельную функцию, а то копипаст получается

Comment thread src/hw_1/func.c
}

int dots = 0;
while (str[i] != '\0') {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А \n?

Comment thread src/hw_1/read.c

char* copy = copyStr(buffer, k);

char** tempData = realloc(data, rows * sizeof(char*));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Так делать плохо, во-первых из-за того что частые реаллокации создают проблему фрагментации памяти, во-вторых системные вызовы это дорого, нужно пытаться их оптимизировать

Comment thread src/hw_1/read.c
#include <stdlib.h>
#include <string.h>

int main()

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тут не должно быть никакой логики, попилите на функции и сделайте какое-нибудь представление таблицы в виде структуры, чтобы можно было разные файлики подгружать, а не только "files/input3.csv"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants