mersenneforum.org  

Go Back   mersenneforum.org > Extra Stuff > Programming

Reply
 
Thread Tools
Old 2019-06-29, 00:00   #12
ldesnogu
 
ldesnogu's Avatar
 
Jan 2008
France

2·52·11 Posts
Default

Quote:
Originally Posted by ixfd64 View Post
I tried that too, but it always returns 1.
That's odd and definitely wrong and not what I get. What compiler are you using?


Code:
#include <iostream>
#include <stdio.h>

int main()
{
    char *str_arr[] = {"foo","bar"};
    std::cout << sizeof(str_arr) << std::endl;
    std::cout << sizeof(str_arr[0]) << std::endl;
    for (int i = 0; i < sizeof(str_arr) / sizeof(str_arr[0]); i++)
       printf("curr_strA = %s\n", str_arr[i]);
    return 0;
}
$ ./a.out 
16
8
curr_strA = foo
curr_strA = bar
$ g++ --version
g++ (Ubuntu 7.4.0-1ubuntu1~18.04) 7.4.0
ldesnogu is offline   Reply With Quote
Old 2019-06-29, 02:53   #13
ewmayer
2ω=0
 
ewmayer's Avatar
 
Sep 2002
Repรบblica de California

266128 Posts
Default

Quote:
Originally Posted by ldesnogu View Post
You don't really need a sentinel for an array declared at compile time. You can get the numbers of elements with something like sizeof(array)/sizeof(array[0])
Right ... I was thinking of the variable string lengths, but the C array is an array of char*, i.e. of pointers, which are all the same size, and sizeof(array) is just the sum of the individual char* pointers making up the (char**) array. Thanks for the correction to my overhasty post.
ewmayer is offline   Reply With Quote
Old 2019-06-29, 07:17   #14
ldesnogu
 
ldesnogu's Avatar
 
Jan 2008
France

2×52×11 Posts
Default

You can't be blamed, C is such a horrible language... and the only one I use now Modern C++ looks much better these days but the syntax still is awful.
ldesnogu is offline   Reply With Quote
Old 2019-07-01, 23:07   #15
ixfd64
Bemusing Prompter
 
ixfd64's Avatar
 
"Danny"
Dec 2002
California

2,411 Posts
Default

Quote:
Originally Posted by ldesnogu View Post
That's odd and definitely wrong and not what I get. What compiler are you using?


Code:
#include <iostream>
#include <stdio.h>

int main()
{
    char *str_arr[] = {"foo","bar"};
    std::cout << sizeof(str_arr) << std::endl;
    std::cout << sizeof(str_arr[0]) << std::endl;
    for (int i = 0; i < sizeof(str_arr) / sizeof(str_arr[0]); i++)
       printf("curr_strA = %s\n", str_arr[i]);
    return 0;
}
$ ./a.out 
16
8
curr_strA = foo
curr_strA = bar
$ g++ --version
g++ (Ubuntu 7.4.0-1ubuntu1~18.04) 7.4.0
I'm using Clang on macOS.

This is the line in question:

Code:
printf("array size=%d",sizeof(device_class)/sizeof(device_class[0]));
Output:

Quote:
array size=1

Last fiddled with by ixfd64 on 2019-07-01 at 23:07
ixfd64 is offline   Reply With Quote
Old 2019-07-02, 21:41   #16
ixfd64
Bemusing Prompter
 
ixfd64's Avatar
 
"Danny"
Dec 2002
California

2,411 Posts
Default

I did some further research and found out it's not possible to get the length of an array given only a pointer. However, I found a solution that's acceptable enough: define the array as normal and insert a NULL after the last item. We then loop through the array (using an arbitrary index) until we reach the NULL character. The null check is done first so we don't get a segmentation fault. This solution isn't as elegant as a foreach loop, but at least it's relatively clean.

