LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-11-2019, 12:23 PM   #136
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,897
Blog Entries: 13

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393

The problem I'm talking about has nothing to do with allocation or variable sizes.

What's wrong with this line?
Code:
while( fgets(array, arraySize, arrayfile ) !=NULL) {
You've seen the problem before.
 
Old 08-11-2019, 12:35 PM   #137
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,897
Blog Entries: 13

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393
On a side note, I don't recommend you use realloc().

A more typical thing for me would be to have a linked list of structures and I'd allocate/free members as they were added and removed from the list.

I'm obviously atypical, therefore no worries, experiment away.
 
1 members found this post helpful.
Old 08-12-2019, 12:51 AM   #138
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,890

Original Poster
Rep: Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481
Quote:
Originally Posted by rtmistler View Post
The problem I'm talking about has nothing to do with allocation or variable sizes.

What's wrong with this line?
Code:
while( fgets(array, arraySize, arrayfile ) !=NULL) {
You've seen the problem before.
I don't know. I've tried reading the man page for fgets(), and nothing it says is giving me any clue as to what the problem is, nor is gdb giving me any clue. I'm sure I probably have seen this problem, as I'm sure I've probably had nearly, if not, every other problem one could ever have too. I'm sorry, I'm NOT trying to be difficult, but it simply isn't obvious to me what the problem even just might be. I've tried changing the part of the call to fgets() in the line you quoted above for the array size, but again, nothing I try will work - it still changes the size of the array with realloc() anyway, regardless of what I try. Can you just tell what the problem is?

Quote:
Originally Posted by rtmistler View Post
On a side note, I don't recommend you use realloc().

A more typical thing for me would be to have a linked list of structures and I'd allocate/free members as they were added and removed from the list.

I'm obviously atypical, therefore no worries, experiment away.
While I have a basic understanding of structures, and while that's another thing on the list of things to explore more, right now that's beyond my current skill level. And if I'm having enough trouble just trying to get the above code to only change the array size with realloc() IF, and only IF there's more that 10 elements worth of data in the text file my code is calling, then I'm not going to be able to do what you suggest above at this point, sorry.
 
Old 08-12-2019, 04:34 AM   #139
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,110
Blog Entries: 17

Rep: Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792
A few pointers (if you'll excuse the pun ):
Code:
char *array = NULL;
sizeof(array)
What do you think sizeof (array) gives? And by extension, what's given by:
sizeof(array) / sizeof(int) in your program.

What does sizeof (int) used throughout your program have to do with anything?


Anyway, I'm not entirely sure what your intentions are with this program. It looks like you're trying to implement something akin to getline(), but if that's the case it isn't going to work the way it's written, even if you fixed the above.

Last edited by GazL; 08-12-2019 at 04:49 AM.
 
Old 08-12-2019, 04:47 AM   #140
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,897
Blog Entries: 13

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393
Why don't you examine what possible error codes and return values you may obtain from each library function.

Best I have for you here.

If you have difficulties with structures, then why in the world are you playing with pointers, memory allocation, or functions like realloc and strtok?
 
Old 08-12-2019, 04:48 AM   #141
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,890

Original Poster
Rep: Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481
I was thinking it gives the size of the array, but I'm not sure. I was thinking sizeof(int) tells gcc the size of the array's elements, but again I'm not sure - I looked at the other program I wrote in my other thread that sums the array.

I was just trying to learn how to do both dynamic memory allocation, and reallocate the memory if the array size was too small for the string(s) being uploaded from the file into the array. Because I want to add it to my ffcliFront program, so I don't need a "fixed array size".
 
Old 08-12-2019, 05:00 AM   #142
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,890

Original Poster
Rep: Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481
Quote:
Originally Posted by rtmistler View Post
...
If you have difficulties with structures, then why in the world are you playing with pointers, memory allocation, or functions like realloc and strtok?
Well, I can't say I'm an expert on array's either (I do understand them a lot better than I did before), that's why I haven't even tried to write my own graphics library, as much as that was something I was reading about (not because I want to try and write one, just wondering how that sort of things works, as I've always wondered about that). It doesn't mean I don't know what an array is, and how to declare one. I don't see why I need to be an expert on doing dynamic memory allocation/reallocation the "atypical" way you would do it, just to learn something about dynamic memory allocation, pointers, etc. I don't remember reading anything that says something to the effect of "you must be an expert on structures, etc, to have any chance at all of using calloc(), malloc(), realloc(), etc".

I also said that I DO understand what a structure is, and DO understand how to write one - I was just pointing out that I need to take things one step at a time.
 
Old 08-12-2019, 05:49 AM   #143
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,110
Blog Entries: 17

Rep: Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792
Despite the name 'array' that you've given it, it is defined as a pointer in this program.
char array[n]; is an array
char *array; is a pointer.
char *array[n]; is an array of pointers.

In C, arrays are implemented using pointers and are interchangeable in many uses, but sizeof isn't one of them.

And no, sizeof (int) will give you just that, the size of an int, it has nothing to do with the number of members in an array (except when it's used as a divisor to calculate the number of members in an array of ints by dividing the size]).

If you're going to progress any further you'll need a solid foundation to build upon. You'll need to understand this stuff. Give the book(s) another review.

Last edited by GazL; 08-12-2019 at 05:50 AM.
 
1 members found this post helpful.
Old 08-12-2019, 07:25 AM   #144
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,890

Original Poster
Rep: Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481
Thank you GazL.

So would I need to declare an array normally (without pointers)?

Anyway, I guess I've got more to do...
 
Old 08-12-2019, 08:24 AM   #145
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,110
Blog Entries: 17

Rep: Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792
Quote:
Originally Posted by jsbjsb001 View Post
So would I need to declare an array normally (without pointers)?
Not if you're intending to use it with malloc() no. The take home here is simply that you can't use sizeof like that when you're using a pointer to dynamically allocated memory. But then, if you've allocated it with malloc() you already know how big it is because you had to tell malloc how much to allocate in the first place, so there's no need to.
 
1 members found this post helpful.
Old 08-12-2019, 08:58 AM   #146
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,897
Blog Entries: 13

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393
To cut to the chase. The mistake is this, which is in fact the same problem I introduced by way of a non-tested example, ere long ago when I wrote it without compiling and testing.
Same problem.

Incorrect:
Code:
while( fgets(array, arraySize, arrayfile ) !=NULL) {
Correct:
Code:
while( fgets(array, arraySize, arrayfile ) !=EOF) {
Quote:
Originally Posted by jsbjsb001 View Post
Well, I can't say I'm an expert on array's either (I do understand them a lot better than I did before), that's why I haven't even tried to write my own graphics library
Writing a graphics library is very advanced coding. I've never even approached this type of effort, for a couple of reasons. First, because it is very difficult to do, there are so very many things to consider, it is a daunting project to undertake. Second, a ton, and I mean an TON of graphical libraries do in fact exist. Therefore, why fight the wheel and try to reinvent it? I would not recommend approaching this type of programming project anytime soon.
Quote:
Originally Posted by jsbjsb001 View Post
I also said that I DO understand what a structure is, and DO understand how to write one - I was just pointing out that I need to take things one step at a time.
My opinions about taking things one step at a time, would include:
  • Not trying to involve dynamic allocation when you aren't sure about it.
  • Not processing strings when you aren't sure about that either.
  • Instead, hard coding the argument strings, using static allocations of memory (arrays) to store strings, just make them sufficiently large to fit all you need.
  • Slowly adding in one additional technical concept at a time, testing it thoroughly to ensure you've got it correct, freezing the confirmed code (no longer changing it), and moving on to the next technical challenge.
  • You're writing something to do a process. How I approach that is:
    1. Determine the primary steps, such as 1, 2, 3, 4, ... They may be Read input, Parse input, Organize input into an argument list for an execute call, Perform the execute call
    2. So I ONLY write the Read input part first, test it out, verify I got it correct, make it a function, and then leave it alone
    3. Next I proceed to Parse input. Same detail I confirm I got it correct, debug it. And ... believe it or not, may discover that my reading of input was not fully tested, or lacking, so that I may go back to fix that, but I fix it independently and leave the form of the resultant read in input in the same form, so that my parse input process still has what it expected
    4. Next I work on organizing all the input data into an argument list. Sounds simple, but not always. Obviously I do it the fastest, most prudent, and easiest way first, and then I look for ways to make it better, but actually "better". The difference between dynamically allocating versus using, and re-using a fixed array, especially when these arrays are ... 128-256 bytes? I may never choose to go dynamic. I also consider that my argument list may always be the same length. Why make my life difficult by allocating memory, when I'm likely to always have memory in use, just instead pre-allocate it as part of my variable declaration, and I'm done. I don't have to worry about screwing it up.
    5. Finally moving to the call to exec. Again, something to debug, things to learn. But ultimately the project gets done and I feel I've accomplished it. I save that, and then consider alternatives, but I also consider whether or not any alternatives are necessary.
 
1 members found this post helpful.
Old 08-12-2019, 09:27 AM   #147
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,110
Blog Entries: 17

Rep: Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792
Quote:
Originally Posted by rtmistler View Post
Incorrect:
Code:
while( fgets(array, arraySize, arrayfile ) !=NULL) {
Correct:
Code:
while( fgets(array, arraySize, arrayfile ) !=EOF) {
I believe you've got that the wrong way around. I think you're maybe thinking about fgetc()?
Code:
FGETC(3)                                       Linux Programmer's Manual                                       FGETC(3)

NAME
       fgetc, fgets, getc, getchar, ungetc - input of characters and strings

SYNOPSIS
       #include <stdio.h>

       int fgetc(FILE *stream);

       char *fgets(char *s, int size, FILE *stream);

       int getc(FILE *stream);

       int getchar(void);

       int ungetc(int c, FILE *stream);
 
2 members found this post helpful.
Old 08-12-2019, 10:13 AM   #148
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,897
Blog Entries: 13

Rep: Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393Reputation: 3393
You are correct GazL. fgets() will return NULL.
 
Old 08-12-2019, 11:44 AM   #149
GazL
LQ Guru
 
Registered: May 2008
Posts: 5,110
Blog Entries: 17

Rep: Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792Reputation: 2792
jsb,

Here's an example of a full-on dynamic allocation, using realloc() to resize the array table as needed. I've set it to double the array each time it needs expanding but it could just increment linearly by '+ 10' rather than a '* 2'

It's based on NevemTeve's suggestion of using structs to keep all the data together, and it's rather involved, so there's a good chance you're not going to follow some of it.

As rtmistler said earlier, one would likely use a link-list in cases like this (or just use a statically oversized array), but just to show you what's involved with using realloc -- and to get you to think about whether on consideration you really want to do so -- I thought I'd knock one up:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

#define LINES_INITIAL_SIZE 2
#define LINES_INCREMENT_BY * 2

struct Lines
{
    size_t count;
    size_t capacity;
    char **lineArray;
};

void BOOM( void )
{
    fprintf(stderr,"BOOM!\n");
    exit(EXIT_FAILURE);
    return;
}

char *stripNewline( char *string )
{
    if (string)
    {
        char *nl = strrchr(string, '\n');
        if ( nl )
            *nl = '\0';
    }
    return string;
}

struct Lines *allocLines()
{
    struct Lines *new = malloc( sizeof (struct Lines) );
    if ( new )
    {
        new->count = 0;
        new->capacity = 0;
        new->lineArray = NULL;
    } else {
        BOOM();
    }
    return new;
}

struct Lines *freeLines( struct Lines *lines )
{
    for ( char **line = lines->lineArray ; *line != NULL ; line++ )
        free(*line);

    free(lines->lineArray);
    free(lines);
    
    return NULL;
}

char *addLine( struct Lines *lines, char *line )
{
    if ( !lines || !line )
        return NULL;

    if ( lines->count + 1 >= lines->capacity )
    {
        size_t newSize = ( lines->capacity )? lines->capacity LINES_INCREMENT_BY : LINES_INITIAL_SIZE;
        if ( newSize <= lines->capacity || newSize < 2 )
            BOOM();
        
        void *m = realloc(lines->lineArray, newSize * sizeof lines->lineArray);
        if (m)
        {
            lines->capacity = newSize;
            lines->lineArray = m;
        } else {
            BOOM();
        }
    }
    
    char *s = strdup(line);
    if ( !s )
        BOOM();

    lines->lineArray[lines->count++] = s;
    lines->lineArray[lines->count] = NULL;
    
    return s;
}

int main( int argc, char *argv[] )
{
    struct Lines *my_lines = allocLines();
    char *lineBuf = NULL;
    size_t bufSize = 0;
    
    while ( getline(&lineBuf, &bufSize, stdin) != -1 )
        if (errno)
            BOOM();
        else
            addLine(my_lines, stripNewline(lineBuf));

    free(lineBuf);


    for ( size_t i = 0 ; i < my_lines->count ; i++ )
        printf("line: %s\n", my_lines->lineArray[i]);

    my_lines = freeLines(my_lines);
    
    return EXIT_SUCCESS;
}
As you can see, unless you have a really good reason to use realloc(), you probably won't want to, because there are easier ways of managing a list of strings than this.
 
2 members found this post helpful.
Old 08-13-2019, 08:34 AM   #150
jsbjsb001
Senior Member
 
Registered: Mar 2009
Location: Earth? I would say I hope so but I'm not so sure about that... I could just be a figment of your imagination too.
Distribution: Currently OpenMandriva. Previously openSUSE, PCLinuxOS, CentOS, among others over the years.
Posts: 2,890

Original Poster
Rep: Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481Reputation: 1481
Thanks again guys, and thanks for the example GazL!

It's not so much the concepts themselves, like for example; I can see the concepts that are being employed, like in the example above, I see there's a structure called "Lines", it has 3 members (I'm not quite sure yet how size_t is different to an int), two "size_t" variables, and a char pointer called "lineArray" (I think the two "**" means a pointer to a pointer?). There's some functions following the structure, like the "boom" function that by the looks of it is called if the allocation fails, and other functions following that, etc, etc.

I'm not really sure how to explain it in a way that will make sense of what I mean, but anyways... It's more trying read the code and understand the program as a whole. Or maybe in other words; trying to put "it all together", and understand why the particular concepts are being employed, and why they are being employed in the way they are, then trying to follow the code from start to finish. That seems to be the most difficult part for me. Particularly trying to understand how pointers work, while trying to understand how code like the above works. Sorry if that doesn't really make sense, but it's the only way I can really explain it.

Or maybe put another way again; I get what an if statement does, "if something is true", then execute the statements under it. Or in terms of structures, and without structures, there would be no "logical" connection between the members that you would otherwise use a structure for - to provide that "logical" connection. Like I said, it's trying to "put it all together" that gets me.

It's easy in terms of Linux and non-programming related problems, a lot of the time what to do in a given situation is just automatic, like troubleshooting a non-programming related problem. I've done it that many times, you just know instinctively what to do/try to get something working, even if I'm not 100% sure about it. Like it's child's play for you guys to just read the code and just "know" what to look at, how to interpret it, then figure out where the problem(s) lie, and what to do to fix it - clearly when it comes to programming, I haven't got that ability yet.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] virtualbox installs pcbsd again and again and again straffetoebak Linux - Virtualization and Cloud 4 11-21-2014 07:14 PM
LXer: Do you want a serious—I mean serious—developer laptop? Then Dell and Ubuntu have the system fo LXer Syndicated Linux News 0 11-29-2012 03:30 PM
Firefox...I have tried and tried... Basslord1124 Fedora 4 10-29-2004 11:51 PM
my ps/2's wheel doesn't work. tried and tried but failed. Choey Linux - Hardware 5 09-17-2003 06:47 PM
I have tried and tried, I really have! Ewen Linux - General 13 01-14-2003 11:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:40 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration