0

I'm trying to make a table of binary strings of different sizes, without the values comporting only 0's. I'm very probably not using the best technique to do that but still, I think there is some way for this to work.

My question is about the function

void addbinarystring(char **prev, int size)

Here is my code:

#include <iostream>

using namespace std;

void addbinarystring(char **prev, int size) {
    cout << (prev+size);
    if (*prev[0] == '0') {
        cout << "one";
        *(prev+size)[0] = '1';
    } else if (*prev[0] == '1') {
        *(prev+size) = '0';
        addbinarystring(prev-1, size);
    }
}

int main() {
    char *size1[1];
    size1[0] = (char*) malloc(sizeof(char));
    size1[0] = "1";
    char *size2[3];
    size2[0] = (char*) malloc(6*sizeof(char));
    size2[0] = "01";
    size2[1] = "10";
    size2[2] = "11";
    char *size3[7][3];
    size3[0][0] = (char*) malloc(21*sizeof(char));
    size3[0][0] = "0";
    size3[0][1] = "0";
    size3[0][2] = "1";
    for (int i = 0; i < 7; i++)
    {
        addbinarystring(&size3[i][2], 3);
    }
}

I don't understand why I get a segmentation fault error on the lines with *(prev + size), I thought it would access the memory where my next binary string is located. I have initialized the memory with malloc.

as I said, this is probably not the best way to get where I want to, but now that I'm in it I really want it to work. Thanks for any help!

Daniel
  • 1
  • 1
  • you want to do it this way, ok. Fwiw, `std::vector` would turn your code to 2-3 lines – 463035818_is_not_an_ai Mar 27 '23 at 09:57
  • what do you expect `size2[0] = (char*) malloc(6*sizeof(char));` followed by assigning to `size2[0]` again in the next line to be good for? – 463035818_is_not_an_ai Mar 27 '23 at 09:59
  • I dare to disagree. The way to make this work is to start from scratch. You seem to misunderstand certain things you are using in the code. Thats no shame, because c-arrays and manual memory managment, and `malloc` in particular, are all highly advanced topics in c++. If you don't know how to use them, you will very likely use them wrong – 463035818_is_not_an_ai Mar 27 '23 at 10:02
  • Is this supposed to be a C++ or C question? I guess the wrong tag has been used. – Red.Wave Mar 27 '23 at 10:06
  • Red.Wave: It's using iostream and namespace std. This "makes" it C++ :P. – m2j Mar 27 '23 at 10:08
  • @m2j this language is called "C with " – n. m. could be an AI Mar 27 '23 at 10:12
  • @m2j that might just show the OP's confusion. No part of C++ std library has been used. The rest of this code is pure C. We need to confirm the domain of the question first. – Red.Wave Mar 27 '23 at 10:12
  • There are multiple issues in your code, so writing an answer would be tough, that answer would confuse you even more. Fundamentally, however, you should *avoid* raw pointers, arrays of raw pointers, or `malloc()` as a beginner to C++. Instead, rewrite your code to use `std::vector` - but read *documentation* for both `std::vector` and `std::string` first. You'll find that, if you set things up right that operations on elements of a `vector` and on `string`s work more like you have (incorrectly) expected them to work with pointers. – Peter Mar 27 '23 at 10:14
  • @Red.Wave if it is compiled as C++ then it is C++. Same code is not necessarily the same meaning in C and C++. If this was tagged C, then OP would get a C answer and continue to believe it would be good to use malloc and c-strings in C++ – 463035818_is_not_an_ai Mar 27 '23 at 10:19
  • @Red.Wave Even if we remove the C++-specific parts of the question (and focus on the C parts) answering this question would be a tough ask. The OP has written code that is equally incorrect in both C and C++, based on multiple fundamental misunderstandings of how pointers and arrays of pointers work. It'll still be no easier to write a sensible answer (one that will help the OP understand, and allow him to write code using raw pointers and arrays of raw pointers that will work) regardless of whether the question is tagged C or C++. – Peter Mar 27 '23 at 10:22
  • @463035818_is_not_a_number , but if he needs C, then most of what we like to give him won't be useful. In C++ this code is not welcome; In C, it's just normal(with bugs). – Red.Wave Mar 27 '23 at 10:22
  • @Peter , the level of abstraction, and proper approach to put him on track depends on the language. If he wants to program in C, then he must be advised to learn proper material on raw pointers. If C++ is intended, higher level abstractions can help him get a smoother start. Raw pointers - even smart pointers - can wait a bit. Modern C++ progrmmers should not jump to pointers too early; The hazards an complexities would be frustrating. – Red.Wave Mar 27 '23 at 10:29
  • @Red.Wave why do you think they need C? As they are using `std::cout` they are certainly compiling c++ and not c – 463035818_is_not_an_ai Mar 27 '23 at 10:41
  • 1
    @Red.Wave - I agree that the approach to answer depends on chosen language. The question was tagged C++, and comments already give suggestions for the OP to better tackle his problem (write code to do what he wants) in C++. With the parts that are *also* problems in C there are still multiple interacting concerns so, as I said, writing a useful answer would be difficult. Instead, the OP needs to go back and learn multiple fundamentals correctly, before trying to use them all at once. Getting such code correct is not a doddle for a seasoned C developer either, let alone a beginner. – Peter Mar 27 '23 at 10:45
  • 3
    Thanks a lot for the replies. I'm coding this for uni, so I'm indeed a beginner and still learning. I've been coding in C last semester and am now on an exchange learning C++ which is probably where my confusion also comes from. If I undestand well, my struggle comes partly from mixing up both languages so I will try to get a new understanding from scratch about C++ and come back here; Thanks again! – Daniel Mar 27 '23 at 11:12
  • 1
    "my struggle comes partly from mixing up both languages" I would say that your problem stem from insufficient understanding of pointers in C. There is nothing from C++ in this program apart from `cout << `. The remainder is C, and the problems begin and end in the C part. – n. m. could be an AI Mar 27 '23 at 11:36
  • OP, you have the right attitude. I would spend a bit of time reading one of [these](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Paul Sanders Mar 27 '23 at 12:06
  • @Daniel , the choice of language affects when to get involved with pointers. In the end you need to understand them correctly. But if you're going to learn C++, you need first unlearn C. Because the mentality of C programming prevents progress in modern C++. I mean the correct order of learnin is C++ first, C next. Otherwise you'll always carry the dangerous habit of excessive dependency on pointers. – Red.Wave Mar 28 '23 at 12:55

0 Answers0