Last fiddled with by ixfd64 on 2019-07-02 at 21:42
ixfd64 is offline   Reply With Quote
Old 2019-07-05, 09:11   #17
lavalamp
 
lavalamp's Avatar
 
Oct 2007
Manchester, UK

24·5·17 Posts
Default

Perhaps unordered map is the data structure you're looking for here?

http://www.cplusplus.com/reference/u...unordered_map/

I wouldn't really advise using raw pointers in C++ anymore, there are plenty of STL containers that will do the work for you in memory safe ways that save you the headaches of checking bounds all the time.

I'm not sure in what way you're trying to mantain compatability with mfakto, but it's perfectly possible to convert between C strings and C++ std::string. You can construct a std::string with a C string, and there's a c_str member function to convert back if/when needed.

In C++ you can loop over all std::strings in a vector easily, or search for a std::string in a map or unordered map. IMO it results in simpler, cleaner code that shows a clearer intent when reading the code back in future.
lavalamp is offline   Reply With Quote
Old 2019-07-05, 09:28   #18
xilman
Bamboozled!
 
xilman's Avatar
 
"๐’‰บ๐’ŒŒ๐’‡ท๐’†ท๐’€ญ"
May 2003
Down not across

10,949 Posts
Default

Quote:
Originally Posted by lavalamp View Post
I wouldn't really advise using ... C++ anymore...
Code:
When I find my code in tons of trouble,
Friends and colleagues come to me,
Speaking words of wisdom:
"Write in C."

As the deadline fast approaches,
And bugs are all that I can see,
Somewhere, someone whispers:
"Write in C."

Write in C, Write in C,
Write in C, oh, Write in C.
xilman is online now   Reply With Quote
Old 2019-07-05, 10:07   #19
lavalamp
 
lavalamp's Avatar
 
Oct 2007
Manchester, UK

24×5×17 Posts
Default

Agree to disagree on that one.

https://www.youtube.com/watch?v=u-7qFAuFGao

Last fiddled with by lavalamp on 2019-07-05 at 10:08
lavalamp is offline   Reply With Quote
Old 2019-07-05, 12:10   #20
ldesnogu
 
ldesnogu's Avatar
 
Jan 2008
France

2·52·11 Posts
Default

I had an advanced C++ training and I admit that I like what the language has become. The problem is that the syntax still remains horrible and if you don't practice a lot, you can't remember it.


I have quite fond memories of CommonLISP. You can't forget the syntax
ldesnogu is offline   Reply With Quote
Old 2019-07-05, 16:42   #21
xilman
Bamboozled!
 
xilman's Avatar
 
"๐’‰บ๐’ŒŒ๐’‡ท๐’†ท๐’€ญ"
May 2003
Down not across

10,949 Posts
Default

Quote:
Originally Posted by ldesnogu View Post
I have quite fond memories of CommonLISP. You can't forget the syntax
By far my favourite language is still Algol 68. BCPL was a fine language too. Each of them are much nicer to use than C or C++.

It's a great shame that C was developed from B and not from BCPL --- which it quite easily could have been. Equally, if A68 had had the effort put into it it would still be a mass-market language in the way that FORTRAN and LISP still are today despite being invented over 60 years ago.

Last fiddled with by xilman on 2019-07-05 at 16:43 Reason: Fix spelling mitsake.
xilman is online now   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple factoring challenge + questions siegert81 Factoring 12 2016-05-28 18:36
Programming questions Xyzzy Programming 76 2015-01-24 04:35
Need some help on php programming pinhodecarlos Programming 2 2012-07-23 18:17
New to programming. What to do? lorgix Miscellaneous Math 9 2010-12-08 22:22
plz, help me in c programming alaa Homework Help 12 2007-06-12 22:17

All times are UTC. The time now is 08:01.


Sun Oct 17 08:01:52 UTC 2021 up 86 days, 2:30, 0 users, load averages: 1.98, 1.72, 1.51

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.